diff --git a/src/onthespot/api/spotify.py b/src/onthespot/api/spotify.py index b54dffc..415ccc2 100644 --- a/src/onthespot/api/spotify.py +++ b/src/onthespot/api/spotify.py @@ -11,7 +11,7 @@ from PyQt6.QtCore import QObject from ..otsconfig import config, cache_dir from ..runtimedata import get_logger, account_pool, pending, download_queue -from ..utils import make_call, conv_list_format +from ..utils import make_call, conv_list_format, format_local_id logger = get_logger("api.spotify") @@ -79,15 +79,17 @@ def run(self): parent_category = 'playlist' elif data['context']['type'] in ('album', 'artist'): parent_category = 'album' - + # Use item id to prevent duplicates + #local_id = format_local_id(item_id) pending[item_id] = { - 'item_service': 'spotify', - 'item_type': 'track', - 'item_id': item_id, - 'parent_category': parent_category, - 'playlist_name': playlist_name, - 'playlist_by': playlist_by, - 'playlist_number': '?' + 'local_id': item_id, + 'item_service': 'spotify', + 'item_type': 'track', + 'item_id': item_id, + 'parent_category': parent_category, + 'playlist_name': playlist_name, + 'playlist_by': playlist_by, + 'playlist_number': '?' } logger.info(f'Mirror Spotify Playback added track to download queue: https://open.spotify.com/track/{item_id}') continue diff --git a/src/onthespot/parse_item.py b/src/onthespot/parse_item.py index 8412f2f..aba1c1b 100755 --- a/src/onthespot/parse_item.py +++ b/src/onthespot/parse_item.py @@ -6,6 +6,7 @@ from .runtimedata import get_logger, parsing, download_queue, pending, account_pool from .accounts import get_account_token from .api.deezer import deezer_get_album_items, deezer_get_playlist_items, deezer_get_playlist_data, deezer_get_artist_albums +from .utils import format_local_id logger = get_logger('parse_item') @@ -82,7 +83,7 @@ def parsingworker(): if current_service == "spotify": if current_type == "track": - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': current_service, @@ -96,7 +97,7 @@ def parsingworker(): tracks = spotify_get_album_tracks(token, current_id) for index, track in enumerate(tracks): item_id = track['id'] - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': 'spotify', @@ -113,7 +114,7 @@ def parsingworker(): try: item_id = item['track']['id'] item_type = item['track']['type'] - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': 'spotify', @@ -135,7 +136,7 @@ def parsingworker(): continue elif current_type == "episode": - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': current_service, @@ -148,7 +149,7 @@ def parsingworker(): elif current_type in ['show', 'audiobook']: episode_ids = spotify_get_show_episodes(token, current_id) for index, episode_id in enumerate(episode_ids): - local_id = format_item_id(episode_id) + local_id = format_local_id(episode_id) pending[local_id] = { 'local_id': local_id, 'item_service': 'spotify', @@ -162,7 +163,7 @@ def parsingworker(): tracks = spotify_get_liked_songs(token) for index, track in enumerate(tracks): item_id = track['track']['id'] - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': 'spotify', @@ -179,7 +180,7 @@ def parsingworker(): tracks = spotify_get_your_episodes(token) for index, track in enumerate(tracks): item_id = track['show']['id'] - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': 'spotify', @@ -195,7 +196,7 @@ def parsingworker(): elif current_service == "soundcloud": if current_type == "track": - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': current_service, @@ -210,7 +211,7 @@ def parsingworker(): set_data = soundcloud_get_set_items(token, item['item_url']) for index, track in enumerate(set_data['tracks']): item_id = track['id'] - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_url': track.get('permalink_url', ''), @@ -226,7 +227,7 @@ def parsingworker(): elif current_service == "deezer": if current_type == "track": - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': current_service, @@ -239,7 +240,7 @@ def parsingworker(): tracks = deezer_get_album_items(current_id) for index, track in enumerate(tracks): item_id = track['id'] - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': 'deezer', @@ -253,7 +254,7 @@ def parsingworker(): playlist_name, playlist_by = deezer_get_playlist_data(current_id) for index, track in enumerate(tracks): item_id = track['id'] - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': 'deezer', @@ -274,7 +275,7 @@ def parsingworker(): elif current_service == "youtube": if current_type == "track": - local_id = format_item_id(item_id) + local_id = format_local_id(item_id) pending[local_id] = { 'local_id': local_id, 'item_service': current_service, @@ -285,11 +286,3 @@ def parsingworker(): continue else: time.sleep(0.2) - -def format_item_id(item_id): - suffix = 0 - local_id = f"{item_id}-{suffix}" - while local_id in download_queue or local_id in pending: - suffix += 1 - local_id = f"{item_id}-{suffix}" - return local_id \ No newline at end of file diff --git a/src/onthespot/utils.py b/src/onthespot/utils.py index 51274b1..7642f04 100644 --- a/src/onthespot/utils.py +++ b/src/onthespot/utils.py @@ -14,11 +14,20 @@ from mutagen.oggvorbis import OggVorbis import music_tag from .otsconfig import config, config_dir -from .runtimedata import get_logger +from .runtimedata import get_logger, pending, download_queue logger = get_logger("utils") +def format_local_id(item_id): + suffix = 0 + local_id = f"{item_id}-{suffix}" + while local_id in download_queue or local_id in pending: + suffix += 1 + local_id = f"{item_id}-{suffix}" + return local_id + + def is_latest_release(): url = "https://api.github.com/repos/justin025/onthespot/releases/latest" response = requests.get(url)