-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiThreadsSpotify.py
903 lines (775 loc) · 42.7 KB
/
MultiThreadsSpotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
# Importazioni della Libreria Standard
import os # Per interagire con il sistema operativo (es. gestione file, variabili d'ambiente).
import re # Per le espressioni regolari, utili per il matching di pattern nelle stringhe.
import uuid # Per generare identificatori unici (ad esempio, per creare nomi file univoci).
import threading # Per lavorare con i thread in applicazioni multithread.
import shutil # Per operazioni sui file come copiare, spostare ed eliminare file.
import sys # Per interagire con il sistema e gestire gli argomenti da riga di comando o terminare il programma.
import time # Per operazioni legate al tempo (ad esempio, mettere in pausa il programma, misurare il tempo).
from pathlib import Path # Per gestire e manipolare i percorsi dei file in modo più comodo.
from concurrent.futures import ThreadPoolExecutor, as_completed # Per eseguire operazioni in parallelo usando thread (ThreadPoolExecutor) e gestire i risultati (as_completed).
# Importazioni di Librerie di Terze Parti
import spotipy # Per interagire con l'API Web di Spotify, principalmente per ottenere informazioni su tracce e playlist.
import yt_dlp # Una libreria per il download di contenuti da YouTube e altre piattaforme.
from spotipy.oauth2 import SpotifyClientCredentials # Per gestire l'autenticazione di Spotify usando le credenziali client.
from dotenv import load_dotenv # Per caricare variabili d'ambiente da un file .env, utile per memorizzare dati sensibili come le API key.
from mutagen.easyid3 import EasyID3 # Per leggere e scrivere metadati (tag ID3) nei file MP3.
from mutagen.mp3 import MP3 # Per lavorare con file MP3 (es. ottenere proprietà come la durata).
from colorama import Fore, Style # Per colorare il testo nel terminale e applicare stili (utile per formattare l'output nelle CLI).
#Variabili globali rigurdanti la cartella dell'applicazione
APP_NAME = "SpotifyDl"
DEFAULT_ENV_NAME = ".env"
CONFIG_FOLDER = os.path.join(os.path.expanduser("~"), f".{APP_NAME}")
ENV_PATH = os.path.join(CONFIG_FOLDER, DEFAULT_ENV_NAME)
#Controlla se la cartella della configurazione iniziale esiste
def ensure_config_directory():
if not os.path.exists(CONFIG_FOLDER):
os.makedirs(CONFIG_FOLDER)
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Configuration folder created: {CONFIG_FOLDER}")
#crea il file env per la prima esecuzione del programma con tutte le impostazioni da modificare
def create_env_file():
#Chiede gli input all'utente
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f".env file not found. Creating a new one.")
spotify_client_id = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter SPOTIFY_CLIENT_ID: ").strip() #mi assicuro non ci siano spazi indesiderati
spotify_client_secret = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter SPOTIFY_CLIENT_SECRET: ").strip()
max_threads = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter MAX_THREADS: ").strip()
preferred_quality = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter PREFERRED_QUALITY: ").strip()
cache_yt = os.path.join(CONFIG_FOLDER, "yt-cache")
#Compila ciò che va scritto nel .env
env_content = f"""
SPOTIFY_CLIENT_ID={spotify_client_id}
SPOTIFY_CLIENT_SECRET={spotify_client_secret}
MAX_THREADS={max_threads}
PREFERRED_QUALITY={preferred_quality}
XDG_CACHE_HOME={cache_yt}
"""
with open(ENV_PATH, "w") as f: #crea il .env se manca e lo riempie con ciò di cui ha bisogno per far funzionare il programma
f.write(env_content.strip())
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f".env file created at {ENV_PATH}")
#funzione per pulire il terminale, dovrebbe funzionare sia su WIN sia su MACOS
def clear_terminal():
"""Pulisce il terminale a seconda del sistema operativo."""
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
#Controla se FFmpeg è installato correttamente nel sistema
def check_ffmpeg():
if shutil.which("ffmpeg") is None:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"FFmpeg is not installed or not in the PATH.")
print("Download it from: https://ffmpeg.org/download.html and install it.")
input("\nPress Enter to exit...")
sys.exit(1) # Termina l'app con errore
else:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"FFmpeg is installed in the PATH.")
clear_terminal()
#Controlla se esiste un file .env e si prepara a caricarne le variabili
ensure_config_directory()
if os.path.exists(ENV_PATH):
print(f"File .env trovato in {ENV_PATH}. Loading...")
else:
create_env_file()
# Carica le variabili d'ambiente dal file .env
load_dotenv(ENV_PATH, override=True)
client_id = os.getenv("SPOTIFY_CLIENT_ID")
client_secret = os.getenv("SPOTIFY_CLIENT_SECRET")
max_threads = int(os.getenv("MAX_THREADS", "4"))
print("Configurazione caricata.")
clear_terminal()
#---INIZIO CODICE VECCHIO--- Questa parte del codice forza l'utilizzo del file mp3 in quanto ci sono dei problemi nell'implementare altri tipi di file.
codec = "mp3" #Il supporto a codec diversi non è al momento dispobile
codec = '.' + codec if not codec.startswith('.') else codec # Aggiungiamo il punto se manca
#---FINE CODICE VECCHIO---
#set dati file scaricati, una sorta di database delle playlist scaricate
DATA_FILE = os.path.join(CONFIG_FOLDER, "data.dat")
# Lock per operazioni critiche sui file
file_lock = threading.Lock()
# Set globale per tracce in elaborazione (chiave: (titolo, artista) in lowercase)
in_processing = set()
#è la funzione che ha il compito di scrivere i log sui file, è scritta in questo modo per proteggersi da eventuali problemi di accessi di più threads al file contemporaneamente
def log_error(message, output_folder):
"""Scrive un messaggio di errore nel file log.txt nella cartella di output."""
log_file = os.path.join(output_folder, "log.txt")
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
with open(log_file, "a", encoding="utf-8") as f:
f.write(f"[{timestamp}] {message}\n")
#Funzione che si occupa di prendere le informazioni della playlist
def get_spotify_playlist_tracks(playlist_url, caller):
"""Ottiene la lista dei brani da una playlist di Spotify."""
auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
if "playlist" in playlist_url:
playlist_id = playlist_url.split("/")[-1].split("?")[0]
else:
raise ValueError("Invalid playlist URL")
tracks = []
offset = 0
playlist_info = sp.playlist(playlist_id)
playlist_name = playlist_info['name']
clear_terminal()
if caller == 1:
print(Fore.GREEN + Style.BRIGHT + f"You're downloading from: {playlist_name}" + Style.RESET_ALL)
while True:
results = sp.playlist_tracks(playlist_id, offset=offset)
for item in results['items']:
track = item['track']
if track:
name = track.get('name', 'Unknown Track')
artists = ', '.join([artist.get('name', 'Unknown Artist') for artist in track.get('artists', [])])
album = track.get('album', {}).get('name', 'Unknown Album')
track_number = track.get('track_number', None)
release_date = track['album'].get('release_date', 'Unknown Year')
year = release_date.split("-")[0]
tracks.append({
'name': name,
'artists': artists,
'album': album,
'track_number': track_number,
'year': year
})
else:
if caller == 1:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Track is None, skipping...")
if len(results['items']) < 100:
break
offset += 100
return tracks
#Funzione che si occupa di prendere le informazioni degli album
def get_spotify_album_tracks(album_url):
"""Ottiene la lista dei brani da un album di Spotify."""
auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
if "album" in album_url:
album_id = album_url.split("/")[-1].split("?")[0]
else:
raise ValueError("Invalid album URL")
album_info = sp.album(album_id)
album_name = album_info.get('name', 'Unknown Album')
clear_terminal()
print(Fore.GREEN + Style.BRIGHT + f"You're downloading from Album: {album_name}" + Style.RESET_ALL)
tracks = []
for track in album_info['tracks']['items']:
name = track.get('name', 'Unknown Track')
artists = ', '.join([artist.get('name', 'Unknown Artist') for artist in track.get('artists', [])])
track_number = track.get('track_number', None)
release_date = album_info.get('release_date', 'Unknown Year')
year = release_date.split("-")[0]
tracks.append({
'name': name,
'artists': artists,
'album': album_name,
'track_number': track_number,
'year': year
})
return tracks
#Funzione che si occupa di prendere le informazioni di una singola traccia
def get_spotify_single_track(track_url):
"""Ottiene le informazioni di un singolo brano di Spotify."""
auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
if "track" in track_url:
track_id = track_url.split("/")[-1].split("?")[0]
else:
raise ValueError("Invalid track URL")
track = sp.track(track_id)
name = track.get('name', 'Unknown Track')
artists = ', '.join([artist.get('name', 'Unknown Artist') for artist in track.get('artists', [])])
album = track.get('album', {}).get('name', 'Unknown Album')
track_number = track.get('track_number', None)
release_date = track.get('album', {}).get('release_date', 'Unknown Year')
year = release_date.split("-")[0]
clear_terminal()
print(Fore.GREEN + Style.BRIGHT + f"You're downloading track: {name}" + Style.RESET_ALL)
return [{
'name': name,
'artists': artists,
'album': album,
'track_number': track_number,
'year': year
}]
#Funzione di supporto per tutto quello che è gia stato scaricato
def track_already_downloaded(track, output_folder):
"""
Controlla se un brano è già presente nella cartella, verificando i metadati (titolo e artista).
Vengono controllati solo i file finali (quelli con metadati).
"""
for file in Path(output_folder).glob(f"*{codec}"):
try:
audio = MP3(str(file), ID3=EasyID3)
title = audio.get('title', [None])[0]
artist = audio.get('artist', [None])[0]
if title and artist:
if title.strip().lower() == track['name'].strip().lower() and artist.strip().lower() == track['artists'].strip().lower():
return True
except Exception:
continue
return False
#Funzione nella quale avviene la composizione della query per ricercare le canzoni
def search_youtube(query, output_folder):
"""
Cerca un video su YouTube utilizzando yt-dlp.
Prova prima con la query originale e, in caso di errore (ad esempio 403), prova ad aggiungere "lyrics".
Restituisce l'URL del primo risultato disponibile.
"""
ydl_opts = {
'quiet': True,
'default_search': 'ytsearch5',
'skip_download': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(query, download=False)
except Exception as e:
log_error(f"YouTube search error for query '{query}': {e}", output_folder)
info = None
if info and 'entries' in info and len(info['entries']) > 0:
for entry in info['entries']:
webpage_url = entry.get('webpage_url')
if webpage_url:
return webpage_url
# Se la ricerca con la query originale non ha prodotto risultati, prova con "lyrics"
alt_query = query + " lyrics"
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(alt_query, download=False)
except Exception as e:
log_error(f"YouTube search error for alternative query '{alt_query}': {e}", output_folder)
return None
if info and 'entries' in info and len(info['entries']) > 0:
for entry in info['entries']:
webpage_url = entry.get('webpage_url')
if webpage_url:
return webpage_url
return None
# === FASE 1: Download dei file (senza metadati e senza rinomina) ===
def download_track(track, output_folder):
"""
Scarica il brano da YouTube e restituisce il percorso del file temporaneo.
Se il brano è già presente (verificato sui file finali) o in elaborazione, ritorna None.
"""
#Il codice verifica se una traccia è già in fase di elaborazione o se è stata scaricata. Se sì, la salta. Altrimenti, crea una query di ricerca su YouTube per la traccia e l'artista. Se non trova il video su YouTube, registra l'errore e continua.
key = (track['name'].strip().lower(), track['artists'].strip().lower())
if key in in_processing or track_already_downloaded(track, output_folder):
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Skipping, already exists or in processing: {track['name']} - {track['artists']}")
return None
in_processing.add(key)
query = f"{track['name']} \"{track['artists']}\""
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Searching: {query}")
youtube_url = search_youtube(query, output_folder)
if not youtube_url:
log_error(f"Not found on YouTube: {query}", output_folder)
in_processing.remove(key)
return None
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Downloading from: {youtube_url}")
temp_name = uuid.uuid4().hex
temp_output_path = Path(output_folder) / temp_name
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': "mp3", #hardcoded mp3 file format, si come ho problemi con il supporto di altri tipi di file
'preferredquality': os.getenv("PREFERRED_QUALITY", "192"),
}],
'outtmpl': str(temp_output_path),
'quiet': True,
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url]) #qui avviene l'effettivo download delle tracce
except Exception as e:
log_error(f"Download error for {track['name']}: {e}", output_folder)
in_processing.remove(key)
return None
temp_file = str(temp_output_path) + codec
if not os.path.exists(temp_file):
log_error(f"Temporary file not found for {track['name']}", output_folder)
in_processing.remove(key)
return None
return temp_file
# === FASE 2: Aggiunta metadati ===
def add_metadata_to_file(temp_file, track_info, output_folder):
"""
Aggiunge i metadati al file audio in base al codec scelto.
Ritorna True se va a buon fine, False altrimenti.
"""
try:
audio = MP3(temp_file, ID3=EasyID3)
audio['title'] = track_info['name']
audio['artist'] = track_info['artists']
audio['album'] = track_info['album']
#audio["comment"] = "SpotifyDl"
if track_info['track_number']:
audio['tracknumber'] = str(track_info['track_number'])
audio.save()
del audio # Rilascia la risorsa
return True
except Exception as e:
log_error(f"Error adding metadata for {track_info['name']}: {e}", output_folder)
return False
# === FASE 3: Rinomina del file ===
def rename_file(temp_file, track_info, output_folder):
"""
Rinomina il file temporaneo in base al titolo del brano.
Se esiste già un file con quel nome, prova a usare "nome brano - nome autore".
Se anche questo esiste, aggiunge un suffisso numerico.
Ritorna il percorso finale del file.
Implementa un meccanismo di retry se il file è in uso.
"""
final_name = re.sub(r'[\/:*?."<>|]', " ", track_info['name']).strip().rstrip('.')
final_output_path = Path(output_folder) / final_name
final_file = str(final_output_path) + codec
max_retries = 5
for attempt in range(max_retries):
try:
with file_lock:
if not os.path.exists(final_file):
os.rename(temp_file, final_file)
else:
# Prova con "nome brano - nome autore"
alt_final_name = f"{final_name} - {track_info['artists']}"
alt_final_name = re.sub(r'[\/:*?."<>|]', " ", alt_final_name).strip().rstrip('.')
alt_final_output_path = Path(output_folder) / alt_final_name
alt_final_file = str(alt_final_output_path) + codec
if not os.path.exists(alt_final_file):
os.rename(temp_file, alt_final_file)
final_file = alt_final_file
else:
# Fallback: aggiungi un suffisso numerico
i = 1
while os.path.exists(f"{final_output_path}-{i}{codec}"):
i += 1
final_file = f"{final_output_path}-{i}{codec}"
os.rename(temp_file, final_file)
break
except OSError as e:
if hasattr(e, "winerror") and e.winerror == 32:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"File in use, retrying rename for {final_file} (attempt {attempt+1}/{max_retries})")
time.sleep(0.5)
else:
raise e
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Failed to rename {temp_file} after {max_retries} attempts.")
return None
# Post-rinominazione: controlla che il nome del file corrisponda ai metadati
try:
if os.path.exists(final_file):
audio = MP3(final_file, ID3=EasyID3)
metadata_title = audio.get('title', [final_name])[0]
sanitized_title = re.sub(r'[\/:*?."<>|]', " ", metadata_title).strip().rstrip('.')
current_name = Path(final_file).stem
if sanitized_title != current_name:
new_final_output_path = Path(output_folder) / sanitized_title
new_final_file = str(new_final_output_path) + codec
if not os.path.exists(new_final_file):
os.rename(final_file, new_final_file)
final_file = new_final_file
else:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Post-rename target already exists: {new_final_file}. Keeping original file.")
except Exception as e:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Error in post-renaming check for {track_info['name']}: {e}")
error_file = os.path.join(output_folder, "Error.txt")
with open(error_file, 'a') as file:
file.write(f"[ERROR] Post-renaming check for {track_info['name']}: {e}\n")
return final_file
def finalize_track_processing(track):
"""Rimuove la traccia dal set in_processing."""
key = (track['name'].strip().lower(), track['artists'].strip().lower())
in_processing.discard(key)
# === FASE 4: Verifica finale e correzione ===
def phase4_verification(output_folder):
"""
Controlla tutti i file nella cartella di output:
- Per ogni file .mp3, se mancano i metadati (title o artist), li imposta:
* Usa il nome del file come title.
* Imposta "Unknown" come artist se mancante.
"""
for file in Path(output_folder).iterdir():
if not file.is_file():
continue
# Processa solo i file con estensione .mp3
if file.suffix.lower() != ".mp3":
continue
try:
audio = MP3(str(file), ID3=EasyID3)
title = audio.get('title', [None])[0]
artist = audio.get('artist', [None])[0]
changed = False
if not title:
default_title = file.stem
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL +
f"File {file} missing title. Setting title to '{default_title}'")
audio['title'] = default_title
changed = True
if not artist:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL +
f"File {file} missing artist. Setting artist to 'Unknown'")
audio['artist'] = "Unknown"
changed = True
if changed:
audio.save()
except Exception as e:
log_error(f"Error processing file {file}: {e}", output_folder)
#si occupa del comando download
def spotifydl(spotify_url, output_folder, flag):
if not output_folder:
output_folder = "/app/downloads"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
clear_terminal()
# Estrae le tracce in base al tipo di URL e ne verifica la correttezza
if "playlist" in spotify_url:
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Extracting tracks from the playlist...")
tracks = get_spotify_playlist_tracks(spotify_url, 1)
if flag == 1:
save_entry(spotify_url, output_folder)
elif "album" in spotify_url:
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Extracting tracks from the album...")
tracks = get_spotify_album_tracks(spotify_url)
elif "track" in spotify_url:
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Extracting track information...")
tracks = get_spotify_single_track(spotify_url)
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Unsupported Spotify URL.")
return #in caso di errore esce dalla funzione
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"The songs will be saved in: {output_folder}")
# Deduplica le tracce basandosi su (titolo, artista)
unique_tracks = []
seen = set()
for track in tracks:
key = (track['name'].strip().lower(), track['artists'].strip().lower())
if key not in seen:
unique_tracks.append(track)
seen.add(key)
tracks = unique_tracks
print("\n=== PHASE 1: Download tracks ===")
downloaded_items = [] # Lista di dict: { 'track': ..., 'temp_file': ... }
with ThreadPoolExecutor(max_threads) as executor:
future_to_track = {executor.submit(download_track, track, output_folder): track for track in tracks}
for future in as_completed(future_to_track):
track = future_to_track[future]
try:
temp_file = future.result()
if temp_file:
downloaded_items.append({"track": track, "temp_file": temp_file})
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Downloaded: {track['name']} - Temp file: {temp_file}")
except Exception as e:
log_error(f"Error downloading track {track['name']}: {e}", output_folder)
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Error downloading track {track['name']}: {e}")
print("\n=== PHASE 2: Adding metadata ===")
with ThreadPoolExecutor(max_threads) as executor:
future_to_item = {executor.submit(add_metadata_to_file, item["temp_file"], item["track"], output_folder): item for item in downloaded_items}
for future in as_completed(future_to_item):
item = future_to_item[future]
try:
success = future.result()
if success:
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Metadata added for: {item['track']['name']}")
else:
log_error(f"Error adding metadata for: {item['track']['name']}", output_folder)
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Error adding metadata for: {item['track']['name']}")
except Exception as e:
log_error(f"Error adding metadata for {item['track']['name']}: {e}", output_folder)
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Error adding metadata for {item['track']['name']}: {e}")
print("\n=== PHASE 3: Renaming files ===")
with ThreadPoolExecutor(max_threads) as executor:
future_to_item = {executor.submit(rename_file, item["temp_file"], item["track"], output_folder): item for item in downloaded_items}
for future in as_completed(future_to_item):
item = future_to_item[future]
try:
final_path = future.result()
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"File for {item['track']['name']} renamed to: {final_path}")
except Exception as e:
log_error(f"Error renaming file for {item['track']['name']}: {e}", output_folder)
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Error renaming file for {item['track']['name']}: {e}")
finally:
finalize_track_processing(item["track"])
print("\n=== PHASE 4: Final verification ===")
phase4_verification(output_folder)
clear_terminal()
def load_entries():
"""Legge tutte le righe del file e salva spotify_url e output_folder in due liste."""
spotify_urls = []
output_folders = []
if not os.path.exists(DATA_FILE):
return spotify_urls, output_folders # Se il file non esiste, ritorna liste vuote
with open(DATA_FILE, "r") as file:
for line in file:
parts = line.strip().split(" ", 1) # Divide in due parti: URL e cartella
if len(parts) == 2:
spotify_urls.append(parts[0])
output_folders.append(parts[1])
return spotify_urls, output_folders
#La funzione ha lo scopo di assicurarsi che non ci siano ripetizioni in data.dat
def clean_entries():
"""
Rimuove dal file data.dat le voci con cartelle non più esistenti e quelle playlist
il cui link non è più valido. Se il link della playlist non è valido, stampa il percorso associato.
"""
spotify_urls, output_folders = load_entries()
# Configurazione dell'accesso all'API di Spotify (assicurarsi che client_id e client_secret siano definiti)
auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
valid_entries = []
for url, folder in zip(spotify_urls, output_folders):
# Se la cartella non esiste, salta la voce
if not os.path.exists(folder):
continue
# Estrae l'ID della playlist dall'URL
playlist_id = None
if "playlist/" in url:
playlist_id = url.split("playlist/")[1].split('?')[0]
# Se non riesco a estrarre un ID valido, o se il link non è più valido, stampa un messaggio e salta la voce
if not playlist_id:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"The link is no longer valid for the path: {folder}")
continue
try:
sp.playlist(playlist_id)
except Exception:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"The link is no longer valid for the directory: {folder}")
continue
valid_entries.append((url, folder))
# Riscrive il file data.dat con solo le voci valide
with open(DATA_FILE, "w") as file:
for url, folder in valid_entries:
file.write(f"{url} {folder}\n")
return valid_entries
#Scrive spotify_url e output_folder nel file data.dat, aggiungendo alla fine se esiste già.
def save_entry(spotify_url, output_folder):
with open(DATA_FILE, "a") as file:
file.write(f"{spotify_url} {output_folder}\n")
def has_content(file):
"""Restituisce 1 se il file data.dat contiene dati, altrimenti 0."""
if not os.path.exists(file):
return 0 # Il file non esiste, quindi è vuoto
# Controlla se il file ha contenuto
with open(file, "r") as file:
for line in file:
if line.strip(): # Se c'è almeno una riga non vuota
return 1
return 3 # Il file è vuoto
def get_file_metadata(mp3_file):
"""Legge il titolo e l'artista dal metadato ID3 del file MP3."""
try:
audio = MP3(mp3_file, ID3=EasyID3)
title = audio.get("title", [None])[0] # Prende il primo valore della lista
artist = audio.get("artist", [None])[0] # Prende il primo valore della lista
return title, artist # Restituisce una tupla con titolo e artista
except Exception:
return None, None # Se non può leggere i metadati, restituisce None per entrambi
def check_playlist_files(playlist_url, folder):
"""Controlla se i file della cartella corrispondono ai brani della playlist."""
# Ottieni la lista dei brani tramite la funzione aggiornata
playlist_tracks_dict = get_spotify_playlist_tracks(playlist_url,0)
found_tracks_lower = set()
for file in os.listdir(folder):
if file.endswith(".mp3"):
file_path = os.path.join(folder, file)
track_title, track_artist = get_file_metadata(file_path)
if track_title:
track_title_lower = track_title.lower()
# Confronta il nome del file con i titoli della playlist ottenuti dalle API di Spotify
match = next((track for track in playlist_tracks_dict
if track['name'].lower() == track_title_lower and track['artists'].lower() == track_artist.lower()), None)
if match:
found_tracks_lower.add(track_title_lower)
else:
choice = input(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL +
f"The song '{file}' is not in the playlist. Do you want to delete it? (y/n): ").strip().lower()
if choice == "y":
os.remove(file_path)
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Song '{file}' deleted.")
clear_terminal()
else:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL +
f"The file '{file}' does not have a title metadata. I recommend adding it.")
missing_tracks = [track['name'] for track in playlist_tracks_dict if track['name'].lower() not in found_tracks_lower]
if missing_tracks:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "These songs are missing from the folder:")
for track in missing_tracks:
print(f" - {track['name']} by {track['artists']}")
else:
return
# Trova i brani mancanti utilizzando il confronto normalizzato
missing_tracks = [original for lower, original in playlist_tracks_dict.items() if lower not in found_tracks_lower]
if missing_tracks:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "These songs are missing from the folder:")
for track in missing_tracks:
print(f" - {track}")
return
#si occupa del comando update
def update(playlist_number):
if playlist_number == 0: #aggiorna tutte le playlist
result = has_content(DATA_FILE)
if result == 1:
valid_entries = clean_entries()
for url, folder in valid_entries:
spotifydl(url, folder, 0)
check_playlist_files(url, folder)
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Update complete!")
return
elif result == 3:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"The playlist database is corrupted.")
elif result == 0:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"No playlist has been downloaded yet.")
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Unexpected error.")
else: #aggiorna la playlist playlist_number
try:
with open(DATA_FILE, "r", encoding="utf-8") as file:
lines = file.readlines()
if 1 <= playlist_number <= len(lines):
line = lines[playlist_number - 1].strip()
parts = line.split(" ", 1) # Split only at the first space
if len(parts) == 2:
url, folder = parts
spotifydl(url, folder, 0) #update
check_playlist_files(url, folder)
#stampa il nome della playlist
auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
if "playlist" in url:
playlist_id = url.split("/")[-1].split("?")[0]
else:
raise ValueError("Invalid playlist URL")
playlist_info = sp.playlist(playlist_id)
playlist_name = playlist_info['name']
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"{playlist_name} is now updated") #stampa il nome
return
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f" Line {playlist_number} does not contain data in the correct format.")
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Invalid playlist number.")
except FileNotFoundError:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"File Not Found.")
#si occupa del comando addmeta
def addmeta():
# Richiedi il percorso del file all'utente
file_path = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Enter the file path: ").strip()
# Rimuovi eventuali virgolette iniziali e finali (sia " che ')
if (file_path.startswith('"') and file_path.endswith('"')) or (file_path.startswith("'") and file_path.endswith("'")):
file_path = file_path[1:-1]
# Controlla se il file esiste
if os.path.exists(file_path):
# Richiedi il link Spotify
spotify_url = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Enter the Spotify link: ").strip()
# Rimuovi virgolette se presenti
if (spotify_url.startswith('"') and spotify_url.endswith('"')) or (spotify_url.startswith("'") and spotify_url.endswith("'")):
spotify_url = spotify_url[1:-1]
# Ottieni il percorso della cartella in cui si trova il file
directory_path = os.path.dirname(file_path)
# Se il link contiene "track", procedi con l'estrazione dei metadati
if "track" in spotify_url:
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Extracting track information...")
track_info = get_spotify_single_track(spotify_url)
# Passa il percorso completo del file a add_metadata_to_file
if add_metadata_to_file(file_path, track_info[0], directory_path):
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Metadata added successfully.")
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Failed to add metadata.")
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"{spotify_url} is not a valid track link.")
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"{file_path} does not exist")
#si occupa del comando settings
def settings():
sure = input(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Are you sure you want to modify the settings? You will have to overwrite them all at once, including the Spotify ID and Secret.(y/n): ").lower()
if sure == 'y':
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"edit .env")
spotify_client_id = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter SPOTIFY_CLIENT_ID: ")
spotify_client_secret = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter SPOTIFY_CLIENT_SECRET: ")
max_threads = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter MAX_THREADS: ")
preferred_quality = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Enter PREFERRED_QUALITY: ")
os.environ['SPOTIFY_CLIENT_ID'] = spotify_client_id
os.environ['SPOTIFY_CLIENT_SECRET'] = spotify_client_secret
os.environ['MAX_THREADS'] = max_threads
os.environ['PREFERRED_QUALITY'] = preferred_quality
else:
clear_terminal()
return
def GetList():
result = has_content(DATA_FILE)
if result == 1:
##stampa la lista con i nomi
auth_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(auth_manager=auth_manager)
links = []
try:
with open(DATA_FILE, "r", encoding="utf-8") as file:
for line in file:
parts = line.strip().split() # Divide la riga in base agli spazi
if parts: # Verifica che la riga non sia vuota
links.append(parts[0]) # Il link è il primo elemento della riga
except FileNotFoundError:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"File Not Found.")
return
ID = 1
for link in links:
if "playlist" in link:
playlist_id = link.split("/")[-1].split("?")[0]
else:
raise ValueError("Invalid playlist URL")
playlist_info = sp.playlist(playlist_id)
playlist_name = playlist_info['name']
print(Fore.GREEN + Style.BRIGHT + Style.RESET_ALL + f"{ID}: {playlist_name}" + Style.RESET_ALL)
ID += 1
return
elif result == 3:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"The playlist database is corrupted.")
elif result == 0:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"No playlist has been downloaded yet.")
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"Unexpected error.")
return
# === MAIN ===
def main():
check_ffmpeg()
print("Welcome to SpotifyDl. To see the available commands, type help")
while True:
rss = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Enter command: ").strip().lower()
if rss == "download" :
clear_terminal()
spotify_url = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Enter the Spotify link: ").strip()
output_folder = input(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Enter the destination folder: ").strip()
spotifydl(spotify_url, output_folder, 1)
print(Fore.GREEN + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Download complete!")
log_file = os.path.join(output_folder, "log.txt")
if has_content(log_file) == 1:
print(Fore.YELLOW + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"An error may have occurred. Please check the log file. If there are incorrect or incomplete files, delete the file and enter the \\update command to attempt to repair the playlist. If the error persists, the track cannot be downloaded.")
elif rss == "help":
clear_terminal()
commands = {
"download": "Download any item from Spotify.",
"update <Playlist Number>": "Update a specific playlist using the number obtained from the list. If you don't enter a number, all playlists will be updated automatically with the latest changes.",
"list": "Show a list of the downloaded playlists",
"addMeta": "Add the metadata of a Spotify song to a specific file",
"settings": "edit the .env file",
"exit": "Closes the program."
}
print("Comandi disponibili:")
for command, description in commands.items():
print(f"- {command}: {description}")
elif rss == "exit":
return
elif rss.startswith("update"):
clear_terminal()
parts = rss.split()
if len(parts) == 1:
update(0)
elif len(parts) == 2 and parts[1].isdigit():
playlist_number = int(parts[1])
update(playlist_number)
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + "Invalid update command.")
elif rss == "addmeta":
clear_terminal()
addmeta()
elif rss == "settings":
clear_terminal()
settings()
elif rss == "list":
clear_terminal()
GetList()
else:
print(Fore.RED + Style.BRIGHT + "[SpotifyDl] " + Style.RESET_ALL + f"{rss} is not a command")
if __name__ == "__main__":
main() #si assicura sia l'utente ad aprire lo script