-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetLyrics.py
More file actions
179 lines (127 loc) · 5.04 KB
/
Copy pathgetLyrics.py
File metadata and controls
179 lines (127 loc) · 5.04 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from bs4 import BeautifulSoup
import grequests
import requests
from pprint import pprint
CLIENT_ACCESS_TOKEN = '[API_TOKEN]'
def request_song_info(song_title, artist_name):
base_url = '[BASE_URL]'
headers = {'Authorization': 'Bearer ' + CLIENT_ACCESS_TOKEN}
search_url = base_url + '/search'
data = {'q': song_title + ' ' + artist_name}
response = requests.get(search_url, data=data, headers=headers)
return response
def request_song_info_batch(tracks):
base_url = '[BASE_URL]'
headers = {'Authorization': 'Bearer ' + CLIENT_ACCESS_TOKEN}
search_url = base_url + '/search'
batch_data = [{'q': song_title + ' ' + artist_name} for song_title, artist_name in tracks]
reqs = [grequests.get(search_url, data=data, headers=headers) for data in batch_data]
responses = grequests.map(reqs)
return responses
def scrape_song_url(url):
page = requests.get(url)
html = BeautifulSoup(page.text, 'html.parser')
lyrics = html.find('div', class_='lyrics').get_text()
return lyrics
def cleanup_lyrics(lyrics):
ret = []
lines = lyrics.split("\n")
for l in lines:
if "[" not in l:
ret += [l]
return ". ".join(ret)
def flatten_lyrics(lyrics):
return lyrics.replace("\n", " ")
def postprocessing_lyrics(lyrics):
lyrics = lyrics.strip()
lyrics = cleanup_lyrics(lyrics)
lyrics = flatten_lyrics(lyrics)
return lyrics
def get_url_from_genius(track, response):
artist_name = track[1]
json = response.json()
remote_song_info = None
print(json)
print(artist_name)
pprint(json)
for hit in json['response']['hits']:
print('consider', hit['result']['primary_artist']['name'])
pprint(hit)
if artist_name.lower() in hit['result']['primary_artist']['name'].lower():
remote_song_info = hit
break
pprint(remote_song_info)
if remote_song_info is None:
print("Could not find song/artist")
return None
remote_song_title, remote_artist_name = remote_song_info['result']['title'], remote_song_info['result']['primary_artist']['name']
song_url = remote_song_info['result']['url']
return song_url
def get_song_lyrics(song_title, artist_name):
print("getting", song_title, artist_name)
response = request_song_info(song_title, artist_name)
json = response.json()
remote_song_info = None
print(json)
pprint(json)
for hit in json['response']['hits']:
print('consider', hit['result']['primary_artist']['name'])
pprint(hit)
if artist_name.lower() in hit['result']['primary_artist']['name'].lower():
remote_song_info = hit
break
pprint(remote_song_info)
if remote_song_info is None:
print("Could not find song/artist")
return None
remote_song_title, remote_artist_name = remote_song_info['result']['title'], remote_song_info['result']['primary_artist']['name']
song_url = remote_song_info['result']['url']
print("Found song", remote_song_title, remote_artist_name)
print("Song URL:", song_url)
song_lyrics = scrape_song_url(song_url)
song_lyrics = postprocessing_lyrics(song_lyrics)
return song_lyrics
def get_song_lyrics_batch(tracks):
print("-----[START GET BATCH]")
print(tracks)
song_infos = request_song_info_batch(tracks)
song_urls = [get_url_from_genius(tracks[i], song_infos[i]) for i in range(len(tracks))]
temp = []
reqs = []
for i in range(len(song_urls)):
if song_urls[i] is not None:
reqs += [grequests.get(song_urls[i])]
temp += [tracks[i]]
print("len", len(reqs))
responses = grequests.map(reqs)
print("batch responses")
print(responses)
batch_lyrics = []
for i in range(len(responses)):
html = BeautifulSoup(responses[i].text, 'html.parser')
lyrics = html.find('div', class_='lyrics').get_text()
lyrics = postprocessing_lyrics(lyrics)
batch_lyrics += [(tracks[i], lyrics)]
print(lyrics)
break
print("done")
return batch_lyrics
def get_input():
return input("Song title: "), input("Artist name: ")
if __name__ == "__main__":
with open("reference_songs/sad_songs.txt", "r") as f:
lines = f.readlines()
song_list = [x.strip() for x in lines[0::2]]
artist_list = [x.strip() for x in lines[1::2]]
for i in range(len(artist_list)):
print("[GET]", song_list[i], artist_list[i])
filename = "lyrics/" + song_list[i] + "_" + artist_list[i] + ".txt"
song_lyrics = get_song_lyrics(song_list[i], artist_list[i])
if song_lyrics is not None:
with open(filename, "w") as f:
f.write(song_lyrics)
print(song_lyrics)
while True:
song_title, artist_name = get_input()
song_lyrics = get_song_lyrics(song_title, artist_name)
print(song_lyrics)