Skip to content

Commit

Permalink
Add support for tidal mixes and clean playlist parser
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Dec 17, 2024
1 parent 122c904 commit 3c3dbef
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 50 deletions.
20 changes: 9 additions & 11 deletions src/onthespot/api/apple_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,14 @@ def apple_music_get_artist_album_ids(session, artist_id):
return item_ids


def apple_music_get_playlist_track_ids(session, playlist_id):
logger.info(f"Getting items in playlist: '{playlist_id}'")
playlist_data = make_call(f"{BASE_URL}/catalog/{session.cookies.get("itua")}/playlists/{playlist_id}", session=session)
item_ids = []
for track in playlist_data.get('data', [])[0].get('relationships', {}).get('tracks', {}).get('data', []):
item_ids.append(track['id'])
return item_ids
def apple_music_get_playlist_data(session, playlist_id):
logger.info(f"Get playlist data for playlist: {playlist_id}")
playlist_data = make_call(f"{BASE_URL}/catalog/{session.cookies.get("itua")}/playlists/{playlist_id}", session=session, skip_cache=True)

playlist_name = playlist_data.get('data', [])[0].get('attributes', {}).get('name', '')
playlist_by = playlist_data.get('data', [])[0].get('attributes', {}).get('curatorName', '')

def apple_music_get_playlist_data(session, playlist_id):
logger.info(f"Get playlist data for playlist: '{playlist_id}'")
playlist_data = make_call(f"{BASE_URL}/catalog/{session.cookies.get("itua")}/playlists/{playlist_id}", session=session)
return playlist_data.get('data', [])[0].get('attributes', {}).get('name', ''), playlist_data.get('data', [])[0].get('attributes', {}).get('curatorName', '')
track_ids = []
for track in playlist_data.get('data', [])[0].get('relationships', {}).get('tracks', {}).get('data', []):
track_ids.append(track['id'])
return playlist_name, playlist_by, track_ids
18 changes: 8 additions & 10 deletions src/onthespot/api/deezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,17 @@ def deezer_get_artist_album_ids(token, artist_id):
return item_ids


def deezer_get_playlist_track_ids(token, playlist_id):
logger.info(f"Getting items in playlist: '{playlist_id}'")
album_data = make_call(f"{BASE_URL}/playlist/{playlist_id}")
item_ids = []
for track in album_data.get("tracks", {}).get("data", ''):
item_ids.append(track['id'])
return item_ids


def deezer_get_playlist_data(token, playlist_id):
logger.info(f"Get playlist data for playlist: '{playlist_id}'")
playlist_data = make_call(f"{BASE_URL}/playlist/{playlist_id}")
return playlist_data.get("title", ''), playlist_data.get("creator", {}).get("name", '')

playlist_name = playlist_data.get("title", '')
playlist_by = playlist_data.get("creator", {}).get("name", '')

track_ids = []
for track in playlist_data.get("tracks", {}).get("data", ''):
track_ids.append(track.get('id', ''))
return playlist_name, playlist_by, track_ids


def deezer_get_track_metadata(token, item_id):
Expand Down
42 changes: 27 additions & 15 deletions src/onthespot/api/tidal.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,32 +379,44 @@ def tidal_get_album_track_ids(token, album_id):
return item_ids


def tidal_get_playlist_track_ids(token, playlist_id):
logger.info(f"Getting items in playlist: {playlist_id}")
def tidal_get_playlist_data(token, playlist_id):
logger.info(f"Get playlist data for playlist: {playlist_id}")
headers = {}
headers["Authorization"] = f"Bearer {token['access_token']}"

params = {}
params["countryCode"] = token['country_code']
params["limit"] = '10000'

playlist_track_data = make_call(f"{BASE_URL}/playlists/{playlist_id}/tracks", params=params, headers=headers)
playlist_data = make_call(f"{BASE_URL}/playlists/{playlist_id}", params=params, headers=headers, skip_cache=True)
playlist_track_data = make_call(f"{BASE_URL}/playlists/{playlist_id}/tracks", params=params, headers=headers, skip_cache=True)

item_ids = []
playlist_name = playlist_data.get('title', '')
playlist_by = playlist_data.get('creator', {}).get('name', 'Tidal')

track_ids = []
for track in playlist_track_data['items']:
item_ids.append(track['id'])
return item_ids
track_ids.append(track.get('id', ''))
return playlist_name, playlist_by, track_ids

def tidal_get_playlist_data(token, playlist_id):
logger.info(f"Get playlist data for playlist: {playlist_id}")

def tidal_get_mix_data(token, mix_id):
logger.info(f"Get mix data for mix: {mix_id}")
headers = {}
headers["Authorization"] = f"Bearer {token['access_token']}"

print(mix_id)
params = {}
params["mixId"] = mix_id
params["countryCode"] = token['country_code']
params["limit"] = '10000'

