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

LB-1700: Get all tracks for apple music playlist #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion troi/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ class RecordingsFromMusicServiceElement(Element):
def __init__(self, token=None, playlist_id=None, music_service=None, apple_user_token=None):
"""
Args:
playlist_id: id of the Spotify playlist to be used for creating the playlist element
playlist_id: id of the playlist to be used for creating the playlist element
token: the Spotify token to fetch the playlist tracks
music_service: the name of the music service to be used for fetching the playlist data
apple_music_token (optional): the user token for Apple Music API
Expand Down
6 changes: 1 addition & 5 deletions troi/tools/apple_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ def get_tracks_from_apple_playlist(developer_token, user_token, playlist_id):
""" Get tracks from the Apple Music playlist.
"""
apple = AppleMusicAPI(developer_token, user_token)
response = apple.get_playlist_tracks(playlist_id)

tracks = response["data"][0]["relationships"]["tracks"]["data"]
name = response["data"][0]["attributes"]["name"]
description = response["data"][0]["attributes"].get("description", {}).get("standard", "")
tracks, name, description = apple.get_playlist_tracks(playlist_id)

mapped_tracks = [
{
Expand Down
32 changes: 30 additions & 2 deletions troi/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,36 @@ def playlist_add_tracks(self, playlist_id, track_ids):

def get_playlist_tracks(self, playlist_id):
url = f"{APPLE_MUSIC_URL}/me/library/playlists/{playlist_id}?include=tracks"
response = self.session.get(url, headers=self.headers)
return response.json()
response = self.session.get(url, headers=self.headers).json()
tracks = response["data"][0]["relationships"]["tracks"]["data"]
total_tracks_count = response["data"][0]["relationships"]["tracks"]["meta"]["total"]
playlist_name = response["data"][0]["attributes"]["name"]
playlist_description = response["data"][0]["attributes"].get(
"description", {}).get("standard", "")

# apple music returns only the first 100 tracks with "/playlists/{playlist_id}?include=tracks"
# and we need to fetch the rest of the tracks using the "playlists/{playlist_id}/tracks" endpoint
if len(tracks) < total_tracks_count:
offset = len(tracks)
# endpoint returns 100 tracks per call max -> run iteratively with an offset until there are no more tracks
while True:
url = f"{APPLE_MUSIC_URL}/me/library/playlists/{playlist_id}/tracks?limit=100&offset={offset}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does any more error checking need to be added here? rate limiting or otherwise?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to ensure that errors that might happen are not silently dropped.

Copy link
Member Author

@MonkeyDo MonkeyDo Dec 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A retry strategy is set up at the class level, not sure if that handles rate limiting though looks like urllib3 Retry class handles rate limiting headers by default
https://github.com/metabrainz/troi-recommendation-playground/blob/6293c4993d40048492e20270269ec546b1b21be9/troi/tools/utils.py#L183C5-L197C16

What should be the strategy for handling other errors?
The API will sometimes return a json body with an error key, so I could try parsing for that and logging the error message (instead of the current caught KeyError) as an improvement.
Anything else that I should look out for?

response = self.session.get(url, headers=self.headers).json()
try:
tracks_data = response["data"]
tracks.extend(tracks_data) if tracks_data else None

if len(tracks_data) == 0 or len(tracks) == total_tracks_count:
break
# set new offset for the next loop
offset = offset + len(tracks_data)
except KeyError as err:
# No data returned from API call, could be an error, do nothing
logger.error(
"Failed to fetch Apple playlist tracks: %s, ignoring error..." % err)
break

return tracks, playlist_name, playlist_description


class SoundCloudException(Exception):
Expand Down
Loading