@@ -76,7 +76,7 @@ def check_ffmpeg():
7676#Controlla se esiste un file .env e si prepara a caricarne le variabili
7777ensure_config_directory ()
7878if os .path .exists (ENV_PATH ):
79- print (f"File .env trovato in { ENV_PATH } . Caricamento ..." )
79+ print (f"File .env trovato in { ENV_PATH } . Loading ..." )
8080else :
8181 create_env_file ()
8282# Carica le variabili d'ambiente dal file .env
@@ -570,25 +570,55 @@ def load_entries():
570570
571571#La funzione ha lo scopo di assicurarsi che non ci siano ripetizioni in data.dat
572572def clean_entries ():
573- """Rimuove dal file data.dat le voci con cartelle non più esistenti."""
573+ """
574+ Rimuove dal file data.dat le voci con cartelle non più esistenti e quelle playlist
575+ il cui link non è più valido. Se il link della playlist non è valido, stampa il percorso associato.
576+ """
574577 spotify_urls , output_folders = load_entries ()
575578
576- # Filtra solo gli elementi con una cartella esistente
577- valid_entries = [(url , folder ) for url , folder in zip (spotify_urls , output_folders ) if os .path .exists (folder )]
578-
579+ # Configurazione dell'accesso all'API di Spotify (assicurarsi che client_id e client_secret siano definiti)
580+ auth_manager = SpotifyClientCredentials (client_id = client_id , client_secret = client_secret )
581+ sp = spotipy .Spotify (auth_manager = auth_manager )
582+
583+ valid_entries = []
584+
585+ for url , folder in zip (spotify_urls , output_folders ):
586+ # Se la cartella non esiste, salta la voce
587+ if not os .path .exists (folder ):
588+ continue
589+
590+ # Estrae l'ID della playlist dall'URL
591+ playlist_id = None
592+ if "playlist/" in url :
593+ playlist_id = url .split ("playlist/" )[1 ].split ('?' )[0 ]
594+
595+ # Se non riesco a estrarre un ID valido, o se il link non è più valido, stampa un messaggio e salta la voce
596+ if not playlist_id :
597+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"The link is no longer valid for the path: { folder } " )
598+ continue
599+
600+ try :
601+ sp .playlist (playlist_id )
602+ except Exception :
603+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"The link is no longer valid for the directory: { folder } " )
604+ continue
605+
606+ valid_entries .append ((url , folder ))
607+
579608 # Riscrive il file data.dat con solo le voci valide
580609 with open (DATA_FILE , "w" ) as file :
581610 for url , folder in valid_entries :
582611 file .write (f"{ url } { folder } \n " )
583-
584- return valid_entries # Ritorna la lista pulita
612+
613+ return valid_entries
585614
586615#Scrive spotify_url e output_folder nel file data.dat, aggiungendo alla fine se esiste già.
587616def save_entry (spotify_url , output_folder ):
588617
589618 with open (DATA_FILE , "a" ) as file :
590619 file .write (f"{ spotify_url } { output_folder } \n " )
591620
621+
592622def has_content (file ):
593623 """Restituisce 1 se il file data.dat contiene dati, altrimenti 0."""
594624 if not os .path .exists (file ):
@@ -602,20 +632,54 @@ def has_content(file):
602632 return 3 # Il file è vuoto
603633
604634#si occupa del comando update
605- def update ():
635+ def update (playlist_number ):
636+
637+ if playlist_number == 0 : #aggiorna tutte le playlist
638+ result = has_content (DATA_FILE )
639+ if result == 1 :
640+ valid_entries = clean_entries ()
641+ for url , folder in valid_entries :
642+ spotifydl (url , folder , 0 )
643+ print (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Update complete!" )
644+ return
645+ elif result == 3 :
646+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"The playlist database is corrupted." )
647+ elif result == 0 :
648+ print (Fore .YELLOW + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"No playlist has been downloaded yet." )
649+ else :
650+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"Unexpected error." )
651+ else : #aggiorna la playlist playlist_number
652+ try :
653+ with open (DATA_FILE , "r" , encoding = "utf-8" ) as file :
654+ lines = file .readlines ()
655+
656+ if 1 <= playlist_number <= len (lines ):
657+ line = lines [playlist_number - 1 ].strip ()
658+ parts = line .split (" " , 1 ) # Split only at the first space
659+
660+ if len (parts ) == 2 :
661+ url , folder = parts
662+ spotifydl (url , folder , 0 ) #update
663+
664+ #stampa il nome della playlist
665+ auth_manager = SpotifyClientCredentials (client_id = client_id , client_secret = client_secret )
666+ sp = spotipy .Spotify (auth_manager = auth_manager )
667+ if "playlist" in url :
668+ playlist_id = url .split ("/" )[- 1 ].split ("?" )[0 ]
669+ else :
670+ raise ValueError ("Invalid playlist URL" )
671+
672+ playlist_info = sp .playlist (playlist_id )
673+ playlist_name = playlist_info ['name' ]
674+ print (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"{ playlist_name } is now updated" ) #stampa il nome
675+
676+ else :
677+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f" Line { playlist_number } does not contain data in the correct format." )
678+ else :
679+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"Invalid playlist number." )
680+ except FileNotFoundError :
681+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"File Not Found." )
606682
607- result = has_content (DATA_FILE )
608- if result == 1 :
609- valid_entries = clean_entries ()
610- for url , folder in valid_entries :
611- spotifydl (url , folder , 0 )
612- return
613- elif result == 3 :
614- print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"The playlist database is corrupted." )
615- elif result == 0 :
616- print (Fore .YELLOW + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"No playlist has been downloaded yet." )
617- else :
618- print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"Unexpected error." )
619683#si occupa del comando addmeta
620684def addmeta ():
621685 # Richiedi il percorso del file all'utente
@@ -643,9 +707,9 @@ def addmeta():
643707
644708 # Passa il percorso completo del file a add_metadata_to_file
645709 if add_metadata_to_file (file_path , track_info [0 ], directory_path ):
646- print (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Metadata added successfully." )
710+ print (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f "Metadata added successfully." )
647711 else :
648- print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Failed to add metadata." )
712+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f "Failed to add metadata." )
649713 else :
650714 print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"{ spotify_url } is not a valid track link." )
651715 else :
@@ -668,6 +732,45 @@ def settings():
668732 clear_terminal ()
669733 return
670734
735+ def GetList ():
736+ result = has_content (DATA_FILE )
737+ if result == 1 :
738+ ##stampa la lista con i nomi
739+ auth_manager = SpotifyClientCredentials (client_id = client_id , client_secret = client_secret )
740+ sp = spotipy .Spotify (auth_manager = auth_manager )
741+
742+ links = []
743+ try :
744+ with open (DATA_FILE , "r" , encoding = "utf-8" ) as file :
745+ for line in file :
746+ parts = line .strip ().split () # Divide la riga in base agli spazi
747+ if parts : # Verifica che la riga non sia vuota
748+ links .append (parts [0 ]) # Il link è il primo elemento della riga
749+ except FileNotFoundError :
750+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"File Not Found." )
751+ return
752+
753+ ID = 1
754+ for link in links :
755+
756+ if "playlist" in link :
757+ playlist_id = link .split ("/" )[- 1 ].split ("?" )[0 ]
758+ else :
759+ raise ValueError ("Invalid playlist URL" )
760+
761+ playlist_info = sp .playlist (playlist_id )
762+ playlist_name = playlist_info ['name' ]
763+ print (Fore .GREEN + Style .BRIGHT + Style .RESET_ALL + f"{ ID } : { playlist_name } " + Style .RESET_ALL )
764+ ID += 1
765+
766+ return
767+ elif result == 3 :
768+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"The playlist database is corrupted." )
769+ elif result == 0 :
770+ print (Fore .YELLOW + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"No playlist has been downloaded yet." )
771+ else :
772+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"Unexpected error." )
773+ return
671774
672775# === MAIN ===
673776def main ():
@@ -676,6 +779,7 @@ def main():
676779 while True :
677780 rss = input (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Enter command: " ).strip ().lower ()
678781 if rss == "download" :
782+ clear_terminal ()
679783 spotify_url = input (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Enter the Spotify link: " ).strip ()
680784 output_folder = input (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Enter the destination folder: " ).strip ()
681785 spotifydl (spotify_url , output_folder , 1 )
@@ -687,8 +791,9 @@ def main():
687791 elif rss == "help" :
688792 clear_terminal ()
689793 commands = {
690- "download" : "Download any item from Spotify." ,
691- "update" : "Automatically updates all downloaded playlists with the latest changes." ,
794+ "download" : "Download any item from Spotify." ,
795+ "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." ,
796+ "list" : "Show a list of the downloaded playlists" ,
692797 "addMeta" : "Add the metadata of a Spotify song to a specific file" ,
693798 "settings" : "edit the .env file" ,
694799 "exit" : "Closes the program."
@@ -697,18 +802,31 @@ def main():
697802 print ("Comandi disponibili:" )
698803 for command , description in commands .items ():
699804 print (f"- { command } : { description } " )
700-
805+
701806 elif rss == "exit" :
702807 return
703- elif rss == "update" :
704- update ()
705- print (Fore .GREEN + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Update complete!" )
808+ elif rss .startswith ("update" ):
809+ clear_terminal ()
810+ parts = rss .split ()
811+ if len (parts ) == 1 :
812+ update (0 )
813+ elif len (parts ) == 2 and parts [1 ].isdigit ():
814+ playlist_number = int (parts [1 ])
815+ update (playlist_number )
816+ else :
817+ print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + "Invalid update command." )
706818 elif rss == "addmeta" :
819+ clear_terminal ()
707820 addmeta ()
708821 elif rss == "settings" :
822+ clear_terminal ()
709823 settings ()
824+ elif rss == "list" :
825+ clear_terminal ()
826+ GetList ()
710827 else :
711828 print (Fore .RED + Style .BRIGHT + "[SpotifyDl] " + Style .RESET_ALL + f"{ rss } is not a command" )
829+
712830
713831
714832if __name__ == "__main__" :
0 commit comments