playlist_data = make_call(f"{BASE_URL}/playlists/{playlist_id}", params=params, headers=headers)
playlist_name = playlist_data.get('title', '')
playlist_by = playlist_data.get('creator', {}).get('name', 'Tidal')
return playlist_name, playlist_by
params["locale"] = 'en_US'
params["deviceType"] = 'BROWSER'

mix_data = make_call(f"https://api.tidal.com/v1/pages/mix", params=params, headers=headers)#, skip_cache=True)
print(mix_data)
playlist_name = mix_data['title']
playlist_by = 'Tidal'

track_ids = []
for track in mix_data['rows'][1]['modules'][0]['pagedList']['items']:
track_ids.append(track['id'])
return playlist_name, playlist_by, track_ids
30 changes: 16 additions & 14 deletions src/onthespot/parse_item.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re
import time
from .accounts import get_account_token
from .api.apple_music import apple_music_get_album_track_ids, apple_music_get_artist_album_ids, apple_music_get_playlist_track_ids, apple_music_get_playlist_data
from .api.apple_music import apple_music_get_album_track_ids, apple_music_get_artist_album_ids, apple_music_get_playlist_data
from .api.bandcamp import bandcamp_get_album_track_ids, bandcamp_get_artist_album_ids
from .api.deezer import deezer_get_album_track_ids, deezer_get_artist_album_ids, deezer_get_playlist_track_ids, deezer_get_playlist_data
from .api.deezer import deezer_get_album_track_ids, deezer_get_artist_album_ids, deezer_get_playlist_data
from .api.soundcloud import soundcloud_parse_url, soundcloud_get_set_items
from .api.spotify import spotify_get_album_track_ids, spotify_get_artist_album_ids, spotify_get_playlist_items, spotify_get_playlist_data, spotify_get_liked_songs, spotify_get_your_episodes, spotify_get_show_episode_ids
from .api.tidal import tidal_get_album_track_ids, tidal_get_artist_album_ids, tidal_get_playlist_track_ids, tidal_get_playlist_data
from .api.tidal import tidal_get_album_track_ids, tidal_get_artist_album_ids, tidal_get_playlist_data, tidal_get_mix_data
from .runtimedata import get_logger, parsing, download_queue, pending, parsing_lock, pending_lock
from .utils import format_local_id

Expand All @@ -16,7 +16,7 @@
DEEZER_URL_REGEX = re.compile(r'https?://www.deezer.com/(?:[a-z]{2}/)?(?P<type>album|playlist|track|artist)/(?P<id>\d+)')
SOUNDCLOUD_URL_REGEX = re.compile(r"https?://soundcloud.com/[-\w:/]+")
SPOTIFY_URL_REGEX = re.compile(r"https?://open.spotify.com/(intl-([a-zA-Z]+)/|)(?P<type>track|album|artist|playlist|episode|show)/(?P<id>[0-9a-zA-Z]{22})(\?si=.+?)?$")
TIDAL_URL_REGEX = re.compile(r"https?://(www\.|listen\.)?tidal.com/(browse/)?(?P<type>album|track|artist|playlist)/(?P<id>\d+)")
TIDAL_URL_REGEX = re.compile(r"https?://(www\.|listen\.)?tidal.com/(browse/)?(?P<type>album|track|artist|playlist|mix)/(?P<id>[-a-z0-9]+)")
YOUTUBE_URL_REGEX = re.compile(r"https?://(www\.|music\.)?youtube\.com/watch\?v=(?P<video_id>[a-zA-Z0-9_-]+)(&list=(?P<list_id>[a-zA-Z0-9_-]+))?")
#QOBUZ_INTERPRETER_URL_REGEX = re.compile(r"https?://www\.qobuz\.com/\w\w-\w\w/interpreter/[-\w]+/([-\w]+)")

Expand Down Expand Up @@ -197,22 +197,24 @@ def parsingworker():
}
continue

elif current_type in ("album", "playlist"):
item_ids = globals()[f"{current_service}_get_{current_type}_track_ids"](token, current_id)
elif current_type in ["album", "playlist", "mix"]:
if current_type == "album":
playlist_name = ''
playlist_by = ''
track_ids = globals()[f"{current_service}_get_{current_type}_track_ids"](token, current_id)
else:
playlist_name, playlist_by, track_ids = globals()[f"{current_service}_get_{current_type}_data"](token, current_id)
if current_type == 'mix':
current_type = 'playlist'

playlist_name = ''
playlist_by = ''
if current_type == "playlist":
playlist_name, playlist_by = globals()[f"{current_service}_get_playlist_data"](token, current_id)

for index, item_id in enumerate(item_ids):
local_id = format_local_id(item_id)
for index, track_id in enumerate(track_ids):
local_id = format_local_id(track_id)
with pending_lock:
pending[local_id] = {
'local_id': local_id,
'item_service': current_service,
'item_type': 'track',
'item_id': item_id,
'item_id': track_id,
'parent_category': current_type,
'playlist_name': playlist_name,
'playlist_by': playlist_by,
Expand Down

0 comments on commit 3c3dbef

Please sign in to comment.