-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import Playlists from SoundCloud (#143)
* added soundcloud import * added soundcloud import * added soundcloud import * added title spliter * added title spliter * removed logs and cleaned the code * added rate limit to apple/soundcloud endpoints * defined rate limit for better clarification * added http session with retry && refactored API calls * minor cleanup fix authorization header, formatting fixes, and remove unused dependency --------- Co-authored-by: Rimma Kubanova <rimma.kubanova@gmail.com> Co-authored-by: Kartik Ohri <kartikohri13@gmail.com>
- Loading branch information
1 parent
eb2182e
commit 8da768d
Showing
6 changed files
with
86 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
import requests | ||
|
||
from troi.tools.spotify_lookup import APPLE_MUSIC_URL | ||
|
||
|
||
def convert_apple_tracks_to_json(apple_tracks): | ||
tracks= [] | ||
for track in apple_tracks: | ||
tracks.append({ | ||
"recording_name": track['attributes']['name'], | ||
"artist_name": track['attributes']['artistName'], | ||
}) | ||
return tracks | ||
from .utils import create_http_session | ||
|
||
APPLE_MUSIC_URL = f"https://api.music.apple.com/" | ||
|
||
def get_tracks_from_apple_playlist(developer_token, user_token, playlist_id): | ||
""" Get tracks from the Apple Music playlist. | ||
""" | ||
http = create_http_session() | ||
|
||
headers = { | ||
"Authorization": f"Bearer {developer_token}", | ||
"Music-User-Token": user_token | ||
} | ||
response = requests.get(APPLE_MUSIC_URL+f"v1/me/library/playlists/{playlist_id}?include=tracks", headers=headers) | ||
if response.status_code == 200: | ||
response = response.json() | ||
tracks = response["data"][0]["relationships"]["tracks"]["data"] | ||
name = response["data"][0]["attributes"]["name"] | ||
description = response["data"][0]["attributes"].get("description", {}).get("standard", "") | ||
else: | ||
response.raise_for_status() | ||
return tracks, name, description | ||
response = http.get(APPLE_MUSIC_URL+f"v1/me/library/playlists/{playlist_id}?include=tracks", headers=headers) | ||
response.raise_for_status() | ||
|
||
response = response.json() | ||
tracks = response["data"][0]["relationships"]["tracks"]["data"] | ||
name = response["data"][0]["attributes"]["name"] | ||
description = response["data"][0]["attributes"].get("description", {}).get("standard", "") | ||
|
||
mapped_tracks = [ | ||
{ | ||
"recording_name": track['attributes']['name'], | ||
"artist_name": track['attributes']['artistName'] | ||
} | ||
for track in tracks | ||
] | ||
|
||
return mapped_tracks, name, description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from .utils import create_http_session | ||
|
||
SOUNDCLOUD_URL = f"https://api.soundcloud.com/" | ||
|
||
def get_tracks_from_soundcloud_playlist(developer_token, playlist_id): | ||
""" Get tracks from the Soundcloud playlist. | ||
""" | ||
http = create_http_session() | ||
|
||
headers = { | ||
"Authorization": f"OAuth {developer_token}", | ||
} | ||
response = http.get(f"{SOUNDCLOUD_URL}/playlists/{playlist_id}", headers=headers) | ||
response.raise_for_status() | ||
|
||
response = response.json() | ||
tracks = response["tracks"] | ||
name = response["title"] | ||
description = response["description"] | ||
|
||
mapped_tracks = [ | ||
{ | ||
"recording_name": track['title'].split(" - ")[1] if " - " in track['title'] else track['title'], | ||
"artist_name": track['title'].split(" - ")[0] if " - " in track['title'] else track['user']['username'] | ||
} | ||
for track in tracks | ||
] | ||
|
||
return mapped_tracks, name, description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import requests | ||
from requests.adapters import HTTPAdapter | ||
from urllib3.util.retry import Retry | ||
|
||
|
||
def create_http_session(): | ||
""" Create an HTTP session with retry strategy for handling rate limits and server errors. | ||
""" | ||
retry_strategy = Retry( | ||
total=3, | ||
status_forcelist=[429, 500, 502, 503, 504], | ||
allowed_methods=["HEAD", "GET", "OPTIONS"], | ||
backoff_factor=1 | ||
) | ||
|
||
adapter = HTTPAdapter(max_retries=retry_strategy) | ||
http = requests.Session() | ||
http.mount("https://", adapter) | ||
http.mount("http://", adapter) | ||
return http |