class Lyrics: def __init__(self): self.genius = Genius(data["geniusAPI"]["token"]) def searchLyrics(self, name): try: song, artist = (name.strip()).split(',') except: song, artist = name.strip(), "" self.song = self.genius.search_song(song, artist) song_info = { "title": self.song.title, "artist": self.song.artist, "lyrics": self.song.lyrics, "thumbnail": self.song.song_art_image_thumbnail_url, "linecount": (self.song.lyrics).count('\n') } return song_info def serachSongByLyrics(self, lyr): request = self.genius.search_lyrics(lyr) ans = [] for hit in request['sections'][0]['hits']: ans.append((hit['result']['full_title']).split(' by')) return ans def saveLyrics(self, songName, songLyrics): with open(f'{songName }.txt', 'w') as f: f.write(songLyrics)
class GetGenius: def __init__(self): # init Genius GENIUS_ACCESS_TOKEN = os.getenv('GENIUS_ACCESS_TOKEN') self.genius = Genius(GENIUS_ACCESS_TOKEN) def get_lyrics(self, query): lyrics = [] results = self.genius.search_lyrics(query) for hit in results['sections'][0]['hits']: if hit['result']['title'][-4:] != 'list': docid = hit['result']['id'] title = str(hit['result']['title']).replace('/', ' ').replace( '?', ' ') artist = hit['result']['primary_artist']['name'] url_img = hit['result']['song_art_image_url'] lyric = self.genius.search_song(title, artist).lyrics lyrics.append([docid, title, artist, url_img, lyric]) return (lyrics)