-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
268 lines (205 loc) · 10.3 KB
/
util.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from platform import release
from bs4 import BeautifulSoup
import unidecode
import contractions
from nltk.stem import WordNetLemmatizer
from num2words import num2words
import requests, json, re
from urllib.parse import quote_plus
from lxml import etree
from fuzzywuzzy import fuzz
import time
# is this the right way?
wordnet_lemmatizer = WordNetLemmatizer()
def lemmatizer(text):
lemm_text = [wordnet_lemmatizer.lemmatize(word) for word in text]
return lemm_text
def convertNums(text):
convNums = [num2words(word) for word in text if word.isdigit()]
return convNums
def strip_html_tags(text):
"""remove html tags from text"""
soup = BeautifulSoup(text, 'html.parser')
stripped_text = soup.get_text(separator=" ")
return stripped_text
def remove_accented_chars(text):
"""remove accented characters from text, e.g. café, unicode decode removes non utf-8 characters"""
text = unidecode.unidecode(text)
return text
def expand_contractions(text):
"""expand shortened words, e.g. don't = do not"""
text = contractions.fix(text)
return text
def getGenre(sp, artistName):
searchResults = sp.search(q=artistName, type='artist', limit = 1)
# if doesn't exist = None
if len(searchResults['artists']['items']) == 0:
dictArt = {'id':None, 'genres':None}
# else enter desired fields into dataframe
else:
dictArt = {'id':searchResults['artists']['items'][0]['id'], 'genres':', '.join(searchResults['artists']['items'][0]['genres'])}
return dictArt
def getFeatures(sp, artistName, trackName, spotifyFeatures):
searchResults = sp.search(q=artistName + ' ' + trackName, limit = 1)
if len(searchResults['tracks']['items']) == 0:
dictFeat = {'id': None }
else:
dictFeat = {'id':searchResults['tracks']['items'][0]['id']}
features = sp.audio_features(str(searchResults['tracks']['items'][0]['id']))[0]
#if features dont exist
if features is None:
for j in spotifyFeatures:
dictFeat[j] = None
else:
for j in spotifyFeatures:
dictFeat[j] = features[j]
return dictFeat
findLyrics = etree.XPath("//div[@data-lyrics-container='true']/text()|//div[@data-lyrics-container='true']/a/span/text()")
findReleaseDate = etree.XPath("//div[starts-with(@class, 'HeaderMetadata')]/text()")
findTags = etree.XPath("//a[starts-with(@class, 'SongTags')]/text()")
trackPattern = r'\(.*?\)|\-.*?$'
def getLyrics(artist, track):
tags = None;lyrics = None;geniusArtist=None;releaseDate=None;isFound=False
# search genius for artist + track
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
# this if statement checks status of request
if r.status_code != 200:
while True:
print(str(r))
time.sleep(15)
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
if r.status_code == 200:
break
lyricsJson = json.loads(r.text)
# if nothing is found print to screen and append to list
if bool(lyricsJson['response']['sections'][0]['hits']) == False:
#continue
track = re.sub(trackPattern, '', track).strip()
#print(track)
#time.sleep(.5)
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
if r.status_code != 200:
while True:
print(str(r))
time.sleep(15)
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
if r.status_code == 200:
break
lyricsJson = json.loads(r.text)
if bool(lyricsJson['response']['sections'][0]['hits']) == False:
print(artist + ' ' + track, ': No Hits')
isFound = False
return
# if a path exists, request page and scrape data
if lyricsJson['response']['sections'][0]['hits'][0]['result'].__contains__('path'):
r = requests.get('https://genius.com' + lyricsJson['response']['sections'][0]['hits'][0]['result']['path'])
if r.status_code != 200:
while True:
print(str(r))
time.sleep(15)
r = requests.get('https://genius.com' + lyricsJson['response']['sections'][0]['hits'][0]['result']['path'])
if r.status_code == 200:
break
html = etree.HTML(r.text)
if bool(lyricsJson['response']['sections'][0]['hits'][0]['result'].__contains__('artist_names')):
foundArtist = remove_accented_chars(lyricsJson['response']['sections'][0]['hits'][0]['result']['artist_names']).strip()
else:
foundArtist = None
#calculates levenshtein similarity ratio with fuzzy string matching
fuzzMatch = fuzz.partial_ratio(artist.upper(), foundArtist.upper())
if fuzzMatch <= 70:
# use regex to remove selected portions of string
track = re.sub(trackPattern, '', track).strip()
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
if r.status_code != 200:
while True:
print(str(r))
time.sleep(15)
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
if r.status_code == 200:
break
lyricsJson = json.loads(r.text)
if bool(lyricsJson['response']['sections'][0]['hits']) != False:
if lyricsJson['response']['sections'][0]['hits'][0]['result'].__contains__('path'):
r = requests.get('https://genius.com' + lyricsJson['response']['sections'][0]['hits'][0]['result']['path'])
if r.status_code != 200:
while True:
print(str(r))
time.sleep(15)
r = requests.get('https://genius.com' + lyricsJson['response']['sections'][0]['hits'][0]['result']['path'])
if r.status_code == 200:
break
html = etree.HTML(r.text)
if bool(lyricsJson['response']['sections'][0]['hits'][0]['result'].__contains__('artist_names')):
foundArtist = remove_accented_chars(lyricsJson['response']['sections'][0]['hits'][0]['result']['artist_names']).strip()
else:
foundArtist = None
fuzzMatch = fuzz.partial_ratio(artist.upper(), foundArtist.upper())
if fuzzMatch <= 70:
print(artist + ' ' + foundArtist, ': Bad Match')
isFound = False
else:
print(artist + ' ' + track + ': No Path')
isFound = False
else:
print(artist + ' ' + track + ': No hits')
isFound = False
# if the page/song is tagged as 'Non-Music' print to screen and break to next iteration
# some music is incorrectly tagged, and some searches find the wrong results
if 'Non-Music' in findTags(html) or 'Literature' in findTags(html):
fuzzMatch = fuzz.partial_ratio(artist.upper(), foundArtist)
if fuzzMatch <= 70:
track = re.sub(trackPattern, '', track).strip()
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
if r.status_code != 200:
while True:
print(str(r))
time.sleep(15)
r = requests.get('https://genius.com/api/search/multi?per_page=5&q='+ quote_plus(artist + ' ' + track))
if r.status_code == 200:
break
lyricsJson = json.loads(r.text)
if bool(lyricsJson['response']['sections'][0]['hits']) == True:
if lyricsJson['response']['sections'][0]['hits'][0]['result'].__contains__('path'):
r = requests.get('https://genius.com' + lyricsJson['response']['sections'][0]['hits'][0]['result']['path'])
if r.status_code != 200:
while True:
print(str(r))
time.sleep(15)
r = requests.get('https://genius.com' + lyricsJson['response']['sections'][0]['hits'][0]['result']['path'])
if r.status_code == 200:
break
html = etree.HTML(r.text)
if 'Non-Music' in findTags(html) or 'Literature' in findTags(html):
print(artist + ' ' + track + ' ' + str(findTags(html)))
tags = ', '.join(findTags(html))
return
else:
print(artist + ' ' + track + ': No Path')
isFound = False
else:
print(artist + ' ' + track, ': No Hits')
isFound = False
# store artist name to confirm if lyrics are correct
geniusArtist = foundArtist
# if value is passed, returns True
# json.dumps() allows us to store object as a string in this dataframe
# use json.loads() to load it back as an object
if bool(findLyrics(html)):
lyrics = json.dumps(findLyrics(html))
isFound = True
else:
isFound = False
if bool(findTags(html)):
tags = json.dumps(findTags(html))
else:
pass
if bool(findReleaseDate(html)):
releaseDate = max(findReleaseDate(html))
else:
pass
else:
print(artist + ' ' + track + ': No Path')
isFound = False
lyricsDict = {'tags': tags, 'lyrics': lyrics, 'geniusArtist':geniusArtist, 'releaseDate':releaseDate, 'isFound': isFound}
return lyricsDict