-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUltimate_Crawler.py
356 lines (247 loc) · 9.3 KB
/
Ultimate_Crawler.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# coding: utf-8
# # UGCrawler 1.0 alpha
#
# This Notebook contains code fragments to crawl ultimate-guitar.com. It is far from finished but all the major components are there.
#
# Since it is not feasible for me to generate a complete rip, i am sharing this with all the guitarists out there who are pissed off by UG for any of the million reasons given.
#
# If you would like to contribute, by doing part of the crawl, of implementin a better selection logic, or in any other way, feel free to do so.
# In[1]:
from lxml import etree
import lxml.html
import string
import urllib
from time import sleep
import sys
import random
from fake_useragent import UserAgent
from bs4 import BeautifulSoup
from socket import timeout
# In[2]:
chars = ['0-9'] + list(string.ascii_lowercase)
chars = list(string.ascii_lowercase)
crawl_delay = 3
ua = UserAgent()
# In[3]:
class Proxifier():
def __init__(self):
self.proxies = []
self.refuel()
def refuel(self):
# crawl some proxies
proxies_req = urllib.request.Request('https://www.sslproxies.org/')
proxies_req.add_header('User-Agent', ua.random)
proxies_doc = urllib.request.urlopen(proxies_req).read().decode('utf8')
soup = BeautifulSoup(proxies_doc, 'html.parser')
proxies_table = soup.find(id='proxylisttable')
# Save proxies in the array
for row in proxies_table.tbody.find_all('tr'):
self.proxies.append({
'ip': row.find_all('td')[0].string,
'port': row.find_all('td')[1].string
})
def get_proxy(self):
# Choose a random proxy, refuel if necessary
if len(self.proxies) < 10:
self.refuel()
return random.choice(self.proxies)
def drop_proxy(self, proxy):
self.proxies = [x for x in self.proxies if x != proxy]
# In[4]:
proxifier = Proxifier()
# In[ ]:
# wrapper to encapsulate proxy logic and stuff
def get_html(url):
proxy = proxifier.get_proxy()
print('using proxy: {}'.format(proxy))
# try 10 times with that proxy
tries = 0
while tries < 10:
print('Attempt ' + str(tries) )
req = urllib.request.Request(url)
req.set_proxy(proxy['ip'] + ':' + proxy['port'], 'http')
req.add_header('User-Agent', 'ua.random')
try:
response = urllib.request.urlopen(req, timeout=5).read().decode('utf8')
return response
except urllib.error.HTTPError as e:
if e.code == 404:
# site not found
print('404')
return '404'
elif e.code == 403:
# proxy banned, get new one
print('banned: {}'.format(proxy))
proxifier.drop_proxy(proxy)
proxy = proxifier.get_proxy()
tries = 0
elif e.code == 502:
print('refused: {}'.format(proxy))
tries += 1
elif e.code == 400:
print('Bad request!')
tries += 1
except timeout:
print('Timeout')
proxifier.drop_proxy(proxy)
proxy = proxifier.get_proxy()
tries += 1
except urllib.error.URLError as e:
print(e.reason)
except:
print(sys.exc_info()[0])
tries += 1
# if 10 failures on same proxy for whatever reason, try again with a new one
proxifier.drop_proxy(proxy)
return get_html(url)
# In[ ]:
#proxifier.proxies = [{'ip': '217.61.14.44', 'port': '3128'} for i in range(10)]
get_html('http://www.httpbin.org/ip')
# In[ ]:
def get_band_urls(char):
base_url = 'http://www.ultimate-guitar.com/bands/{}.htm'
htmlparser = etree.HTMLParser()
band_dict = {}
cnt = 0
while True:
postfix = '' if cnt == 0 else cnt
response = get_html(base_url.format(char + str(postfix)))
return response
tree = etree.parse(response, htmlparser)
# collect song links
tab_table = tree.xpath('//*[contains(@class, "b3")]/../table[2]/tr/td[2]/a')
# empty means no more pages
if not tab_table:
break
for child in tab_table:
band_dict[child.text[:-5]] = child.attrib['href']
cnt += 1
return band_dict
# In[ ]:
res = get_band_urls('0-9')
# In[19]:
res
# In[8]:
def get_artist_links(url):
base_url = 'http://www.ultimate-guitar.com'
# 2 schemes for paging:
# https://www.ultimate-guitar.com/artist/3_inches_of_blood_9343?page=1
#
if url.endswith('.htm'):
url = base_url + url[:-4] + '{}' + url[-4:]
else:
url = base_url + url + '?page={}'
htmlparser = etree.HTMLParser()
all_tabs_chords = []
cnt = 0
tries = 0
while True:
# page 0 exists and is pretty much page 1 without the album shit
if cnt == 1:
cnt = 2
continue
response = get_html(url.format(cnt))
tree = etree.parse(response, htmlparser)
# collect entries
tab_table = tree.xpath("//td/b[./text()='Tab' or ./text()='tab']/../..")
chords_table = tree.xpath("//td/b[./text()='Chords' or ./text()='chords']/../..")
for song in tab_table:
name = song.xpath('./td[1]/a[1]')[0].text
link = song.xpath('./td[1]/a[1]')[0].attrib['href']
if not song.xpath('./td[2]/span[1]'):
rating = 0
else:
rating = song.xpath('./td[2]/span[1]')[0].attrib['title']
all_tabs_chords.append(
{
'name': name,
'link': link,
'rating': float(rating),
'type': 'tab'
}
)
for song in chords_table:
name = song.xpath('./td[1]/a[1]')[0].text
link = song.xpath('./td[1]/a[1]')[0].attrib['href']
if not song.xpath('./td[2]/span[1]'):
rating = 0
else:
rating = song.xpath('./td[2]/span[1]')[0].attrib['title']
all_tabs_chords.append(
{
'name': name,
'link': link,
'rating': float(rating),
'type': 'chords'
}
)
cnt += 1
return all_tabs_chords
# In[9]:
def get_print_version(url):
tries = 0
response = get_html(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
# get link
link = tree.xpath("//*[@id='print_link']")[0].attrib['href']
response = get_html('http://tabs.ultimate-guitar.com{}'.format(link))
ht = lxml.html.parse(response)
return ht.xpath("//div[contains(@class, 'tb_ct')]")[0].text_content()
# In[10]:
def clean(s):
return ' '.join(re.sub('[^a-zA-Z0-9\(\)\-\s"\'!?#$\+]', '', s).split())
# # UNLEASH THE SPIDER
# In[11]:
import re, os
highest_rated_only = True
target_dir = '/home/max/ssd_2/UG/'
for c in chars:
# rip the band urls
band_urls = get_band_urls(c)
# rip the tabs for each band
for artist, url in band_urls.items():
# make out dirs
artist_folder_name = clean(artist)
artist_dir = os.path.join(target_dir, c, artist_folder_name)
if not os.path.exists(artist_dir):
os.makedirs(artist_dir)
os.makedirs(os.path.join(artist_dir, 'chords'))
os.makedirs(os.path.join(artist_dir, 'tab'))
else:
# assume we finished crawling this artist
continue
# rip
print('Crawling {}...'.format(artist))
song_links = get_artist_links(url)
if len(song_links) == 0:
print('\tNothing found!')
continue
artist_dict = {}
artist_dict['chords'] = {}
artist_dict['tab'] = {}
# keep the highest rated version, factoring in rating count
for song_dict in song_links:
t = song_dict['type']
if highest_rated_only:
cleaned_name = re.sub(r'\s\(ver\s\d+\)', '', song_dict['name'])
if cleaned_name not in artist_dict[t].keys() or artist_dict[t][cleaned_name]['rating'] < song_dict['rating']:
artist_dict[t][cleaned_name] = song_dict
else:
artist_dict[t][song_dict['name']] = song_dict
# rip and write songs to drive
for name, d in artist_dict['tab'].items():
tab = get_print_version(d['link'])
file_name = clean(name) + '.txt'
with open(os.path.join(artist_dir, 'tab', file_name), 'w') as out:
out.write(tab)
for name, d in artist_dict['chords'].items():
chords = get_print_version(d['link'])
file_name = clean(name) + '.txt'
with open(os.path.join(artist_dir, 'chords', file_name), 'w') as out:
out.write(chords)
sleep(5)
# In[ ]:
f
# In[12]:
response