/
ScrobbleMethod.php
132 lines (115 loc) · 5.33 KB
/
ScrobbleMethod.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* vim:set softtabstop=4 shiftwidth=4 expandtab:
*
* LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
* Copyright 2001 - 2020 Ampache.org
*
* 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\Config\AmpConfig;
use Ampache\Repository\Model\Song;
use Ampache\Repository\Model\User;
use Ampache\Module\Api\Api;
use Ampache\Module\System\Session;
use Ampache\Repository\UserRepositoryInterface;
/**
* Class ScrobbleMethod
* @package Lib\ApiMethods
*/
final class ScrobbleMethod
{
private const ACTION = 'scrobble';
/**
* scrobble
* MINIMUM_API_VERSION=400001
*
* Search for a song using text info and then record a play if found.
* This allows other sources to record play history to Ampache
*
* @param array $input
* song = (string) $song_name
* artist = (string) $artist_name
* album = (string) $album_name
* songmbid = (string) $song_mbid //optional
* artistmbid = (string) $artist_mbid //optional
* albummbid = (string) $album_mbid //optional
* date = (integer) UNIXTIME() //optional
* client = (string) $agent //optional
* @return boolean
*/
public static function scrobble(array $input)
{
if (!Api::check_parameter($input, array('song', 'artist', 'album'), self::ACTION)) {
return false;
}
ob_end_clean();
$charset = AmpConfig::get('site_charset');
$song_name = html_entity_decode(scrub_out($input['song']), ENT_QUOTES, $charset);
$artist_name = html_entity_decode(scrub_out($input['artist']), ENT_QUOTES, $charset);
$album_name = html_entity_decode(scrub_out($input['album']), ENT_QUOTES, $charset);
$song_mbid = html_entity_decode(scrub_out($input['song_mbid'] ?? $input['songmbid'] ?? ''), ENT_QUOTES, $charset); //optional
$artist_mbid = html_entity_decode(scrub_out($input['artist_mbid'] ?? $input['artistmbid'] ?? ''), ENT_QUOTES, $charset); //optional
$album_mbid = html_entity_decode(scrub_out($input['album_mbid'] ?? $input['albummbid'] ?? ''), ENT_QUOTES, $charset); //optional
$date = (array_key_exists('date', $input) && is_numeric(scrub_in($input['date']))) ? (int) scrub_in($input['date']) : time(); //optional
$user = User::get_from_username(Session::username($input['auth']));
$user_id = $user->id;
$valid = in_array($user->id, static::getUserRepository()->getValid());
// validate supplied user
if ($valid === false) {
/* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
Api::error(sprintf(T_('Not Found: %s'), $user_id), '4704', self::ACTION, 'empty', $input['api_format']);
return false;
}
// validate minimum required options
debug_event(self::class, 'scrobble searching for:' . $song_name . ' - ' . $artist_name . ' - ' . $album_name, 4);
if (!$song_name || !$album_name || !$artist_name) {
Api::error(T_('Bad Request'), '4710', self::ACTION, 'input', $input['api_format']);
return false;
}
// validate client string or fall back to 'api'
$agent = filter_var($input['client'], FILTER_SANITIZE_STRING) ?? 'api';
$scrobble_id = Song::can_scrobble($song_name, $artist_name, $album_name, $song_mbid, $artist_mbid, $album_mbid);
if ($scrobble_id === '') {
Api::error(T_('Not Found'), '4704', self::ACTION, 'song', $input['api_format']);
} else {
$media = new Song((int) $scrobble_id);
if (!$media->id) {
/* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
Api::error(sprintf(T_('Not Found: %s'), $scrobble_id), '4704', self::ACTION, 'song', $input['api_format']);
return false;
}
debug_event(self::class, 'scrobble: ' . $media->id . ' for ' . $user->username . ' using ' . $agent . ' ' . $date, 5);
// internal scrobbling (user_activity and object_count tables)
if ($media->set_played($user_id, $agent, array(), $date)) {
// scrobble plugins
User::save_mediaplay($user, $media);
}
Api::message('successfully scrobbled: ' . $scrobble_id, $input['api_format']);
}
Session::extend($input['auth']);
return true;
}
/**
* @deprecated inject dependency
*/
private static function getUserRepository(): UserRepositoryInterface
{
global $dic;
return $dic->get(UserRepositoryInterface::class);
}
}