mirror of
https://github.com/bnair123/MusicAnalyser.git
synced 2026-02-25 11:46:07 +00:00
Fixed and added all the stats_service.py methods
This commit is contained in:
35
backend/app/services/genius_client.py
Normal file
35
backend/app/services/genius_client.py
Normal file
@@ -0,0 +1,35 @@
|
||||
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
|
||||
Reference in New Issue
Block a user