-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyrics.py
25 lines (22 loc) · 820 Bytes
/
lyrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import lyricsgenius
from config import GENIUS_API_TOKEN
genius = lyricsgenius.Genius(GENIUS_API_TOKEN)
def clean_lyrics(lyrics):
lines = lyrics.split('\n')
cleaned_lines = []
for line in lines:
if "You might also like" in line:
break
if not line.startswith('Embed') and not 'Contributors' in line and not line.isdigit():
cleaned_lines.append(line)
return '\n'.join(cleaned_lines)
def complete_lyric(text):
try:
song = genius.search_song(text)
if song:
cleaned_lyrics = clean_lyrics(song.lyrics)
return song.artist, song.title, cleaned_lyrics
else:
return None, None, "Sorry, I couldn't find the lyrics for this song."
except Exception as e:
return None, None, f"An error occurred: {e}"