Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for likes url #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sclib/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SoundcloudAPI:
SEARCH_URL = "https://api-v2.soundcloud.com/search?q={query}&client_id={client_id}&limit={limit}&offset={offset}"
STREAM_URL = "https://api.soundcloud.com/i1/tracks/{track_id}/streams?client_id={client_id}"
TRACKS_URL = "https://api-v2.soundcloud.com/tracks?ids={track_ids}&client_id={client_id}"
LIKES_URL = "https://api-v2.soundcloud.com/users/{user_id}/likes?limit={limit}&offset={offset}&client_id={client_id}"
PROGRESSIVE_URL = "https://api-v2.soundcloud.com/media/soundcloud:tracks:723290971/53dc4e74-0414-4ab8-8741-a07ac56c787f/stream/progressive?client_id={client_id}"

TRACK_API_MAX_REQUEST_SIZE = 50
Expand Down Expand Up @@ -87,6 +88,8 @@ def resolve(self, url):
playlist = Playlist(obj=obj, client=self)
playlist.clean_attributes()
return playlist
if obj['kind'] == 'user':
return User(obj=obj, client=self)
return None

def _format_get_tracks_urls(self, track_ids):
Expand Down Expand Up @@ -117,6 +120,26 @@ def get_tracks(self, *track_ids):
tracks = sorted(tracks, key=lambda x: track_ids.index(x['id']))
return tracks

def get_likes(self, user_id, limit=50, offset=0):
""" Get the likes of a user """
if not self.client_id:
self.get_credentials()
url = self.LIKES_URL.format(user_id=user_id, limit=limit, offset=offset, client_id=self.client_id)
return get_obj_from(url)

def get_all_likes(self, user_id):
""" Get all likes of a user, handling pagination """
likes = []
offset = 0
while True:
response = self.get_likes(user_id, limit=50, offset=offset)
if not response:
break
likes.extend(response['collection'])
if not response['next_href']:
break
offset += 50
return likes

class Track:
""" Track object """
Expand Down