lyrics fetcher
This commit is contained in:
parent
d5cb3a48a4
commit
eb745301e5
8 changed files with 277 additions and 23 deletions
51
Flow.py
51
Flow.py
|
|
@ -1,7 +1,11 @@
|
|||
|
||||
from tqdm import tqdm
|
||||
import src.lyrics.lyrics as Lyrics
|
||||
|
||||
from Env import client_id, client_secret, local_library_path
|
||||
from Env import work_dir
|
||||
|
||||
from src.Library import *
|
||||
from src.LibrarySerializer import LibrarySaver, LibraryLoader
|
||||
|
||||
from src.spotify.SpotifyWebAPI import fetch_all
|
||||
|
|
@ -15,8 +19,8 @@ from src.StatGenerator import log_stats
|
|||
|
||||
|
||||
class Flow:
|
||||
spotify_library = None
|
||||
local_library = None
|
||||
spotify_library : Library = None
|
||||
local_library : Library = None
|
||||
|
||||
@staticmethod
|
||||
def fetch_spotify(self):
|
||||
|
|
@ -45,6 +49,49 @@ class Flow:
|
|||
library_parsed = parser.parse(work_dir / "local" / "local_library.json")
|
||||
LibrarySaver(library_parsed).save(work_dir / "local_library.json")
|
||||
|
||||
|
||||
def fetch_and_save_lyrics(self):
|
||||
"""
|
||||
Fetches and writes lyrics for tracks that don't have lyrics yet.
|
||||
Displays stats and uses a progress bar.
|
||||
"""
|
||||
|
||||
# ---------- Step 1: Collect tracks without lyrics ----------
|
||||
tracks_missing_lyrics = [
|
||||
track for track in self.local_library.tracks
|
||||
if len(track.artists) > 0 and not track.has_lyrics
|
||||
]
|
||||
|
||||
total_tracks = len(self.local_library.tracks)
|
||||
missing_count = len(tracks_missing_lyrics)
|
||||
|
||||
print(f"[INFO] Total tracks in library: {total_tracks}")
|
||||
print(f"[INFO] Tracks missing lyrics: {missing_count}")
|
||||
|
||||
if missing_count == 0:
|
||||
print("[INFO] No tracks need lyrics. Exiting.")
|
||||
return
|
||||
|
||||
# ---------- Step 2: Fetch and write lyrics with progress bar ----------
|
||||
for track in tqdm(tracks_missing_lyrics, desc="Fetching lyrics", unit="track"):
|
||||
artist_name = track.artists[0].title
|
||||
album_title = track.album.title if track.album else "Unknown Album"
|
||||
print(f"\n[INFO] Processing: '{track.title}' by '{artist_name}' on '{album_title}'")
|
||||
|
||||
lyrics_text = Lyrics.fetch_lyrics(artist_name, album_title, track.title, track.duration_ms)
|
||||
|
||||
if lyrics_text:
|
||||
print("[SUCCESS] Lyrics found, writing to file...")
|
||||
file_path = local_library_path / track.local_path
|
||||
try:
|
||||
Lyrics.write_lyrics(file_path, lyrics_text)
|
||||
print(f"[SUCCESS] Lyrics written for '{track.title}'")
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Failed to write lyrics for '{track.title}': {e}")
|
||||
else:
|
||||
print(f"[WARNING] Lyrics not found for '{track.title}'")
|
||||
|
||||
|
||||
def load_libraries(self):
|
||||
self.spotify_library = LibraryLoader().load(work_dir / "spotify_library.json")
|
||||
self.local_library = LibraryLoader().load(work_dir / "local_library.json")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue