mirror of
https://github.com/bnair123/MusicAnalyser.git
synced 2026-02-25 19:56:06 +00:00
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
import os
|
|
import lyricsgenius
|
|
from typing import Optional, Dict, Any
|
|
|
|
class GeniusClient:
|
|
def __init__(self):
|
|
self.access_token = os.getenv("GENIUS_ACCESS_TOKEN")
|
|
if self.access_token:
|
|
self.genius = lyricsgenius.Genius(self.access_token, verbose=False, remove_section_headers=True)
|
|
else:
|
|
print("WARNING: GENIUS_ACCESS_TOKEN not found. Lyrics enrichment will be skipped.")
|
|
self.genius = None
|
|
|
|
def search_song(self, title: str, artist: str) -> Optional[Dict[str, Any]]:
|
|
"""
|
|
Searches for a song on Genius and returns metadata + lyrics.
|
|
"""
|
|
if not self.genius:
|
|
return None
|
|
|
|
try:
|
|
# Clean up title (remove "Feat.", "Remastered", etc for better search match)
|
|
clean_title = title.split(" - ")[0].split("(")[0].strip()
|
|
song = self.genius.search_song(clean_title, artist)
|
|
|
|
if song:
|
|
return {
|
|
"lyrics": song.lyrics,
|
|
"image_url": song.song_art_image_url,
|
|
"artist_image_url": song.primary_artist.image_url
|
|
}
|
|
except Exception as e:
|
|
print(f"Genius Search Error for {title} by {artist}: {e}")
|
|
|
|
return None
|