complete overhaul with pre-commit hooks

This commit is contained in:
Mustafa Soylu
2025-06-07 18:47:18 +02:00
parent 3971dba9bf
commit 62cbeeb513
71 changed files with 4200 additions and 2820 deletions

View File

@@ -1,11 +1,12 @@
import os
import json
import traceback
from deezspot.spotloader import SpoLogin
from deezspot.deezloader import DeeLogin
from pathlib import Path
from routes.utils.credentials import get_credential, _get_global_spotify_api_creds, get_spotify_blob_path
from routes.utils.celery_config import get_config_params
from routes.utils.credentials import (
get_credential,
_get_global_spotify_api_creds,
get_spotify_blob_path,
)
def download_track(
url,
@@ -23,28 +24,32 @@ def download_track(
max_retries=3,
progress_callback=None,
convert_to=None,
bitrate=None
bitrate=None,
):
try:
# Detect URL source (Spotify or Deezer) from URL
is_spotify_url = 'open.spotify.com' in url.lower()
is_deezer_url = 'deezer.com' in url.lower()
service = ''
is_spotify_url = "open.spotify.com" in url.lower()
is_deezer_url = "deezer.com" in url.lower()
service = ""
if is_spotify_url:
service = 'spotify'
service = "spotify"
elif is_deezer_url:
service = 'deezer'
service = "deezer"
else:
error_msg = "Invalid URL: Must be from open.spotify.com or deezer.com"
print(f"ERROR: {error_msg}")
raise ValueError(error_msg)
print(f"DEBUG: track.py - Service determined from URL: {service}")
print(f"DEBUG: track.py - Credentials provided: main_account_name='{main}', fallback_account_name='{fallback}'")
print(
f"DEBUG: track.py - Credentials provided: main_account_name='{main}', fallback_account_name='{fallback}'"
)
# Get global Spotify API credentials for SpoLogin and DeeLogin (if it uses Spotify search)
global_spotify_client_id, global_spotify_client_secret = _get_global_spotify_api_creds()
global_spotify_client_id, global_spotify_client_secret = (
_get_global_spotify_api_creds()
)
if not global_spotify_client_id or not global_spotify_client_secret:
# This is a critical failure if Spotify operations are involved
warning_msg = "WARN: track.py - Global Spotify client_id/secret not found in search.json. Spotify operations will likely fail."
@@ -52,31 +57,39 @@ def download_track(
# Depending on flow, might want to raise error here if service is 'spotify'
# For now, let it proceed and fail at SpoLogin/DeeLogin init if keys are truly needed and missing.
if service == 'spotify':
if fallback: # Fallback is a Deezer account name for a Spotify URL
if quality is None: quality = 'FLAC' # Deezer quality for first attempt
if fall_quality is None: fall_quality = 'HIGH' # Spotify quality for fallback (if Deezer fails)
if service == "spotify":
if fallback: # Fallback is a Deezer account name for a Spotify URL
if quality is None:
quality = "FLAC" # Deezer quality for first attempt
if fall_quality is None:
fall_quality = (
"HIGH" # Spotify quality for fallback (if Deezer fails)
)
deezer_error = None
try:
# Attempt 1: Deezer via download_trackspo (using 'fallback' as Deezer account name)
print(f"DEBUG: track.py - Spotify URL. Attempt 1: Deezer (account: {fallback})")
deezer_fallback_creds = get_credential('deezer', fallback)
arl = deezer_fallback_creds.get('arl')
print(
f"DEBUG: track.py - Spotify URL. Attempt 1: Deezer (account: {fallback})"
)
deezer_fallback_creds = get_credential("deezer", fallback)
arl = deezer_fallback_creds.get("arl")
if not arl:
raise ValueError(f"ARL not found for Deezer account '{fallback}'.")
raise ValueError(
f"ARL not found for Deezer account '{fallback}'."
)
dl = DeeLogin(
arl=arl,
spotify_client_id=global_spotify_client_id, # Global creds
spotify_client_secret=global_spotify_client_secret, # Global creds
progress_callback=progress_callback
spotify_client_id=global_spotify_client_id, # Global creds
spotify_client_secret=global_spotify_client_secret, # Global creds
progress_callback=progress_callback,
)
# download_trackspo means: Spotify URL, download via Deezer
dl.download_trackspo(
link_track=url, # Spotify URL
link_track=url, # Spotify URL
output_dir="./downloads",
quality_download=quality, # Deezer quality
quality_download=quality, # Deezer quality
recursive_quality=False,
recursive_download=False,
not_interface=False,
@@ -87,35 +100,52 @@ def download_track(
retry_delay_increase=retry_delay_increase,
max_retries=max_retries,
convert_to=convert_to,
bitrate=bitrate
bitrate=bitrate,
)
print(
f"DEBUG: track.py - Track download via Deezer (account: {fallback}) successful for Spotify URL."
)
print(f"DEBUG: track.py - Track download via Deezer (account: {fallback}) successful for Spotify URL.")
except Exception as e:
deezer_error = e
print(f"ERROR: track.py - Deezer attempt (account: {fallback}) for Spotify URL failed: {e}")
print(
f"ERROR: track.py - Deezer attempt (account: {fallback}) for Spotify URL failed: {e}"
)
traceback.print_exc()
print(f"DEBUG: track.py - Attempting Spotify direct download (account: {main})...")
print(
f"DEBUG: track.py - Attempting Spotify direct download (account: {main})..."
)
# Attempt 2: Spotify direct via download_track (using 'main' as Spotify account for blob)
try:
if not global_spotify_client_id or not global_spotify_client_secret:
raise ValueError("Global Spotify API credentials (client_id/secret) not configured for Spotify download.")
if (
not global_spotify_client_id
or not global_spotify_client_secret
):
raise ValueError(
"Global Spotify API credentials (client_id/secret) not configured for Spotify download."
)
# Use get_spotify_blob_path directly
blob_file_path = get_spotify_blob_path(main)
if not blob_file_path.exists(): # Check existence on the Path object
raise FileNotFoundError(f"Spotify credentials blob file not found at {str(blob_file_path)} for account '{main}'")
blob_file_path = get_spotify_blob_path(main)
if (
not blob_file_path.exists()
): # Check existence on the Path object
raise FileNotFoundError(
f"Spotify credentials blob file not found at {str(blob_file_path)} for account '{main}'"
)
spo = SpoLogin(
credentials_path=str(blob_file_path), # Account specific blob
spotify_client_id=global_spotify_client_id, # Global API keys
spotify_client_secret=global_spotify_client_secret, # Global API keys
progress_callback=progress_callback
credentials_path=str(
blob_file_path
), # Account specific blob
spotify_client_id=global_spotify_client_id, # Global API keys
spotify_client_secret=global_spotify_client_secret, # Global API keys
progress_callback=progress_callback,
)
spo.download_track(
link_track=url, # Spotify URL
link_track=url, # Spotify URL
output_dir="./downloads",
quality_download=fall_quality, # Spotify quality
quality_download=fall_quality, # Spotify quality
recursive_quality=False,
recursive_download=False,
not_interface=False,
@@ -128,38 +158,49 @@ def download_track(
retry_delay_increase=retry_delay_increase,
max_retries=max_retries,
convert_to=convert_to,
bitrate=bitrate
bitrate=bitrate,
)
print(
f"DEBUG: track.py - Spotify direct download (account: {main} for blob) successful."
)
print(f"DEBUG: track.py - Spotify direct download (account: {main} for blob) successful.")
except Exception as e2:
print(f"ERROR: track.py - Spotify direct download (account: {main} for blob) also failed: {e2}")
print(
f"ERROR: track.py - Spotify direct download (account: {main} for blob) also failed: {e2}"
)
raise RuntimeError(
f"Both Deezer attempt (account: {fallback}) and Spotify direct (account: {main} for blob) failed. "
f"Deezer error: {deezer_error}, Spotify error: {e2}"
) from e2
else:
# Spotify URL, no fallback. Direct Spotify download using 'main' (Spotify account for blob)
if quality is None: quality = 'HIGH' # Default Spotify quality
print(f"DEBUG: track.py - Spotify URL, no fallback. Direct download with Spotify account (for blob): {main}")
if quality is None:
quality = "HIGH" # Default Spotify quality
print(
f"DEBUG: track.py - Spotify URL, no fallback. Direct download with Spotify account (for blob): {main}"
)
if not global_spotify_client_id or not global_spotify_client_secret:
raise ValueError("Global Spotify API credentials (client_id/secret) not configured for Spotify download.")
raise ValueError(
"Global Spotify API credentials (client_id/secret) not configured for Spotify download."
)
# Use get_spotify_blob_path directly
blob_file_path = get_spotify_blob_path(main)
if not blob_file_path.exists(): # Check existence on the Path object
raise FileNotFoundError(f"Spotify credentials blob file not found at {str(blob_file_path)} for account '{main}'")
if not blob_file_path.exists(): # Check existence on the Path object
raise FileNotFoundError(
f"Spotify credentials blob file not found at {str(blob_file_path)} for account '{main}'"
)
spo = SpoLogin(
credentials_path=str(blob_file_path), # Account specific blob
spotify_client_id=global_spotify_client_id, # Global API keys
spotify_client_secret=global_spotify_client_secret, # Global API keys
progress_callback=progress_callback
credentials_path=str(blob_file_path), # Account specific blob
spotify_client_id=global_spotify_client_id, # Global API keys
spotify_client_secret=global_spotify_client_secret, # Global API keys
progress_callback=progress_callback,
)
spo.download_track(
link_track=url,
output_dir="./downloads",
quality_download=quality,
quality_download=quality,
recursive_quality=False,
recursive_download=False,
not_interface=False,
@@ -172,26 +213,31 @@ def download_track(
retry_delay_increase=retry_delay_increase,
max_retries=max_retries,
convert_to=convert_to,
bitrate=bitrate
bitrate=bitrate,
)
print(f"DEBUG: track.py - Direct Spotify download (account: {main} for blob) successful.")
elif service == 'deezer':
print(
f"DEBUG: track.py - Direct Spotify download (account: {main} for blob) successful."
)
elif service == "deezer":
# Deezer URL. Direct Deezer download using 'main' (Deezer account name for ARL)
if quality is None: quality = 'FLAC' # Default Deezer quality
print(f"DEBUG: track.py - Deezer URL. Direct download with Deezer account: {main}")
deezer_main_creds = get_credential('deezer', main) # For ARL
arl = deezer_main_creds.get('arl')
if quality is None:
quality = "FLAC" # Default Deezer quality
print(
f"DEBUG: track.py - Deezer URL. Direct download with Deezer account: {main}"
)
deezer_main_creds = get_credential("deezer", main) # For ARL
arl = deezer_main_creds.get("arl")
if not arl:
raise ValueError(f"ARL not found for Deezer account '{main}'.")
dl = DeeLogin(
arl=arl, # Account specific ARL
spotify_client_id=global_spotify_client_id, # Global Spotify keys for internal Spo use by DeeLogin
spotify_client_secret=global_spotify_client_secret, # Global Spotify keys
progress_callback=progress_callback
arl=arl, # Account specific ARL
spotify_client_id=global_spotify_client_id, # Global Spotify keys for internal Spo use by DeeLogin
spotify_client_secret=global_spotify_client_secret, # Global Spotify keys
progress_callback=progress_callback,
)
dl.download_trackdee( # Deezer URL, download via Deezer
dl.download_trackdee( # Deezer URL, download via Deezer
link_track=url,
output_dir="./downloads",
quality_download=quality,
@@ -205,12 +251,14 @@ def download_track(
retry_delay_increase=retry_delay_increase,
max_retries=max_retries,
convert_to=convert_to,
bitrate=bitrate
bitrate=bitrate,
)
print(
f"DEBUG: track.py - Direct Deezer download (account: {main}) successful."
)
print(f"DEBUG: track.py - Direct Deezer download (account: {main}) successful.")
else:
# Should be caught by initial service check, but as a safeguard
raise ValueError(f"Unsupported service determined: {service}")
except Exception as e:
except Exception:
traceback.print_exc()
raise