added spot fallback
This commit is contained in:
@@ -4,57 +4,95 @@ import traceback
|
||||
from deezspot.spotloader import SpoLogin
|
||||
from deezspot.deezloader import DeeLogin
|
||||
|
||||
def download_album(service, url, account):
|
||||
def download_album(service, url, main, fallback=None):
|
||||
try:
|
||||
if service == 'spotify':
|
||||
# Construct Spotify credentials path
|
||||
creds_dir = os.path.join('./creds/spotify', account)
|
||||
credentials_path = os.path.abspath(os.path.join(creds_dir, 'credentials.json'))
|
||||
|
||||
# Initialize Spotify client
|
||||
spo = SpoLogin(credentials_path=credentials_path)
|
||||
|
||||
# Download Spotify album
|
||||
spo.download_album(
|
||||
link_album=url,
|
||||
output_dir="./downloads/albums",
|
||||
quality_download="NORMAL",
|
||||
recursive_quality=True,
|
||||
recursive_download=False,
|
||||
not_interface=False,
|
||||
method_save=1,
|
||||
make_zip=True
|
||||
)
|
||||
|
||||
if fallback:
|
||||
# First attempt: use DeeLogin's download_albumspo with the 'main' (Deezer credentials)
|
||||
try:
|
||||
# Load Deezer credentials from 'main' under deezer directory
|
||||
deezer_creds_dir = os.path.join('./creds/deezer', main)
|
||||
deezer_creds_path = os.path.abspath(os.path.join(deezer_creds_dir, 'credentials.json'))
|
||||
with open(deezer_creds_path, 'r') as f:
|
||||
deezer_creds = json.load(f)
|
||||
# Initialize DeeLogin with Deezer credentials
|
||||
dl = DeeLogin(
|
||||
arl=deezer_creds.get('arl', ''),
|
||||
email=deezer_creds.get('email', ''),
|
||||
password=deezer_creds.get('password', '')
|
||||
)
|
||||
# Download using download_albumspo
|
||||
dl.download_albumspo(
|
||||
link_album=url,
|
||||
output_dir="./downloads",
|
||||
quality_download="FLAC",
|
||||
recursive_quality=True,
|
||||
recursive_download=False,
|
||||
not_interface=False,
|
||||
make_zip=True,
|
||||
method_save=1
|
||||
)
|
||||
except Exception as e:
|
||||
# If the first attempt fails, use the fallback Spotify main
|
||||
print(f"Failed to download via Deezer fallback: {e}. Trying Spotify fallback main.")
|
||||
# Load fallback Spotify credentials and attempt download
|
||||
try:
|
||||
spo_creds_dir = os.path.join('./creds/spotify', fallback)
|
||||
spo_creds_path = os.path.abspath(os.path.join(spo_creds_dir, 'credentials.json'))
|
||||
spo = SpoLogin(credentials_path=spo_creds_path)
|
||||
spo.download_album(
|
||||
link_album=url,
|
||||
output_dir="./downloads",
|
||||
quality_download="HIGH",
|
||||
recursive_quality=True,
|
||||
recursive_download=False,
|
||||
not_interface=False,
|
||||
method_save=1,
|
||||
make_zip=False
|
||||
)
|
||||
except Exception as e2:
|
||||
# If fallback also fails, raise an error indicating both attempts failed
|
||||
raise RuntimeError(
|
||||
f"Both main (Deezer) and fallback (Spotify) attempts failed. "
|
||||
f"Deezer error: {e}, Spotify error: {e2}"
|
||||
) from e2
|
||||
else:
|
||||
# Original behavior: use Spotify main
|
||||
creds_dir = os.path.join('./creds/spotify', main)
|
||||
credentials_path = os.path.abspath(os.path.join(creds_dir, 'credentials.json'))
|
||||
spo = SpoLogin(credentials_path=credentials_path)
|
||||
spo.download_album(
|
||||
link_album=url,
|
||||
output_dir="./downloads",
|
||||
quality_download="HIGH",
|
||||
recursive_quality=True,
|
||||
recursive_download=False,
|
||||
not_interface=False,
|
||||
method_save=1,
|
||||
make_zip=False
|
||||
)
|
||||
elif service == 'deezer':
|
||||
# Construct Deezer credentials path
|
||||
creds_dir = os.path.join('./creds/deezer', account)
|
||||
# Existing code remains the same, ignoring fallback
|
||||
creds_dir = os.path.join('./creds/deezer', main)
|
||||
creds_path = os.path.abspath(os.path.join(creds_dir, 'credentials.json'))
|
||||
|
||||
# Load Deezer credentials
|
||||
with open(creds_path, 'r') as f:
|
||||
creds = json.load(f)
|
||||
|
||||
# Initialize Deezer client
|
||||
dl = DeeLogin(
|
||||
arl=creds.get('arl', ''),
|
||||
email=creds.get('email', ''),
|
||||
password=creds.get('password', '')
|
||||
)
|
||||
|
||||
# Download Deezer album
|
||||
dl.download_albumdee(
|
||||
link_album=url,
|
||||
output_dir="./downloads/albums",
|
||||
output_dir="./downloads",
|
||||
quality_download="FLAC",
|
||||
recursive_quality=True,
|
||||
recursive_download=False,
|
||||
method_save=1
|
||||
method_save=1,
|
||||
make_zip=False
|
||||
)
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unsupported service: {service}")
|
||||
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
raise
|
||||
raise # Re-raise the exception after logging
|
||||
Reference in New Issue
Block a user