Skip to content

Commit

Permalink
Fix spotify playlist parser and m3u already exists bug
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Oct 26, 2024
1 parent 96a70ca commit 388980c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
60 changes: 45 additions & 15 deletions src/onthespot/api/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,48 +252,78 @@ def spotify_get_lyrics(session, item_id, item_type, metadata, filepath):

def spotify_get_playlist_items(token, playlist_id):
logger.info(f"Getting items in playlist: '{playlist_id}'")
items = []
offset = 0
limit = 50
token = token.tokens().get("playlist-read-private")
headers = {'Authorization': f'Bearer {token}'}
url = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks?additional_types=track%2Cepisode'
resp = make_call(url, headers=headers, skip_cache=True)
return resp


while True:
headers = {'Authorization': f'Bearer {token}'}
params = {
'limit': limit,
'offset': offset
}

resp = make_call(url, headers=headers, params=params, skip_cache=True)

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

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


def spotify_get_liked_songs(token):
logger.info(f"Getting liked songs")
songs = []
logger.info("Getting liked songs")
items = []
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)
headers = {'Authorization': f'Bearer {token}'}
params = {
'limit': limit,
'offset': offset
}

resp = make_call(url, headers=headers, params=params, skip_cache=True)

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

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


def spotify_get_your_episodes(token):
logger.info(f"Getting your episodes")
songs = []
logger.info("Getting your episodes")
items = []
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)
headers = {'Authorization': f'Bearer {token}'}
params = {
'limit': limit,
'offset': offset
}

resp = make_call(url, headers=headers, params=params, skip_cache=True)

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

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

def get_album_name(session, album_id):
logger.info(f"Get album info from album by id ''{album_id}'")
Expand Down
11 changes: 9 additions & 2 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def run(self):
if file_path not in [line.strip() for line in m3u_contents]:
with open(m3u_path, 'a') as m3u_file:
m3u_file.write(f"#EXTINF:-1, {item_metadata['artists']} - {item_metadata['title']}\n{file_path}\n")
else:
logger.info(f"{file_path} already exists in the M3U file.") # Log or handle the existing entry case




# Skip download if file exists under different extension
Expand All @@ -132,10 +136,13 @@ def run(self):
matching_files = [file for file in files_in_directory if file.startswith(base_file_path) and not file.endswith('.lrc')]

if matching_files:
item['item_status'] = 'Already Exists'
if self.gui:
if item['gui']['status_label'].text() == self.tr("Downloading"):
if item['item_status'] in (
"Downloading",
"Adding To M3U"
):
self.progress.emit(item, self.tr("Already Exists"), 100) # Emit progress
item['item_status'] = 'Already Exists'
logger.info(f"File already exists, Skipping download for track by id '{item_id}'")
time.sleep(1)
self.readd_item_to_download_queue(item)
Expand Down
9 changes: 4 additions & 5 deletions src/onthespot/parse_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def parsingworker():
continue

elif current_type == "playlist":
data = spotify_get_playlist_items(token, current_id)
items = spotify_get_playlist_items(token, current_id)
playlist_name, playlist_by = spotify_get_playlist_data(token, current_id)
for index, track in enumerate(data["items"]):
item_id = track['track']['id']
item_type = track['track']['type']
for index, item in enumerate(items):
item_id = item['track']['id']
item_type = item['track']['type']
pending[item_id] = {
'item_service': 'spotify',
'item_type': item_type,
Expand Down Expand Up @@ -123,7 +123,6 @@ def parsingworker():
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']
Expand Down

0 comments on commit 388980c

Please sign in to comment.