Skip to content

Commit

Permalink
feat: ability to manage tracks from the user's library
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed Oct 9, 2021
1 parent 10f2967 commit 59b0f57
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 0 deletions.
28 changes: 28 additions & 0 deletions deezer/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,34 @@ def remove_user_artist(self, artist_id: int) -> bool:
"""
return self.request("DELETE", "user/me/artists", artist_id=artist_id)

def get_user_tracks(self, user_id: Optional[int] = None) -> List[Track]:
"""
Get the favourites tracks for the given user_id if provided or current user if not.
:param user_id: the user ID to get favourites tracks.
:return: a list of :class:`~deezer.resources.Track` instances.
"""
user_id_str = str(user_id) if user_id else "me"
return self.request("GET", f"user/{user_id_str}/tracks")

def add_user_track(self, track_id: int) -> bool:
"""
Add a track to the user's library
:param track_id: the ID of the track to add.
:return: boolean whether the operation succeeded.
"""
return self.request("POST", "user/me/tracks", track_id=track_id)

def remove_user_track(self, track_id: int) -> bool:
"""
Remove a track from the user's library
:param track_id: the ID of the track to remove.
:return: boolean whether the operation succeeded.
"""
return self.request("DELETE", "user/me/tracks", track_id=track_id)

def search(self, query, relation=None, index=0, limit=25, **kwargs):
"""
Search track, album, artist or user
Expand Down
54 changes: 54 additions & 0 deletions tests/cassettes/TestClient.test_add_user_track.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- identity
Connection:
- keep-alive
Content-Length:
- '0'
User-Agent:
- python-requests/2.26.0
method: POST
uri: https://api.deezer.com/user/me/tracks?track_id=1374789602&access_token=dummy
response:
body:
string: 'true'
headers:
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Headers:
- X-Requested-With, Content-Type, Authorization, Origin, Accept, Accept-Encoding
Access-Control-Allow-Methods:
- POST, GET, OPTIONS, DELETE, PUT
Access-Control-Expose-Headers:
- Location
Access-Control-Max-Age:
- '86400'
Cache-Control:
- no-store, no-cache, must-revalidate
Connection:
- keep-alive
Content-Length:
- '4'
Content-Type:
- application/json; charset=utf-8
P3P:
- policyref="/w3c/p3p.xml" CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"
Pragma:
- no-cache
Server:
- Apache
X-Content-Type-Options:
- nosniff
X-Host:
- blm-web-160
x-org:
- FR
status:
code: 200
message: OK
version: 1
61 changes: 61 additions & 0 deletions tests/cassettes/TestClient.test_get_user_tracks[args0].yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- identity
Connection:
- keep-alive
User-Agent:
- python-requests/2.26.0
method: GET
uri: https://api.deezer.com/user/me/tracks?access_token=dummy
response:
body:
string: '{"data":[{"id":1374789602,"readable":true,"title":"Flyover","link":"https:\/\/www.deezer.com\/track\/1374789602","duration":258,"rank":311062,"explicit_lyrics":false,"explicit_content_lyrics":0,"explicit_content_cover":2,"md5_image":"0a52a069f4596c26c6f5b1527c8814c1","time_add":1633802081,"album":{"id":230721992,"title":"Tank
(Remastered)","cover":"https:\/\/api.deezer.com\/album\/230721992\/image","cover_small":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/56x56-000000-80-0-0.jpg","cover_medium":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/250x250-000000-80-0-0.jpg","cover_big":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/500x500-000000-80-0-0.jpg","cover_xl":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/1000x1000-000000-80-0-0.jpg","md5_image":"0a52a069f4596c26c6f5b1527c8814c1","tracklist":"https:\/\/api.deezer.com\/album\/230721992\/tracks","type":"album"},"artist":{"id":1422,"name":"Asian
Dub Foundation","picture":"https:\/\/api.deezer.com\/artist\/1422\/image","picture_small":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/56x56-000000-80-0-0.jpg","picture_medium":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/250x250-000000-80-0-0.jpg","picture_big":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/500x500-000000-80-0-0.jpg","picture_xl":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/1000x1000-000000-80-0-0.jpg","tracklist":"https:\/\/api.deezer.com\/artist\/1422\/top?limit=50","type":"artist"},"type":"track"},{"id":3834127,"readable":true,"title":"Poney
Pt. I","link":"https:\/\/www.deezer.com\/track\/3834127","duration":323,"rank":630344,"explicit_lyrics":false,"explicit_content_lyrics":0,"explicit_content_cover":2,"md5_image":"c35a9579e79da78be284094d5cca571b","time_add":1633802089,"album":{"id":357404,"title":"OK
Cowboy","cover":"https:\/\/api.deezer.com\/album\/357404\/image","cover_small":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/56x56-000000-80-0-0.jpg","cover_medium":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/250x250-000000-80-0-0.jpg","cover_big":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/500x500-000000-80-0-0.jpg","cover_xl":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/1000x1000-000000-80-0-0.jpg","md5_image":"c35a9579e79da78be284094d5cca571b","tracklist":"https:\/\/api.deezer.com\/album\/357404\/tracks","type":"album"},"artist":{"id":641,"name":"Vitalic","picture":"https:\/\/api.deezer.com\/artist\/641\/image","picture_small":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/56x56-000000-80-0-0.jpg","picture_medium":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/250x250-000000-80-0-0.jpg","picture_big":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/500x500-000000-80-0-0.jpg","picture_xl":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/1000x1000-000000-80-0-0.jpg","tracklist":"https:\/\/api.deezer.com\/artist\/641\/top?limit=50","type":"artist"},"type":"track"},{"id":9919591,"readable":true,"title":"Young
Blood","link":"https:\/\/www.deezer.com\/track\/9919591","duration":246,"rank":557693,"explicit_lyrics":false,"explicit_content_lyrics":0,"explicit_content_cover":2,"md5_image":"8dbe8f855de33502b4e3f1c132291c24","time_add":1633802113,"album":{"id":908962,"title":"Passive
Me, Aggressive You","cover":"https:\/\/api.deezer.com\/album\/908962\/image","cover_small":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/56x56-000000-80-0-0.jpg","cover_medium":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/250x250-000000-80-0-0.jpg","cover_big":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/500x500-000000-80-0-0.jpg","cover_xl":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/1000x1000-000000-80-0-0.jpg","md5_image":"8dbe8f855de33502b4e3f1c132291c24","tracklist":"https:\/\/api.deezer.com\/album\/908962\/tracks","type":"album"},"artist":{"id":812093,"name":"The
Naked And Famous","picture":"https:\/\/api.deezer.com\/artist\/812093\/image","picture_small":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/56x56-000000-80-0-0.jpg","picture_medium":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/250x250-000000-80-0-0.jpg","picture_big":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/500x500-000000-80-0-0.jpg","picture_xl":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/1000x1000-000000-80-0-0.jpg","tracklist":"https:\/\/api.deezer.com\/artist\/812093\/top?limit=50","type":"artist"},"type":"track"}],"checksum":"2adba03e714364afc0c5048cc41c2db6","total":3}'
headers:
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Headers:
- X-Requested-With, Content-Type, Authorization, Origin, Accept, Accept-Encoding
Access-Control-Allow-Methods:
- POST, GET, OPTIONS, DELETE, PUT
Access-Control-Expose-Headers:
- Location
Access-Control-Max-Age:
- '86400'
Cache-Control:
- no-store, no-cache, must-revalidate
Connection:
- keep-alive
Content-Length:
- '5210'
Content-Type:
- application/json; charset=utf-8
P3P:
- policyref="/w3c/p3p.xml" CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"
Pragma:
- no-cache
Server:
- Apache
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Host:
- blm-web-77
x-org:
- FR
status:
code: 200
message: OK
version: 1
61 changes: 61 additions & 0 deletions tests/cassettes/TestClient.test_get_user_tracks[args1].yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- identity
Connection:
- keep-alive
User-Agent:
- python-requests/2.26.0
method: GET
uri: https://api.deezer.com/user/359622/tracks?access_token=dummy
response:
body:
string: '{"data":[{"id":1374789602,"readable":true,"title":"Flyover","link":"https:\/\/www.deezer.com\/track\/1374789602","duration":258,"rank":311062,"explicit_lyrics":false,"explicit_content_lyrics":0,"explicit_content_cover":2,"md5_image":"0a52a069f4596c26c6f5b1527c8814c1","time_add":1633802081,"album":{"id":230721992,"title":"Tank
(Remastered)","cover":"https:\/\/api.deezer.com\/album\/230721992\/image","cover_small":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/56x56-000000-80-0-0.jpg","cover_medium":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/250x250-000000-80-0-0.jpg","cover_big":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/500x500-000000-80-0-0.jpg","cover_xl":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/0a52a069f4596c26c6f5b1527c8814c1\/1000x1000-000000-80-0-0.jpg","md5_image":"0a52a069f4596c26c6f5b1527c8814c1","tracklist":"https:\/\/api.deezer.com\/album\/230721992\/tracks","type":"album"},"artist":{"id":1422,"name":"Asian
Dub Foundation","picture":"https:\/\/api.deezer.com\/artist\/1422\/image","picture_small":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/56x56-000000-80-0-0.jpg","picture_medium":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/250x250-000000-80-0-0.jpg","picture_big":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/500x500-000000-80-0-0.jpg","picture_xl":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/8c4fda02331e9e658877b28b0b7a3115\/1000x1000-000000-80-0-0.jpg","tracklist":"https:\/\/api.deezer.com\/artist\/1422\/top?limit=50","type":"artist"},"type":"track"},{"id":3834127,"readable":true,"title":"Poney
Pt. I","link":"https:\/\/www.deezer.com\/track\/3834127","duration":323,"rank":630344,"explicit_lyrics":false,"explicit_content_lyrics":0,"explicit_content_cover":2,"md5_image":"c35a9579e79da78be284094d5cca571b","time_add":1633802089,"album":{"id":357404,"title":"OK
Cowboy","cover":"https:\/\/api.deezer.com\/album\/357404\/image","cover_small":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/56x56-000000-80-0-0.jpg","cover_medium":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/250x250-000000-80-0-0.jpg","cover_big":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/500x500-000000-80-0-0.jpg","cover_xl":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/c35a9579e79da78be284094d5cca571b\/1000x1000-000000-80-0-0.jpg","md5_image":"c35a9579e79da78be284094d5cca571b","tracklist":"https:\/\/api.deezer.com\/album\/357404\/tracks","type":"album"},"artist":{"id":641,"name":"Vitalic","picture":"https:\/\/api.deezer.com\/artist\/641\/image","picture_small":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/56x56-000000-80-0-0.jpg","picture_medium":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/250x250-000000-80-0-0.jpg","picture_big":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/500x500-000000-80-0-0.jpg","picture_xl":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/5e970ce2f5c52fe2b036f088eb4a7bef\/1000x1000-000000-80-0-0.jpg","tracklist":"https:\/\/api.deezer.com\/artist\/641\/top?limit=50","type":"artist"},"type":"track"},{"id":9919591,"readable":true,"title":"Young
Blood","link":"https:\/\/www.deezer.com\/track\/9919591","duration":246,"rank":557693,"explicit_lyrics":false,"explicit_content_lyrics":0,"explicit_content_cover":2,"md5_image":"8dbe8f855de33502b4e3f1c132291c24","time_add":1633802113,"album":{"id":908962,"title":"Passive
Me, Aggressive You","cover":"https:\/\/api.deezer.com\/album\/908962\/image","cover_small":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/56x56-000000-80-0-0.jpg","cover_medium":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/250x250-000000-80-0-0.jpg","cover_big":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/500x500-000000-80-0-0.jpg","cover_xl":"https:\/\/cdns-images.dzcdn.net\/images\/cover\/8dbe8f855de33502b4e3f1c132291c24\/1000x1000-000000-80-0-0.jpg","md5_image":"8dbe8f855de33502b4e3f1c132291c24","tracklist":"https:\/\/api.deezer.com\/album\/908962\/tracks","type":"album"},"artist":{"id":812093,"name":"The
Naked And Famous","picture":"https:\/\/api.deezer.com\/artist\/812093\/image","picture_small":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/56x56-000000-80-0-0.jpg","picture_medium":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/250x250-000000-80-0-0.jpg","picture_big":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/500x500-000000-80-0-0.jpg","picture_xl":"https:\/\/cdns-images.dzcdn.net\/images\/artist\/43610e20900bd86247b881a387456b77\/1000x1000-000000-80-0-0.jpg","tracklist":"https:\/\/api.deezer.com\/artist\/812093\/top?limit=50","type":"artist"},"type":"track"}],"checksum":"2adba03e714364afc0c5048cc41c2db6","total":3}'
headers:
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Headers:
- X-Requested-With, Content-Type, Authorization, Origin, Accept, Accept-Encoding
Access-Control-Allow-Methods:
- POST, GET, OPTIONS, DELETE, PUT
Access-Control-Expose-Headers:
- Location
Access-Control-Max-Age:
- '86400'
Cache-Control:
- no-store, no-cache, must-revalidate
Connection:
- keep-alive
Content-Length:
- '5210'
Content-Type:
- application/json; charset=utf-8
P3P:
- policyref="/w3c/p3p.xml" CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"
Pragma:
- no-cache
Server:
- Apache
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Host:
- blm-web-156
x-org:
- FR
status:
code: 200
message: OK
version: 1
54 changes: 54 additions & 0 deletions tests/cassettes/TestClient.test_remove_user_track.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- identity
Connection:
- keep-alive
Content-Length:
- '0'
User-Agent:
- python-requests/2.26.0
method: DELETE
uri: https://api.deezer.com/user/me/tracks?track_id=1374789602&access_token=dummy
response:
body:
string: 'true'
headers:
Access-Control-Allow-Credentials:
- 'true'
Access-Control-Allow-Headers:
- X-Requested-With, Content-Type, Authorization, Origin, Accept, Accept-Encoding
Access-Control-Allow-Methods:
- POST, GET, OPTIONS, DELETE, PUT
Access-Control-Expose-Headers:
- Location
Access-Control-Max-Age:
- '86400'
Cache-Control:
- no-store, no-cache, must-revalidate
Connection:
- keep-alive
Content-Length:
- '4'
Content-Type:
- application/json; charset=utf-8
P3P:
- policyref="/w3c/p3p.xml" CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"
Pragma:
- no-cache
Server:
- Apache
X-Content-Type-Options:
- nosniff
X-Host:
- blm-web-76
x-org:
- FR
status:
code: 200
message: OK
version: 1
25 changes: 25 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,31 @@ def test_remove_user_artist(self, client_token):
result = client_token.remove_user_artist(243)
assert result is True

@pytest.mark.parametrize(
"args",
[
(),
(359622,),
],
)
def test_get_user_tracks(self, client_token, args):
user_tracks = client_token.get_user_tracks(*args)
assert len(user_tracks) == 3
assert all(isinstance(a, deezer.resources.Track) for a in user_tracks)
assert [t.title for t in user_tracks] == [
"Flyover",
"Poney Pt. I",
"Young Blood",
]

def test_add_user_track(self, client_token):
result = client_token.add_user_track(1374789602)
assert result is True

def test_remove_user_track(self, client_token):
result = client_token.remove_user_track(1374789602)
assert result is True

def test_options_1(self, client):
"""Test a query with extra arguments"""
result = client.search("Billy Jean", limit=2)
Expand Down

0 comments on commit 59b0f57

Please sign in to comment.