Skip to content

Commit

Permalink
Fix mirror playback keyerror
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Nov 28, 2024
1 parent bcf2070 commit 933b4ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
20 changes: 11 additions & 9 deletions src/onthespot/api/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand Down
35 changes: 14 additions & 21 deletions src/onthespot/parse_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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', ''),
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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
11 changes: 10 additions & 1 deletion src/onthespot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 933b4ce

Please sign in to comment.