mansiarora1009 commited on
Commit
e5a7b7c
·
verified ·
1 Parent(s): 55be1fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -36,18 +36,17 @@ def get_song_metadata(video_id):
36
  return None, None
37
 
38
  def clean_song_title(song_title):
39
- """Cleans song title by removing extra info like '|', brackets, and special characters."""
40
- # Remove everything after "|"
41
- song_title = song_title.split("|")[0].strip()
42
- # Remove text inside brackets (e.g., (Live), (Official Video), (Lyrics))
43
- song_title = re.sub(r"\(.*?\)", "", song_title).strip()
44
  return song_title
45
 
46
  def fetch_lyrics(song_title, artist):
47
- """Fetch lyrics for a given song using Genius API."""
48
  try:
49
  # Clean the song title
50
- search_query = clean_song_title(song_title)
 
51
  print(f"🔍 Searching Genius for: {search_query}") # Debugging
52
 
53
  # Search for the song on Genius
@@ -59,14 +58,15 @@ def fetch_lyrics(song_title, artist):
59
  if "response" in search_results and "hits" in search_results["response"]:
60
  hits = search_results["response"]["hits"]
61
 
62
- # Check multiple results to find the best match
63
  for hit in hits:
64
- song_title_genius = hit["result"]["title"]
65
- artist_genius = hit["result"]["primary_artist"]["name"]
66
  song_url = hit["result"]["url"]
67
 
68
- # If the Genius result contains part of the cleaned title, return it
69
- if search_query.lower() in song_title_genius.lower():
 
70
  return f"🔗 [Click here for lyrics]({song_url})"
71
 
72
  return "❌ Error: Lyrics not found on Genius."
@@ -74,6 +74,7 @@ def fetch_lyrics(song_title, artist):
74
  except Exception as e:
75
  return f"❌ Error: {str(e)}"
76
 
 
77
  def translate_text(text):
78
  """Translates text using Google Cloud Translation API"""
79
  print(f"Attempting to translate text: '{text}'") # ADDED: Print input text
 
36
  return None, None
37
 
38
  def clean_song_title(song_title):
39
+ """Cleans the song title by removing extra details for better Genius search."""
40
+ song_title = song_title.split("|")[0].strip() # Remove anything after "|"
41
+ song_title = re.sub(r"\(.*?\)", "", song_title).strip() # Remove (Live) or (Lyrics)
 
 
42
  return song_title
43
 
44
  def fetch_lyrics(song_title, artist):
45
+ """Fetch lyrics for a given song using Genius API with improved search."""
46
  try:
47
  # Clean the song title
48
+ search_query = f"{clean_song_title(song_title)} {artist.split('&')[0].split(',')[0]}".strip()
49
+
50
  print(f"🔍 Searching Genius for: {search_query}") # Debugging
51
 
52
  # Search for the song on Genius
 
58
  if "response" in search_results and "hits" in search_results["response"]:
59
  hits = search_results["response"]["hits"]
60
 
61
+ # Loop through multiple results to find the best match
62
  for hit in hits:
63
+ genius_title = hit["result"]["title"].lower()
64
+ genius_artist = hit["result"]["primary_artist"]["name"].lower()
65
  song_url = hit["result"]["url"]
66
 
67
+ # Check if Genius title contains cleaned song title
68
+ if clean_song_title(song_title).lower() in genius_title:
69
+ print(f"✅ Matched Genius Result: {genius_title} by {genius_artist}")
70
  return f"🔗 [Click here for lyrics]({song_url})"
71
 
72
  return "❌ Error: Lyrics not found on Genius."
 
74
  except Exception as e:
75
  return f"❌ Error: {str(e)}"
76
 
77
+
78
  def translate_text(text):
79
  """Translates text using Google Cloud Translation API"""
80
  print(f"Attempting to translate text: '{text}'") # ADDED: Print input text