-
Notifications
You must be signed in to change notification settings - Fork 0
/
songLyrics.py
62 lines (51 loc) · 1.91 KB
/
songLyrics.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from ytmusicapi import YTMusic
import lyricsgenius as lg
class SongLyrics:
def __init__(self, playlist):
self.playlist = playlist
music = YTMusic()
final_list = []
genius = lg.Genius('Client_Access_Token_Goes_Here', skip_non_songs=True, excluded_terms=["(Remix)", "(Live)"],
remove_section_headers=True)
def get_lyrics_from_genius(self, title, author):
try:
song = self.genius.search_song(artist=author, title=title)
except:
return []
if song == None:
return []
return song.lyrics.lower()
def get_lyrics_of_songs(self):
playlist = ""
while True:
try:
playlist = self.music.get_playlist(playlistId=self.playlist, limit=4)
break
except:
print("connection error, trying again")
tracks = playlist.get('tracks')
for track in tracks:
title = ""
author = ""
song_details = []
# create helper list to put title and lyrics together into final_list
title = track["title"]
author = track["artists"][0]
author = list(author.items())[0][1]
index = title.find('(')
if index == -1:
index = title.find("[")
lyrics = []
if index > 0:
lyrics = self.get_lyrics_from_genius(title[0:index], author)
else:
lyrics = self.get_lyrics_from_genius(title, author)
if not lyrics == [] and lyrics is not None:
song_details.append(author)
song_details.append(title)
song_details.append(lyrics)
# putting together
self.final_list.append(song_details)
if len(self.final_list) == 10:
return self.final_list
return self.final_list