1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\LastFm\Service; |
13
|
|
|
|
14
|
|
|
use Core23\LastFm\Client\ApiClientInterface; |
15
|
|
|
use Core23\LastFm\Filter\Period; |
16
|
|
|
use Core23\LastFm\Filter\RangeFilter; |
17
|
|
|
use Core23\LastFm\Model\Album; |
18
|
|
|
use Core23\LastFm\Model\Artist; |
19
|
|
|
use Core23\LastFm\Model\Chart; |
20
|
|
|
use Core23\LastFm\Model\Song; |
21
|
|
|
use Core23\LastFm\Model\SongInfo; |
22
|
|
|
use Core23\LastFm\Model\Tag; |
23
|
|
|
use Core23\LastFm\Model\User; |
24
|
|
|
use Core23\LastFm\Util\ApiHelper; |
25
|
|
|
|
26
|
|
|
final class UserService implements UserServiceInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ApiClientInterface |
30
|
|
|
*/ |
31
|
|
|
private $client; |
32
|
|
|
|
33
|
|
|
public function __construct(ApiClientInterface $client) |
34
|
|
|
{ |
35
|
|
|
$this->client = $client; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getArtistTracks(string $username, string $artist, ?RangeFilter $filter = null, int $page = 1): array |
39
|
|
|
{ |
40
|
|
|
$query = null !== $filter ? $filter->getQuery('startTimestamp', 'endTimestamp') : []; |
41
|
|
|
$query = array_merge($query, [ |
42
|
|
|
'user' => $username, |
43
|
|
|
'artist' => $artist, |
44
|
|
|
'page' => $page, |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$response = $this->client->unsignedCall('user.getArtistTracks', $query); |
48
|
|
|
|
49
|
|
|
if (!isset($response['artisttracks']['track'])) { |
50
|
|
|
return []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return ApiHelper::mapList( |
54
|
|
|
static function ($data) { |
55
|
|
|
return Song::fromApi($data); |
56
|
|
|
}, |
57
|
|
|
$response['artisttracks']['track'] |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getFriends(string $username, bool $recenttracks = false, $limit = 50, int $page = 1): array |
62
|
|
|
{ |
63
|
|
|
$response = $this->client->unsignedCall('user.getFriends', [ |
64
|
|
|
'user' => $username, |
65
|
|
|
'recenttracks' => (int) $recenttracks, |
66
|
|
|
'limit' => $limit, |
67
|
|
|
'page' => $page, |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
if (!isset($response['friends']['user'])) { |
71
|
|
|
return []; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return ApiHelper::mapList( |
75
|
|
|
static function ($data) { |
76
|
|
|
return User::fromApi($data); |
77
|
|
|
}, |
78
|
|
|
$response['friends']['user'] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getInfo(string $username): ?User |
83
|
|
|
{ |
84
|
|
|
$response = $this->client->unsignedCall('user.getInfo', [ |
85
|
|
|
'user' => $username, |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
if (!isset($response['user'])) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return User::fromApi($response['user']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getLovedTracks(string $username, int $limit = 50, int $page = 1): array |
96
|
|
|
{ |
97
|
|
|
$response = $this->client->unsignedCall('user.getLovedTracks', [ |
98
|
|
|
'user' => $username, |
99
|
|
|
'limit' => $limit, |
100
|
|
|
'page' => $page, |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
if (!isset($response['lovedtracks']['track'])) { |
104
|
|
|
return []; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return ApiHelper::mapList( |
108
|
|
|
static function ($data) { |
109
|
|
|
return Song::fromApi($data); |
110
|
|
|
}, |
111
|
|
|
$response['lovedtracks']['track'] |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getRecentTracks(string $username, ?RangeFilter $filter = null, $extended = false, $limit = 50, int $page = 1): array |
116
|
|
|
{ |
117
|
|
|
$query = null !== $filter ? $filter->getQuery('from', 'to') : []; |
118
|
|
|
$query = array_merge($query, [ |
119
|
|
|
'user' => $username, |
120
|
|
|
'limit' => $limit, |
121
|
|
|
'page' => $page, |
122
|
|
|
'extended' => (int) $extended, |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
$response = $this->client->unsignedCall('user.getRecentTracks', $query); |
126
|
|
|
|
127
|
|
|
if (!isset($response['recenttracks']['track'])) { |
128
|
|
|
return []; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return ApiHelper::mapList( |
132
|
|
|
static function ($data) { |
133
|
|
|
return Song::fromApi($data); |
134
|
|
|
}, |
135
|
|
|
$response['recenttracks']['track'] |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getPersonalTagsForArtist(string $username, string $tag, int $limit = 50, int $page = 1): array |
140
|
|
|
{ |
141
|
|
|
$response = $this->client->unsignedCall('user.getPersonalTags', [ |
142
|
|
|
'taggingtype' => 'artist', |
143
|
|
|
'user' => $username, |
144
|
|
|
'tag' => $tag, |
145
|
|
|
'limit' => $limit, |
146
|
|
|
'page' => $page, |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
if (!isset($response['taggings']['artists']['artist'])) { |
150
|
|
|
return []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return ApiHelper::mapList( |
154
|
|
|
static function ($data) { |
155
|
|
|
return Artist::fromApi($data); |
156
|
|
|
}, |
157
|
|
|
$response['taggings']['artists']['artist'] |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getPersonalTagsForAlbum(string $username, string $tag, int $limit = 50, int $page = 1): array |
162
|
|
|
{ |
163
|
|
|
$response = $this->client->unsignedCall('user.getPersonalTags', [ |
164
|
|
|
'taggingtype' => 'album', |
165
|
|
|
'user' => $username, |
166
|
|
|
'tag' => $tag, |
167
|
|
|
'limit' => $limit, |
168
|
|
|
'page' => $page, |
169
|
|
|
]); |
170
|
|
|
|
171
|
|
|
if (!isset($response['taggings']['albums']['album'])) { |
172
|
|
|
return []; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return ApiHelper::mapList( |
176
|
|
|
static function ($data) { |
177
|
|
|
return Album::fromApi($data); |
178
|
|
|
}, |
179
|
|
|
$response['taggings']['albums']['album'] |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getPersonalTagsForTracks(string $username, string $tag, int $limit = 50, int $page = 1): array |
184
|
|
|
{ |
185
|
|
|
$response = $this->client->unsignedCall('user.getPersonalTags', [ |
186
|
|
|
'taggingtype' => 'track', |
187
|
|
|
'user' => $username, |
188
|
|
|
'tag' => $tag, |
189
|
|
|
'limit' => $limit, |
190
|
|
|
'page' => $page, |
191
|
|
|
]); |
192
|
|
|
|
193
|
|
|
if (!isset($response['taggings']['tracks']['track'])) { |
194
|
|
|
return []; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return ApiHelper::mapList( |
198
|
|
|
static function ($data) { |
199
|
|
|
return SongInfo::fromApi($data); |
200
|
|
|
}, |
201
|
|
|
$response['taggings']['tracks']['track'] |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function getTopAlbums(string $username, Period $period, int $limit = 50, int $page = 1): array |
206
|
|
|
{ |
207
|
|
|
$response = $this->client->unsignedCall('user.getTopAlbums', [ |
208
|
|
|
'user' => $username, |
209
|
|
|
'period' => $period->getValue(), |
210
|
|
|
'limit' => $limit, |
211
|
|
|
'page' => $page, |
212
|
|
|
]); |
213
|
|
|
|
214
|
|
|
if (!isset($response['topalbums']['album'])) { |
215
|
|
|
return []; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return ApiHelper::mapList( |
219
|
|
|
static function ($data) { |
220
|
|
|
return Album::fromApi($data); |
221
|
|
|
}, |
222
|
|
|
$response['topalbums']['album'] |
223
|
|
|
); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getTopArtists(string $username, Period $period, int $limit = 50, int $page = 1): array |
227
|
|
|
{ |
228
|
|
|
$response = $this->client->unsignedCall('user.getTopArtists', [ |
229
|
|
|
'user' => $username, |
230
|
|
|
'period' => $period->getValue(), |
231
|
|
|
'limit' => $limit, |
232
|
|
|
'page' => $page, |
233
|
|
|
]); |
234
|
|
|
|
235
|
|
|
if (!isset($response['topartists']['artist'])) { |
236
|
|
|
return []; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return ApiHelper::mapList( |
240
|
|
|
static function ($data) { |
241
|
|
|
return Artist::fromApi($data); |
242
|
|
|
}, |
243
|
|
|
$response['topartists']['artist'] |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function getTopTags(string $username, int $limit = 50): array |
248
|
|
|
{ |
249
|
|
|
$response = $this->client->unsignedCall('user.getTopTags', [ |
250
|
|
|
'user' => $username, |
251
|
|
|
'limit' => $limit, |
252
|
|
|
]); |
253
|
|
|
|
254
|
|
|
if (!isset($response['toptags']['tag'])) { |
255
|
|
|
return []; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return ApiHelper::mapList( |
259
|
|
|
static function ($data) { |
260
|
|
|
return Tag::fromApi($data); |
261
|
|
|
}, |
262
|
|
|
$response['toptags']['tag'] |
263
|
|
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function getTopTracks(string $username, Period $period, int $limit = 50, int $page = 1): array |
267
|
|
|
{ |
268
|
|
|
$response = $this->client->unsignedCall('user.getTopTracks', [ |
269
|
|
|
'user' => $username, |
270
|
|
|
'period' => $period->getValue(), |
271
|
|
|
'limit' => $limit, |
272
|
|
|
'page' => $page, |
273
|
|
|
]); |
274
|
|
|
|
275
|
|
|
if (!isset($response['toptracks']['track'])) { |
276
|
|
|
return []; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return ApiHelper::mapList( |
280
|
|
|
static function ($data) { |
281
|
|
|
return SongInfo::fromApi($data); |
282
|
|
|
}, |
283
|
|
|
$response['toptracks']['track'] |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function getWeeklyAlbumChart(string $username, ?RangeFilter $filter = null): array |
288
|
|
|
{ |
289
|
|
|
$query = null !== $filter ? $filter->getQuery('from', 'to') : []; |
290
|
|
|
$query = array_merge($query, [ |
291
|
|
|
'user' => $username, |
292
|
|
|
]); |
293
|
|
|
|
294
|
|
|
$response = $this->client->unsignedCall('user.getWeeklyAlbumChart', $query); |
295
|
|
|
|
296
|
|
|
if (!isset($response['weeklyalbumchart']['album'])) { |
297
|
|
|
return []; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return ApiHelper::mapList( |
301
|
|
|
static function ($data) { |
302
|
|
|
return Album::fromApi($data); |
303
|
|
|
}, |
304
|
|
|
$response['weeklyalbumchart']['album'] |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function getWeeklyArtistChart(string $username, ?RangeFilter $filter = null): array |
309
|
|
|
{ |
310
|
|
|
$query = null !== $filter ? $filter->getQuery('from', 'to') : []; |
311
|
|
|
$query = array_merge($query, [ |
312
|
|
|
'user' => $username, |
313
|
|
|
]); |
314
|
|
|
|
315
|
|
|
$response = $this->client->unsignedCall('user.getWeeklyArtistChart', $query); |
316
|
|
|
|
317
|
|
|
if (!isset($response['weeklyartistchart']['artist'])) { |
318
|
|
|
return []; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return ApiHelper::mapList( |
322
|
|
|
static function ($data) { |
323
|
|
|
return Artist::fromApi($data); |
324
|
|
|
}, |
325
|
|
|
$response['weeklyartistchart']['artist'] |
326
|
|
|
); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
public function getWeeklyChartList(string $username): array |
330
|
|
|
{ |
331
|
|
|
$response = $this->client->unsignedCall('user.getWeeklyChartList', [ |
332
|
|
|
'user' => $username, |
333
|
|
|
]); |
334
|
|
|
|
335
|
|
|
if (!isset($response['weeklychartlist']['chart'])) { |
336
|
|
|
return []; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
return ApiHelper::mapList( |
340
|
|
|
static function ($data) { |
341
|
|
|
return Chart::fromApi($data); |
342
|
|
|
}, |
343
|
|
|
$response['weeklychartlist']['chart'] |
344
|
|
|
); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
public function getWeeklyTrackChart(string $username, ?RangeFilter $filter = null): array |
348
|
|
|
{ |
349
|
|
|
$query = null !== $filter ? $filter->getQuery('from', 'to') : []; |
350
|
|
|
$query = array_merge($query, [ |
351
|
|
|
'user' => $username, |
352
|
|
|
]); |
353
|
|
|
|
354
|
|
|
$response = $this->client->unsignedCall('user.getWeeklyTrackChart', $query); |
355
|
|
|
|
356
|
|
|
if (!isset($response['weeklytrackchart']['track'])) { |
357
|
|
|
return []; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return ApiHelper::mapList( |
361
|
|
|
static function ($data) { |
362
|
|
|
return SongInfo::fromApi($data); |
363
|
|
|
}, |
364
|
|
|
$response['weeklytrackchart']['track'] |
365
|
|
|
); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|