-
Notifications
You must be signed in to change notification settings - Fork 585
/
GetSimilarMethod.php
115 lines (104 loc) · 3.6 KB
/
GetSimilarMethod.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright Ampache.org, 2001-2023
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
declare(strict_types=0);
namespace Ampache\Module\Api\Method;
use Ampache\Module\Api\Api;
use Ampache\Module\Api\Json_Data;
use Ampache\Module\Api\Xml_Data;
use Ampache\Module\Util\Recommendation;
use Ampache\Repository\Model\User;
/**
* Class GetSimilarMethod
* @package Lib\ApiMethods
*/
final class GetSimilarMethod
{
public const ACTION = 'get_similar';
/**
* get_similar
* MINIMUM_API_VERSION=420000
*
* Return similar artist id's or similar song ids compared to the input filter
*
* @param array $input
* @param User $user
* type = (string) 'song', 'artist'
* filter = (integer) artist id or song id
* offset = (integer) //optional
* limit = (integer) //optional
* @return boolean
*/
public static function get_similar(array $input, User $user): bool
{
if (!Api::check_parameter($input, array('type', 'filter'), self::ACTION)) {
return false;
}
$type = (string) $input['type'];
$object_id = (int) $input['filter'];
// confirm the correct data
if (!in_array(strtolower($type), array('song', 'artist'))) {
Api::error(sprintf(T_('Bad Request: %s'), $type), '4710', self::ACTION, 'type', $input['api_format']);
return false;
}
$results = array();
$similar = array();
switch ($type) {
case 'artist':
$similar = Recommendation::get_artists_like($object_id);
break;
case 'song':
$similar = Recommendation::get_songs_like($object_id);
}
foreach ($similar as $child) {
$results[] = $child['id'];
}
if (empty($results)) {
Api::empty($type, $input['api_format']);
return false;
}
ob_end_clean();
switch ($input['api_format']) {
case 'json':
Json_Data::set_offset($input['offset'] ?? 0);
Json_Data::set_limit($input['limit'] ?? 0);
switch ($type) {
case 'artist':
echo Json_Data::artists($results, array(), $user);
break;
case 'song':
echo Json_Data::songs($results, $user);
}
break;
default:
Xml_Data::set_offset($input['offset'] ?? 0);
Xml_Data::set_limit($input['limit'] ?? 0);
switch ($type) {
case 'artist':
echo Xml_Data::artists($results, array(), $user);
break;
case 'song':
echo Xml_Data::songs($results, $user);
}
}
return true;
}
}