Skip to content

Commit

Permalink
Add ability to download spotify liked songs and your episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Oct 26, 2024
1 parent 12460ba commit 96a70ca
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/onthespot/api/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,42 @@ def spotify_get_playlist_items(token, playlist_id):
resp = make_call(url, headers=headers, skip_cache=True)
return resp

def spotify_get_liked_songs(token):
logger.info(f"Getting liked songs")
songs = []
offset = 0
limit = 50
token = token.tokens().get("user-library-read")
headers = {'Authorization': f'Bearer {token}'}
url = f'https://api.spotify.com/v1/me/tracks'
while True:
resp = make_call(url, headers=headers, skip_cache=True)

offset += limit
songs.extend(resp['items'])

if resp['total'] <= offset:
break
return songs


def spotify_get_your_episodes(token):
logger.info(f"Getting your episodes")
songs = []
offset = 0
limit = 50
token = token.tokens().get("user-library-read")
headers = {'Authorization': f'Bearer {token}'}
url = f'https://api.spotify.com/v1/me/shows'
while True:
resp = make_call(url, headers=headers, skip_cache=True)

offset += limit
songs.extend(resp['items'])

if resp['total'] <= offset:
break
return songs

def get_album_name(session, album_id):
logger.info(f"Get album info from album by id ''{album_id}'")
Expand Down
41 changes: 40 additions & 1 deletion src/onthespot/parse_item.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import time
from .otsconfig import config
from .api.spotify import spotify_get_token, spotify_get_album_tracks, spotify_get_playlist_data, spotify_get_playlist_items, spotify_get_artist_albums, spotify_get_show_episodes
from .api.spotify import spotify_get_token, spotify_get_liked_songs, spotify_get_your_episodes, spotify_get_album_tracks, spotify_get_playlist_data, spotify_get_playlist_items, spotify_get_artist_albums, spotify_get_show_episodes
from .api.soundcloud import soundcloud_parse_url, soundcloud_get_set_items
from .runtimedata import get_logger, parsing, download_queue, pending
from .accounts import get_account_token
Expand All @@ -25,6 +25,16 @@ def parse_url(url):
item_id = match.group("ID")
item_type = match.group("Type")
item_service = "spotify"
# Spotify Liked Songs
elif account_service == 'spotify' and url == 'https://open.spotify.com/collection/tracks':
item_id = None
item_type = 'liked_songs'
item_service = "spotify"
# Spotify Your Episodes
elif account_service == 'spotify' and url == 'https://open.spotify.com/collection/your-episodes':
item_id = None
item_type = 'your_episodes'
item_service = "spotify"
else:
logger.info(f'Invalid Url: {url}')
return False
Expand Down Expand Up @@ -112,6 +122,35 @@ def parsingworker():
parse_url(episode_url)
continue

elif current_type == "liked_songs":
print(token)
tracks = spotify_get_liked_songs(token)
for index, track in enumerate(tracks):
item_id = track['track']['id']
pending[item_id] = {
'item_service': 'spotify',
'item_type': 'track',
'item_id': item_id,
'is_playlist_item': True,
'playlist_name': 'Liked Songs',
'playlist_by': 'me'
}
continue

elif current_type == "your_episodes":
tracks = spotify_get_your_episodes(token)
for index, track in enumerate(tracks):
item_id = track['show']['id']
pending[item_id] = {
'item_service': 'spotify',
'item_type': 'episode',
'item_id': item_id,
'is_playlist_item': True,
'playlist_name': 'Your Episodes',
'playlist_by': 'me'
}
continue

elif current_service == "soundcloud":

if current_type == "track":
Expand Down

0 comments on commit 96a70ca

Please sign in to comment.