-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathspotify.py
More file actions
1054 lines (930 loc) · 38.3 KB
/
Copy pathspotify.py
File metadata and controls
1054 lines (930 loc) · 38.3 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Spotify client for handling connections with Spotify."""
from __future__ import annotations
import asyncio
from dataclasses import dataclass
from importlib import metadata
from typing import TYPE_CHECKING, Any, Callable, Concatenate, Self
from aiohttp import ClientSession
from aiohttp.hdrs import METH_DELETE, METH_GET, METH_POST, METH_PUT
from mashumaro.codecs.json import JSONDecoder
import orjson
from yarl import URL
from spotifyaio.exceptions import (
SpotifyConnectionError,
SpotifyError,
SpotifyNotFoundError,
)
from spotifyaio.models import (
Album,
AlbumsResponse,
AlbumTracksResponse,
Artist,
ArtistResponse,
ArtistTopTracksResponse,
Audiobook,
AudiobookChapterResponse,
AudiobooksResponse,
AudioFeatures,
BasePlaylist,
BaseUserProfile,
CategoriesResponse,
Category,
CategoryPlaylistResponse,
Chapter,
ChaptersResponse,
CurrentPlaying,
Device,
Devices,
Episode,
EpisodesResponse,
FeaturedPlaylistResponse,
FollowedArtistResponse,
FollowType,
Image,
ModifyPlaylistResponse,
NewReleasesResponse,
NewReleasesResponseInner,
PlaybackState,
PlayedTrack,
PlayedTrackResponse,
Playlist,
PlaylistResponse,
PlaylistTrack,
PlaylistTrackResponse,
RepeatMode,
SavedAlbum,
SavedAlbumResponse,
SavedAudiobookResponse,
SavedEpisode,
SavedEpisodeResponse,
SavedShow,
SavedShowResponse,
SavedTrack,
SavedTrackResponse,
SearchResult,
SearchType,
Show,
ShowEpisodesResponse,
SimplifiedAudiobook,
SimplifiedChapter,
SimplifiedEpisode,
TopArtistsResponse,
TopTracksResponse,
UserProfile,
)
from spotifyaio.util import get_identifier
if TYPE_CHECKING:
from collections.abc import Awaitable
from spotifyaio import SimplifiedAlbum, SimplifiedTrack, Track
VERSION = metadata.version(__package__)
def catch_json_decode_error[**P, R](
func: Callable[Concatenate[SpotifyClient, P], Awaitable[R]],
) -> Callable[Concatenate[SpotifyClient, P], Awaitable[R]]:
"""Catch JSON decode errors."""
async def wrapper(self: SpotifyClient, *args: P.args, **kwargs: P.kwargs) -> R:
try:
return await func(self, *args, **kwargs)
except orjson.JSONDecodeError as e: # pylint: disable=no-member
msg = f"Failed to decode JSON: {e}"
raise SpotifyError(msg) from e
return wrapper
@dataclass
class SpotifyClient:
"""Main class for handling connections with Spotify."""
session: ClientSession | None = None
request_timeout: int = 10
api_host: str = "api.spotify.com"
_token: str | None = None
_close_session: bool = False
refresh_token_function: Callable[[], Awaitable[str]] | None = None
async def refresh_token(self) -> None:
"""Refresh token with provided function."""
if self.refresh_token_function:
self._token = await self.refresh_token_function()
def authenticate(self, token: str) -> None:
"""Authenticate the user with a token."""
self._token = token
async def _request(
self,
method: str,
uri: str,
*,
data: dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
) -> str:
"""Handle a request to Spotify."""
url = URL.build(
scheme="https",
host=self.api_host,
port=443,
).joinpath(uri)
await self.refresh_token()
headers = {
"User-Agent": f"AioSpotify/{VERSION}",
"Accept": "application/json, text/plain, */*",
"Authorization": f"Bearer {self._token}",
}
if self.session is None:
self.session = ClientSession()
self._close_session = True
try:
async with asyncio.timeout(self.request_timeout):
response = await self.session.request(
method,
url,
headers=headers,
json=data,
params=params,
)
except asyncio.TimeoutError as exception:
msg = "Timeout occurred while connecting to Spotify"
raise SpotifyConnectionError(msg) from exception
if response.status == 204:
return ""
text = await response.text()
if '"status": 404' in text:
msg = f"Resource not found: {uri}"
raise SpotifyNotFoundError(msg)
return text
async def _get(self, uri: str, params: dict[str, Any] | None = None) -> str:
"""Handle a GET request to Spotify."""
return await self._request(METH_GET, uri, params=params)
async def _post(
self,
uri: str,
data: dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
) -> str:
"""Handle a POST request to Spotify."""
return await self._request(METH_POST, uri, data=data, params=params)
async def _put(
self,
uri: str,
data: dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
) -> str:
"""Handle a PUT request to Spotify."""
return await self._request(METH_PUT, uri, data=data, params=params)
async def _delete(
self,
uri: str,
data: dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
) -> str:
"""Handle a DELETE request to Spotify."""
return await self._request(METH_DELETE, uri, data=data, params=params)
@catch_json_decode_error
async def get_album(self, album_id: str) -> Album:
"""Get album."""
identifier = get_identifier(album_id)
response = await self._get(f"v1/albums/{identifier}")
return Album.from_json(response)
@catch_json_decode_error
async def get_albums(self, album_ids: list[str]) -> list[Album]:
"""Get albums."""
if not album_ids:
return []
if len(album_ids) > 20:
msg = "Maximum of 20 albums can be requested at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in album_ids])
}
response = await self._get("v1/albums", params=params)
return AlbumsResponse.from_json(response).albums
@catch_json_decode_error
async def get_album_tracks(self, album_id: str) -> list[SimplifiedTrack]:
"""Get album tracks."""
identifier = get_identifier(album_id)
params: dict[str, Any] = {"limit": 48}
response = await self._get(f"v1/albums/{identifier}/tracks", params=params)
return AlbumTracksResponse.from_json(response).items
@catch_json_decode_error
async def get_saved_albums(self) -> list[SavedAlbum]:
"""Get saved albums."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/albums", params=params)
return SavedAlbumResponse.from_json(response).items
async def save_albums(self, album_ids: list[str]) -> None:
"""Save albums."""
if not album_ids:
return
if len(album_ids) > 50:
msg = "Maximum of 50 albums can be saved at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in album_ids])
}
await self._put("v1/me/albums", params=params)
async def remove_saved_albums(self, album_ids: list[str]) -> None:
"""Remove saved albums."""
if not album_ids:
return
if len(album_ids) > 50:
msg = "Maximum of 50 albums can be removed at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in album_ids])
}
await self._delete("v1/me/albums", params=params)
@catch_json_decode_error
async def are_albums_saved(self, album_ids: list[str]) -> dict[str, bool]:
"""Check if albums are saved."""
if not album_ids:
return {}
if len(album_ids) > 20:
msg = "Maximum of 20 albums can be checked at once"
raise ValueError(msg)
identifiers = [get_identifier(i) for i in album_ids]
params: dict[str, Any] = {"ids": ",".join(identifiers)}
response = await self._get("v1/me/albums/contains", params=params)
body: list[bool] = orjson.loads(response) # pylint: disable=no-member
return dict(zip(identifiers, body))
async def is_album_saved(self, album_id: str) -> bool:
"""Check if album is saved."""
identifier = get_identifier(album_id)
return (await self.are_albums_saved([identifier]))[identifier]
@catch_json_decode_error
async def get_new_releases(self) -> list[SimplifiedAlbum]:
"""Get new releases."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/browse/new-releases", params=params)
return NewReleasesResponse.from_json(response).albums.items
@catch_json_decode_error
async def get_artist(self, artist_id: str) -> Artist:
"""Get artist."""
identifier = artist_id.split(":")[-1]
response = await self._get(f"v1/artists/{identifier}")
return Artist.from_json(response)
@catch_json_decode_error
async def get_artists(self, artist_ids: list[str]) -> list[Artist]:
"""Get several artists."""
if not artist_ids:
return []
if len(artist_ids) > 50:
msg = "Maximum of 50 artists can be requested at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in artist_ids])
}
response = await self._get("v1/artists", params=params)
return ArtistResponse.from_json(response).artists
@catch_json_decode_error
async def get_artist_albums(self, artist_id: str) -> list[SimplifiedAlbum]:
"""Get artist albums."""
params: dict[str, Any] = {"limit": 48}
identifier = artist_id.split(":")[-1]
response = await self._get(f"v1/artists/{identifier}/albums", params=params)
return NewReleasesResponseInner.from_json(response).items
@catch_json_decode_error
async def get_artist_top_tracks(self, artist_id: str) -> list[Track]:
"""Get artist top tracks."""
identifier = artist_id.split(":")[-1]
response = await self._get(f"v1/artists/{identifier}/top-tracks")
return ArtistTopTracksResponse.from_json(response).tracks
@catch_json_decode_error
async def get_audiobook(self, audiobook_id: str) -> Audiobook:
"""Get audiobook."""
identifier = get_identifier(audiobook_id)
response = await self._get(f"v1/audiobooks/{identifier}")
return Audiobook.from_json(response)
@catch_json_decode_error
async def get_audiobooks(self, audiobook_ids: list[str]) -> list[Audiobook]:
"""Get audiobooks."""
identifiers = [get_identifier(i) for i in audiobook_ids]
params: dict[str, Any] = {"ids": ",".join(identifiers)}
response = await self._get("v1/audiobooks", params=params)
return AudiobooksResponse.from_json(response).audiobooks
@catch_json_decode_error
async def get_audiobook_chapters(
self, audiobook_id: str
) -> list[SimplifiedChapter]:
"""Get audiobook chapters."""
identifier = get_identifier(audiobook_id)
params: dict[str, Any] = {"limit": 50}
response = await self._get(
f"v1/audiobooks/{identifier}/chapters", params=params
)
return AudiobookChapterResponse.from_json(response).items
@catch_json_decode_error
async def get_saved_audiobooks(self) -> list[SimplifiedAudiobook]:
"""Get saved audiobooks."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/audiobooks", params=params)
return SavedAudiobookResponse.from_json(response).items
async def save_audiobooks(self, audiobook_ids: list[str]) -> None:
"""Save audiobooks."""
if not audiobook_ids:
return
if len(audiobook_ids) > 50:
msg = "Maximum of 50 audiobooks can be saved at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in audiobook_ids])
}
await self._put("v1/me/audiobooks", params=params)
async def remove_saved_audiobooks(self, audiobook_ids: list[str]) -> None:
"""Remove saved audiobooks."""
if not audiobook_ids:
return
if len(audiobook_ids) > 50:
msg = "Maximum of 50 audiobooks can be removed at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in audiobook_ids])
}
await self._delete("v1/me/audiobooks", params=params)
@catch_json_decode_error
async def are_audiobooks_saved(self, audiobook_ids: list[str]) -> dict[str, bool]:
"""Check if audiobooks are saved."""
if not audiobook_ids:
return {}
if len(audiobook_ids) > 50:
msg = "Maximum of 50 audiobooks can be checked at once"
raise ValueError(msg)
identifiers = [get_identifier(i) for i in audiobook_ids]
params: dict[str, Any] = {"ids": ",".join(identifiers)}
response = await self._get("v1/me/audiobooks/contains", params=params)
body: list[bool] = orjson.loads(response) # pylint: disable=no-member
return dict(zip(identifiers, body))
@catch_json_decode_error
async def get_categories(self) -> list[Category]:
"""Get list of categories."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/browse/categories", params=params)
return CategoriesResponse.from_json(response).categories.items
@catch_json_decode_error
async def get_category(self, category_id: str) -> Category:
"""Get category."""
response = await self._get(f"v1/browse/categories/{category_id}")
return Category.from_json(response)
@catch_json_decode_error
async def get_chapter(self, chapter_id: str) -> Chapter:
"""Get chapter."""
identifier = chapter_id.split(":")[-1]
response = await self._get(f"v1/chapters/{identifier}")
return Chapter.from_json(response)
@catch_json_decode_error
async def get_chapters(self, chapter_ids: list[str]) -> list[Chapter]:
"""Get chapters."""
if not chapter_ids:
return []
if len(chapter_ids) > 50:
msg = "Maximum of 50 chapters can be requested at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in chapter_ids])
}
response = await self._get("v1/chapters", params=params)
return ChaptersResponse.from_json(response).chapters
@catch_json_decode_error
async def get_episode(self, episode_id: str) -> Episode:
"""Get episode."""
identifier = episode_id.split(":")[-1]
response = await self._get(f"v1/episodes/{identifier}")
return Episode.from_json(response)
@catch_json_decode_error
async def get_episodes(self, episode_ids: list[str]) -> list[Episode]:
"""Get episodes."""
if not episode_ids:
return []
if len(episode_ids) > 50:
msg = "Maximum of 50 episodes can be requested at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in episode_ids])
}
response = await self._get("v1/episodes", params=params)
return EpisodesResponse.from_json(response).episodes
@catch_json_decode_error
async def get_saved_episodes(self) -> list[SavedEpisode]:
"""Get saved episodes."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/episodes", params=params)
return SavedEpisodeResponse.from_json(response).items
async def save_episodes(self, episode_ids: list[str]) -> None:
"""Save episodes."""
if not episode_ids:
return
if len(episode_ids) > 50:
msg = "Maximum of 50 episodes can be saved at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in episode_ids])
}
await self._put("v1/me/episodes", params=params)
async def remove_saved_episodes(self, episode_ids: list[str]) -> None:
"""Remove saved episodes."""
if not episode_ids:
return
if len(episode_ids) > 50:
msg = "Maximum of 50 episodes can be removed at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in episode_ids])
}
await self._delete("v1/me/episodes", params=params)
@catch_json_decode_error
async def are_episodes_saved(self, episode_ids: list[str]) -> dict[str, bool]:
"""Check if episodes are saved."""
if not episode_ids:
return {}
if len(episode_ids) > 50:
msg = "Maximum of 50 episodes can be checked at once"
raise ValueError(msg)
identifiers = [get_identifier(i) for i in episode_ids]
params: dict[str, Any] = {"ids": ",".join(identifiers)}
response = await self._get("v1/me/episodes/contains", params=params)
body: list[bool] = orjson.loads(response) # pylint: disable=no-member
return dict(zip(identifiers, body))
async def is_episode_saved(self, episode_id: str) -> bool:
"""Check if episode is saved."""
identifier = get_identifier(episode_id)
return (await self.are_episodes_saved([identifier]))[identifier]
async def is_added_to_library(self, uri: str) -> bool:
"""Check if item is added to library."""
if uri.startswith("spotify:track:"):
return await self.is_track_saved(uri)
if uri.startswith("spotify:episode:"):
return await self.is_episode_saved(uri)
if uri.startswith("spotify:show:"):
return await self.is_show_saved(uri)
if uri.startswith("spotify:album:"):
return await self.is_album_saved(uri)
msg = "Invalid URI format"
raise ValueError(msg)
@catch_json_decode_error
async def get_playback(self) -> PlaybackState | None:
"""Get playback state."""
response = await self._get(
"v1/me/player", params={"additional_types": "track,episode"}
)
if response == "":
return None
return PlaybackState.from_json(response)
async def transfer_playback(self, device_id: str) -> None:
"""Transfer playback."""
await self._put("v1/me/player", {"device_ids": [device_id]})
@catch_json_decode_error
async def get_devices(self) -> list[Device]:
"""Get devices."""
response = await self._get("v1/me/player/devices")
return Devices.from_json(response).devices
@catch_json_decode_error
async def get_current_playing(self) -> CurrentPlaying | None:
"""Get playback state."""
response = await self._get("v1/me/player/currently-playing")
if response == "":
return None
return CurrentPlaying.from_json(response)
async def start_playback(
self,
*,
device_id: str | None = None,
context_uri: str | None = None,
uris: list[str] | None = None,
position_offset: int | None = None,
uri_offset: str | None = None,
position: int | None = 0,
) -> None:
"""Start playback."""
payload: dict[str, Any] = {
"position_ms": position,
}
if context_uri:
payload["context_uri"] = context_uri
if uris:
payload["uris"] = uris
if position_offset:
payload["offset"] = {"position": position_offset}
if uri_offset:
payload["offset"] = {"uri": uri_offset}
params = {}
if device_id:
params["device_id"] = device_id
await self._put("v1/me/player/play", payload, params=params)
async def pause_playback(self, device_id: str | None = None) -> None:
"""Pause playback."""
params = {}
if device_id:
params["device_id"] = device_id
await self._put("v1/me/player/pause", params=params)
async def next_track(self, device_id: str | None = None) -> None:
"""Next track."""
params: dict[str, str] = {}
if device_id:
params["device_id"] = device_id
await self._post("v1/me/player/next", params=params)
async def previous_track(self, device_id: str | None = None) -> None:
"""Previous track."""
params: dict[str, str] = {}
if device_id:
params["device_id"] = device_id
await self._post("v1/me/player/previous", params=params)
async def seek_track(self, position: int, device_id: str | None = None) -> None:
"""Seek track."""
params: dict[str, Any] = {"position_ms": position}
if device_id:
params["device_id"] = device_id
await self._put("v1/me/player/seek", params=params)
async def set_repeat(self, state: RepeatMode, device_id: str | None = None) -> None:
"""Set repeat."""
params: dict[str, str] = {"state": state}
if device_id:
params["device_id"] = device_id
await self._put("v1/me/player/repeat", params=params)
async def set_volume(self, volume: int, device_id: str | None = None) -> None:
"""Set volume."""
params: dict[str, Any] = {"volume_percent": volume}
if device_id:
params["device_id"] = device_id
await self._put("v1/me/player/volume", params=params)
async def set_shuffle(self, *, state: bool, device_id: str | None = None) -> None:
"""Set shuffle."""
params: dict[str, Any] = {"state": str(state).lower()}
if device_id:
params["device_id"] = device_id
await self._put("v1/me/player/shuffle", params=params)
@catch_json_decode_error
async def get_recently_played_tracks(self) -> list[PlayedTrack]:
"""Get recently played tracks."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/player/recently-played", params=params)
return PlayedTrackResponse.from_json(response).items
# Get queue
async def add_to_queue(self, uri: str, device_id: str | None = None) -> None:
"""Add to queue."""
data: dict[str, str] = {"uri": uri}
if device_id:
data["device_id"] = device_id
await self._post("v1/me/player/queue", data=data)
@catch_json_decode_error
async def get_playlist(self, playlist_id: str) -> Playlist:
"""Get playlist."""
identifier = playlist_id.split(":")[-1]
response = await self._get(
f"v1/playlists/{identifier}", params={"additional_types": "track,episode"}
)
return Playlist.from_json(response)
async def update_playlist_details(
self,
playlist_id: str,
*,
name: str | None = None,
description: str | None = None,
public: bool | None = None,
collaborative: bool | None = None,
) -> None:
"""Update playlist details."""
identifier = get_identifier(playlist_id)
data: dict[str, Any] = {}
if name:
data["name"] = name
if description:
data["description"] = description
if public is not None:
data["public"] = public
if collaborative is not None:
data["collaborative"] = collaborative
await self._put(f"v1/playlists/{identifier}", data=data)
@catch_json_decode_error
async def get_playlist_items(self, playlist_id: str) -> list[PlaylistTrack]:
"""Get playlist tracks."""
identifier = get_identifier(playlist_id)
params: dict[str, Any] = {"limit": 48}
response = await self._get(f"v1/playlists/{identifier}/tracks", params=params)
return PlaylistTrackResponse.from_json(response).items
@catch_json_decode_error
async def update_playlist_items(
self,
playlist_id: str,
uris: list[str],
*,
range_start: int | None = None,
insert_before: int | None = None,
range_length: int | None = None,
snapshot_id: str | None = None,
) -> str:
"""Update playlist tracks."""
identifier = get_identifier(playlist_id)
data: dict[str, Any] = {"uris": uris}
if range_start is not None:
data["range_start"] = range_start
if insert_before is not None:
data["insert_before"] = insert_before
if range_length is not None:
data["range_length"] = range_length
if snapshot_id:
data["snapshot_id"] = snapshot_id
response = await self._put(f"v1/playlists/{identifier}/tracks", data=data)
return ModifyPlaylistResponse.from_json(response).snapshot_id
@catch_json_decode_error
async def add_playlist_items(
self,
playlist_id: str,
uris: list[str],
position: int | None = None,
) -> str:
"""Add playlist tracks."""
if len(uris) > 100:
msg = "Maximum of 100 tracks can be added at once"
raise ValueError(msg)
identifier = get_identifier(playlist_id)
data: dict[str, Any] = {"uris": uris}
if position is not None:
data["position"] = position
response = await self._post(f"v1/playlists/{identifier}/tracks", data=data)
return ModifyPlaylistResponse.from_json(response).snapshot_id
@catch_json_decode_error
async def remove_playlist_items(
self,
playlist_id: str,
uris: list[str],
snapshot_id: str | None = None,
) -> str:
"""Remove playlist tracks."""
if len(uris) > 100:
msg = "Maximum of 100 tracks can be removed at once"
raise ValueError(msg)
identifier = get_identifier(playlist_id)
data: dict[str, Any] = {"tracks": [{"uri": uri} for uri in uris]}
if snapshot_id:
data["snapshot_id"] = snapshot_id
response = await self._delete(f"v1/playlists/{identifier}/tracks", data=data)
return ModifyPlaylistResponse.from_json(response).snapshot_id
@catch_json_decode_error
async def get_playlists_for_current_user(self) -> list[BasePlaylist]:
"""Get playlists."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/playlists", params=params)
return PlaylistResponse.from_json(response).items
@catch_json_decode_error
async def get_playlists_for_user(self, user_id: str) -> list[BasePlaylist]:
"""Get playlists."""
identifier = get_identifier(user_id)
params: dict[str, Any] = {"limit": 48}
response = await self._get(f"v1/user/{identifier}/playlists", params=params)
return PlaylistResponse.from_json(response).items
@catch_json_decode_error
async def create_playlist(
self,
user_id: str,
name: str,
*,
description: str | None = None,
public: bool | None = None,
collaborative: bool | None = None,
) -> Playlist:
"""Create playlist."""
identifier = get_identifier(user_id)
data: dict[str, Any] = {"name": name}
if description:
data["description"] = description
if public is not None:
data["public"] = public
if collaborative is not None:
data["collaborative"] = collaborative
response = await self._post(f"v1/users/{identifier}/playlists", data=data)
return Playlist.from_json(response)
@catch_json_decode_error
async def get_featured_playlists(self) -> list[BasePlaylist]:
"""Get featured playlists."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/browse/featured-playlists", params=params)
return FeaturedPlaylistResponse.from_json(response).playlists.items
@catch_json_decode_error
async def get_category_playlists(self, category_id: str) -> list[BasePlaylist]:
"""Get category playlists."""
params: dict[str, Any] = {"limit": 48}
response = await self._get(
f"v1/browse/categories/{category_id}/playlists",
params=params,
)
return CategoryPlaylistResponse.from_json(response).playlists.items
@catch_json_decode_error
async def get_playlist_cover_image(self, playlist_id: str) -> list[Image]:
"""Get playlist cover image."""
identifier = get_identifier(playlist_id)
response = await self._get(f"v1/playlists/{identifier}/images")
return JSONDecoder(list[Image]).decode(response)
# Upload a custom playlist cover image
@catch_json_decode_error
async def search(
self, query: str, types: list[SearchType], *, limit: int = 48
) -> SearchResult:
"""Search."""
params: dict[str, Any] = {"q": query, "limit": limit, "type": ",".join(types)}
response = await self._get("v1/search", params=params)
return SearchResult.from_json(response)
@catch_json_decode_error
async def get_show(self, show_id: str) -> Show:
"""Get show."""
identifier = show_id.split(":")[-1]
response = await self._get(f"v1/shows/{identifier}")
return Show.from_json(response)
# Get several shows
@catch_json_decode_error
async def get_show_episodes(self, show_id: str) -> list[SimplifiedEpisode]:
"""Get show episodes."""
identifier = show_id.split(":")[-1]
params: dict[str, Any] = {"limit": 48}
response = await self._get(f"v1/shows/{identifier}/episodes", params=params)
return ShowEpisodesResponse.from_json(response).items
@catch_json_decode_error
async def get_saved_shows(self) -> list[SavedShow]:
"""Get saved shows."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/shows", params=params)
return SavedShowResponse.from_json(response).items
@catch_json_decode_error
async def save_shows(self, show_ids: list[str]) -> None:
"""Save shows."""
if not show_ids:
return
if len(show_ids) > 50:
msg = "Maximum of 50 shows can be saved at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in show_ids])
}
await self._put("v1/me/shows", params=params)
async def remove_saved_shows(self, show_ids: list[str]) -> None:
"""Remove saved shows."""
if not show_ids:
return
if len(show_ids) > 50:
msg = "Maximum of 50 shows can be removed at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in show_ids])
}
await self._delete("v1/me/shows", params=params)
@catch_json_decode_error
async def are_shows_saved(self, show_ids: list[str]) -> dict[str, bool]:
"""Check if shows are saved."""
if not show_ids:
return {}
if len(show_ids) > 50:
msg = "Maximum of 50 shows can be checked at once"
raise ValueError(msg)
identifiers = [get_identifier(i) for i in show_ids]
params: dict[str, Any] = {"ids": ",".join(identifiers)}
response = await self._get("v1/me/shows/contains", params=params)
body: list[bool] = orjson.loads(response) # pylint: disable=no-member
return dict(zip(identifiers, body))
async def is_show_saved(self, show_id: str) -> bool:
"""Check if show is saved."""
identifier = get_identifier(show_id)
return (await self.are_shows_saved([identifier]))[identifier]
# Get a track
# Get several tracks
@catch_json_decode_error
async def get_saved_tracks(self) -> list[SavedTrack]:
"""Get saved tracks."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/tracks", params=params)
return SavedTrackResponse.from_json(response).items
async def save_tracks(self, track_ids: list[str]) -> None:
"""Save tracks."""
if not track_ids:
return
if len(track_ids) > 50:
msg = "Maximum of 50 tracks can be saved at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in track_ids])
}
await self._put("v1/me/tracks", params=params)
async def remove_saved_tracks(self, track_ids: list[str]) -> None:
"""Remove saved tracks."""
if not track_ids:
return
if len(track_ids) > 50:
msg = "Maximum of 50 tracks can be removed at once"
raise ValueError(msg)
params: dict[str, Any] = {
"ids": ",".join([get_identifier(i) for i in track_ids])
}
await self._delete("v1/me/tracks", params=params)
@catch_json_decode_error
async def are_tracks_saved(self, track_ids: list[str]) -> dict[str, bool]:
"""Check if tracks are saved."""
if not track_ids:
return {}
if len(track_ids) > 50:
msg = "Maximum of 50 tracks can be checked at once"
raise ValueError(msg)
identifiers = [get_identifier(i) for i in track_ids]
params: dict[str, Any] = {"ids": ",".join(identifiers)}
response = await self._get("v1/me/tracks/contains", params=params)
body: list[bool] = orjson.loads(response) # pylint: disable=no-member
return dict(zip(identifiers, body))
async def is_track_saved(self, track_id: str) -> bool:
"""Check if track is saved."""
identifier = get_identifier(track_id)
return (await self.are_tracks_saved([identifier]))[identifier]
@catch_json_decode_error
async def get_audio_features(self, track_id: str) -> AudioFeatures:
"""Get audio features."""
identifier = get_identifier(track_id)
response = await self._get(f"v1/audio-features/{identifier}")
return AudioFeatures.from_json(response)
@catch_json_decode_error
async def get_current_user(self) -> UserProfile:
"""Get current user."""
response = await self._get("v1/me")
return UserProfile.from_json(response)
@catch_json_decode_error
async def get_top_artists(self) -> list[Artist]:
"""Get top artists."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/top/artists", params=params)
return TopArtistsResponse.from_json(response).items
@catch_json_decode_error
async def get_top_tracks(self) -> list[Track]:
"""Get top tracks."""
params: dict[str, Any] = {"limit": 48}
response = await self._get("v1/me/top/tracks", params=params)
return TopTracksResponse.from_json(response).items
@catch_json_decode_error
async def get_user(self, user_id: str) -> BaseUserProfile:
"""Get user."""
response = await self._get(f"v1/users/{user_id}")
return BaseUserProfile.from_json(response)
async def follow_playlist(self, playlist_id: str) -> None:
"""Follow a playlist."""
identifier = get_identifier(playlist_id)
await self._put(f"v1/playlists/{identifier}/followers")
async def unfollow_playlist(self, playlist_id: str) -> None:
"""Unfollow a playlist."""
identifier = get_identifier(playlist_id)
await self._delete(f"v1/playlists/{identifier}/followers")
@catch_json_decode_error
async def get_followed_artists(self) -> list[Artist]:
"""Get followed artists."""
params: dict[str, Any] = {"limit": 48, "type": "artist"}
response = await self._get("v1/me/following", params=params)
return FollowedArtistResponse.from_json(response).artists.items
async def follow_account(self, follow_type: FollowType, ids: list[str]) -> None:
"""Follow an artist or user."""
if not ids:
return
if len(ids) > 50:
msg = "Maximum of 50 accounts can be followed at once"
raise ValueError(msg)
params: dict[str, Any] = {
"type": follow_type,
"ids": ",".join([get_identifier(i) for i in ids]),
}
await self._put("v1/me/following", params=params)
async def unfollow_account(self, follow_type: FollowType, ids: list[str]) -> None:
"""Unfollow an artist or user."""
if not ids:
return
if len(ids) > 50:
msg = "Maximum of 50 accounts can be unfollowed at once"