-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazlyrics_scrapping.py
235 lines (198 loc) · 7.53 KB
/
azlyrics_scrapping.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
import os, os.path
from billboard_scrapping import get_titles_and_artists_billboard, clean_artist, clean_song
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen, ProxyHandler, build_opener
import time
from whoosh.fields import Schema, TEXT, ID, STORED
from whoosh.query import Every
from whoosh.index import create_in, open_dir, exists_in
from whoosh.qparser import QueryParser
from shutil import rmtree
import re
from whoosh.query import Phrase
base_url = 'https://www.azlyrics.com/'
headers = {'User-Agent':'Mozilla/5.0'}
def get_song_data_from_url(url):
try:
req = Request(url, headers=headers)
#req.set_proxy(proxy_host, 'http')
webpage = urlopen(req).read()
bs_webpage = BeautifulSoup(webpage,"lxml")
except:
print('exception at url: {}'.format(url))
return None
title = bs_webpage.find_all('b')[1].text
artist = bs_webpage.find('h2').find('b').text
lyrics = bs_webpage.find_all('div', attrs={'class': None})[1].text
try:
album = bs_webpage.find('div', class_='songinalbum_title').text
except AttributeError:
album = ""
cleaning = Cleaning()
title = cleaning.title(title)
lyrics = cleaning.lyrics(lyrics)
full_lyrics = cleaning.full_lyrics(lyrics)
if album=="You May Also Like":
album = ""
if album!="":
album = cleaning.album(album)
artist = " ".join(artist.split(" ")[:-1])
data = {
'title': title,
'artist': artist,
'lyrics': lyrics,
'full_lyrics': full_lyrics,
'album': album,
'url': url
}
return data
def create_schema():
schema = Schema(url=ID(),
title=TEXT(stored=True),
artist=TEXT(stored=True),
full_lyrics=TEXT(stored=True, phrase=True),
lyrics=TEXT(stored=True),
album=TEXT(stored=True))
return schema
def create_or_open_index(directory):
if not os.path.exists(directory):
os.mkdir(directory)
if exists_in(directory):
index = open_dir(directory)
else:
schema = create_schema()
index = create_in(directory, schema)
return index
def index_song(index, song_data):
writer = index.writer()
writer.add_document(url=u'{}'.format(song_data['url']),
title=u'{}'.format(song_data['title']),
artist=u'{}'.format(song_data['artist']),
full_lyrics=u'{}'.format(song_data['full_lyrics']),
lyrics=u'{}'.format(song_data['lyrics']),
album=u'{}'.format(song_data['album']))
writer.commit(optimize=True)
def search_song_by_title(title, index):
results_list = list()
qp = QueryParser('title', schema=index.schema)
q = qp.parse(u"{}".format(title))
with index.searcher() as searcher:
results = searcher.search(q)
for result in results:
data = {
'title': result['title'],
'artist': result['artist'],
'full_lyrics': result['full_lyrics'],
'lyrics': result['lyrics'],
'album': result['album']
}
results_list.append(data)
return results_list
def search_song_by_author(author, index):
results_list = list()
qp = QueryParser('author', schema=index.schema)
q = qp.parse(u"{}".format(title))
with index.searcher() as searcher:
results = searcher.search(q)
for result in results:
data = {
'title': result['title'],
'artist': result['artist'],
'full_lyrics': result['full_lyrics'],
'lyrics': result['lyrics'],
'album': result['album']
}
results_list.append(data)
return results_list
def search_song_by_lyrics(terms, index):
results_list = list()
qp = QueryParser('full_lyrics', schema=index.schema)
q = qp.parse(u'"{}"'.format(terms))
with index.searcher() as searcher:
results = searcher.search(q)
for result in results:
data = {
'title': result['title'],
'artist': result['artist'],
'full_lyrics': result['full_lyrics'],
'lyrics': result['lyrics'],
'album': result['album']
}
results_list.append(data)
return results_list
def get_songs_urls_by_letter(letter, limit=None):
url = base_url+'{}.html'.format(letter)
final_url_list = list()
def get_urls_by_letter(url):
req = Request(url)
webpage = urlopen(req).read()
bs_webpage = BeautifulSoup(webpage,"lxml")
divs = bs_webpage.find_all('div', class_='col-sm-6')
a_list = [i.find_all('a') for i in divs]
a_list = [base_url+i['href'] for j in a_list for i in j]
return a_list
urls = get_urls_by_letter(url)
for u in urls[:limit]:
req = Request(u)
webpage = urlopen(req).read()
bs_webpage = BeautifulSoup(webpage,"lxml")
divs = bs_webpage.find_all('div', class_='listalbum-item')
a_list = [i.find_all('a') for i in divs]
final_url_list.append([base_url+i['href'][3:] for j in a_list for i in j])
time.sleep(20)
final_url_list = [i for j in final_url_list for i in j]
return final_url_list
def index_songs_by_letter(letter, index, limit=None):
urls = get_songs_urls_by_letter(letter, limit)
for u in urls:
song_data = get_song_data_from_url(u)
print(song_data['title'])
index_song(index, song_data)
time.sleep(15)
def index_songs_by_artist(artist, index):
letter = 19 if artist[:1] not in "abcdefghijklmnopqrstuvwxyz" else artist[:1]
url = base_url+"{}/{}.html".format(letter, clean_artist(artist))
req = Request(url)
webpage = urlopen(req).read()
bs_webpage = BeautifulSoup(webpage,"lxml")
divs = bs_webpage.find_all('div', class_='listalbum-item')
a_list = [i.find_all('a') for i in divs]
final_url_list = [base_url+i['href'][3:] for j in a_list for i in j]
time.sleep(20)
for u in final_url_list:
song_data = get_song_data_from_url(u)
print(song_data['title'])
index_song(index, song_data)
time.sleep(15)
def index_songs_by_billboard(number, index, limit=None):
song_artist_tuple = get_titles_and_artists_billboard(number)
for song, artist in song_artist_tuple:
song = clean_song(song)
artist = clean_artist(artist)
url = base_url+'lyrics/{}/{}.html'.format(artist, song)
print(url)
song_data = get_song_data_from_url(url)
if song_data is None:
continue
print(song_data['title'])
index_song(index, song_data)
time.sleep(15)
class Cleaning():
def full_lyrics(self, lyrics):
lyrics = lyrics.split('\r\n')
lyrics = [i.replace('\n', ' ')
for i in lyrics if i not in ['\n', '\r', '\n\r', '\r\n', '']]
lyrics = ', '.join(lyrics).replace(',', '').replace('.', '').lower()
return lyrics
def lyrics(self, lyrics):
lyrics = lyrics.replace('\r', '').replace('\n\n', '\n')
lyrics = lyrics[1:][:-1]
return lyrics
def title(self, title):
title = title.replace('"', '')
return title
def album(self, album):
print(album)
if (album!=""):
album = re.findall(r'"([^"]*)"', album)[0]
return album