/
ety_downloader.py
489 lines (445 loc) · 15.4 KB
/
ety_downloader.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## ety_downloader.py
## A helpful tool to fetch data from website & generate mdx source file
##
## This program is a free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, version 3 of the License.
##
## You can get a copy of GNU General Public License along this program
## But you can always get it from http://www.gnu.org/licenses/gpl.txt
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
import os
import re
import fileinput
from os import path
from collections import OrderedDict
from datetime import datetime
from urllib3 import PoolManager
from multiprocessing import Pool
from multiprocessing import Manager
from bs4 import SoupStrainer
from bs4 import BeautifulSoup
STEP = 500
MAX_PROCESS = 25
http = PoolManager()
def fullpath(file, suffix='', base_dir=''):
if base_dir:
return ''.join([os.getcwd(), path.sep, base_dir, file, suffix])
else:
return ''.join([os.getcwd(), path.sep, file, suffix])
def readdata(file, base_dir=''):
fp = fullpath(file, base_dir=base_dir)
if not path.exists(fp):
print(file+" was not found under the same dir of this tool.")
else:
fr = open(fp, 'rU')
try:
return fr.read()
finally:
fr.close()
return None
def getwordlist(file, base_dir=''):
words = readdata(file, base_dir)
if words:
p = re.compile(r'\s*\n\s*')
words = p.sub('\n', words).strip()
return words.split('\n')
print("Please put valid wordlist under the same dir with this tool.")
return []
def dump(data, file, mod='w'):
fname = fullpath(file)
fw = open(fname, mod)
try:
fw.write(data)
finally:
fw.close()
def removefile(file):
if path.exists(file):
os.remove(file)
def getpage(link, BASE_URL = 'http://www.etymonline.com'):
r = http.request('GET', ''.join([BASE_URL, link]))
if r.status == 200:
return r.data
else:
return None
def info(l, s='page'):
return '%d %ss' % (l, s) if l>1 else '%d %s' % (l, s)
def dumpwords(sdir, words, sfx='', finished=True):
if len(words):
f = fullpath('rawhtml.txt', sfx, sdir)
mod = 'a' if sfx else 'w'
fw = open(f, mod)
try:
for en in words:
df = en[1]
if isinstance(df, OrderedDict):
text = []
for k, v in df.iteritems():
if k:
text.extend(['<div class="UOk"><span class="mB1">', k, '</span></div>'])
v = sorted(v, key=lambda d: d[0])
for val in v:
if val[0]:
text.extend(['<div class="m95">', val[0], '</div>'])
vdf = val[1].replace('<div class="FRe">', '<div class="Un2">')
text.append(vdf)
else:
text.append(val[1])
df = ''.join(text)
fw.write('\n'.join([en[0], df, '</>\n']))
finally:
fw.close()
if sfx and finished:
removefile(fullpath('failed.txt', '', sdir))
l = -len(sfx)
cmd = '\1'
nf = f[:l]
if path.exists(nf):
msg = "Found rawhtml.txt in the same dir, delete?(default=y/n)"
cmd = 'y'#raw_input(msg)
if cmd == 'n':
return
elif cmd != '\1':
removefile(nf)
os.rename(f, nf)
def cleansp(html):
p = re.compile(r'\s+')
html = p.sub(' ', html)
p = re.compile(r'<!--[^<>]+?-->')
html = p.sub('', html)
p = re.compile(r'\s*<br/>\s*')
html = p.sub('<br>', html)
p = re.compile(r'\{(/?span[^\{\}<>]*)\}', re.I)
html = p.sub(r'<\1>', html)
p = re.compile(r'(\s*<br>\s*)*(<hr[^>]*>)(\s*<br>\s*)*', re.I)
html = p.sub(r'\2', html)
p = re.compile(r'(<br>\s*){2,}', re.I)
html = p.sub('<div class="Xzi"></div>', html)
p = re.compile(r'(\s*<br>\s*)*(<(?:/?div[^>]*|br)>)(\s*<br>\s*)*', re.I)
html = p.sub(r'\2', html)
p = re.compile(r'\s*(<(?:/?div[^>]*|br)>)\s*', re.I)
html = p.sub(r'\1', html)
return html
def makeappdx(page):
srd = SoupStrainer('div', id='container')
div = BeautifulSoup(page, parse_only=srd).div
nav = div.find('div', id='navigation')
nav.decompose()
title = div.center.get_text(strip=True)
div.center.decompose()
font = div.find_all('font', size='2', color=None)
for f in font:
f.unwrap()
for p in div.find_all('p'):
p['class'] = 'ZFY'
p.name = 'div'
blank = div.find('div', class_='blank')
if blank:
blank.decompose()
ft = div.find('div', id='footer')
if ft:
ft.decompose()
div.attrs.clear()
div['class'] = 'oH1'
formatcontent(div)
text = cleansp(div.encode('iso-8859-1'))
div.decompose()
return ''.join(['<div class="xsv">', title, '</div>', text])
def formatcontent(tag):
for blk in tag.find_all('blockquote'):
blk.name = 'div'
last = blk.contents[-1]
p = re.compile(r'(\[[^\[\]]+?\])\s*$', re.I)
if last.string and p.search(last.string):
str = p.sub(r'{span class="Ewg"}\1{/span}', last.string)
last.string.replace_with(str)
blk['class'] = 'TA7'
else:
blk['class'] = 'YcA'
for hr in tag.find_all('hr'):
hr.attrs.clear()
hr['class'] = 'odM'
for sp in tag.find_all('span', class_='foreign'):
sp.attrs.clear()
sp['class'] = 'myY'
for a in tag.find_all('a', class_='crossreference'):
href = a['href']
a.attrs.clear()
p = re.compile(r'index.php\?term=([^"<>]+?)\&', re.I)
m = p.search(href)
assert m
a['href'] = ''.join(['entry://', m.group(1)])
for a in tag.find_all('a', href=re.compile('http://(?!www.etymonline.com)')):
a['target'] = '_blank'
def getwords(page, mdict, words, dref):
pgc = SoupStrainer('dl')
dl = BeautifulSoup(page, parse_only=pgc).dl
formatcontent(dl)
for a in dl.find_all('a', href=re.compile('http://www.etymonline.com/[^\.]+\.php$')):
href = a['href']
p = re.compile(r'/([^\.]+)\.php', re.I)
m = p.search(href)
assert m
word = ''.join(['appendix-', m.group(1)])
a['href'] = ''.join(['entry://', word])
if not word in dref:
print href
dref[word] = None
worddef = makeappdx(getpage(href, ''))
words.append([word, worddef])
dts = dl.find_all('dt')
l = len(dts)
dds = dl.find_all('dd')
assert l==len(dds)
for i in xrange(0, l):
word = dts[i].a.string.strip()
dd = dds[i]
dd.name = 'div'
dd['class'] = 'FRe'
worddef = cleansp(dd.encode('utf8'))
pos = word.find('(')
prop = None
if pos > 0:
p = re.compile(r'\(((?:[a-zA-Z \,\.]+?)?)[\.,]?(\d*)\.?\)', re.I)
m = p.search(word[pos:].replace('./', '., '))
assert m
prop = m.group(1).rstrip()
if prop:
prop += '.'
worddef = [m.group(2), worddef]
word = word[:pos].rstrip()
if word in mdict:
idx = mdict[word]
df = words[idx][1]
if isinstance(df, OrderedDict):
if prop in df:
df[prop].append(worddef)
else:
df[prop] = [worddef]
else:
if prop:
od = OrderedDict()
od[''] = [['', '<div class="tHO"></div>'.join([df, ''])]]
od[prop] = [worddef]
words[idx][1] = od
else:
words[idx][1] = '<div class="tHO"></div>'.join([df, worddef])
else:
mdict[word] = len(words)
if prop!=None:
od = OrderedDict()
od[prop] = [worddef]
words.append([word, od])
else:
words.append([word, worddef])
dl.decompose()
def makewords(urls, mdict, words, dref):
failed = []
count = 0
for url in urls:
count += 1
if count % 20 == 0:
print ".",
page = None
try:
page = getpage(url)
if page:
getwords(page, mdict, words, dref)
except Exception, e:
import traceback
print traceback.print_exc()
print "%s failed" % url
if not page:
failed.append(url)
return failed
def startdownload(urls, failedlist, mdict, words, dref):
lenr = len(urls)
leni = lenr + 1
failed = []
while lenr>0 and lenr<leni:
leni = lenr
failed = makewords(urls, mdict, words, dref)
urls = failed
lenr = len(failed)
failedlist.extend(failed)
if failedlist:
msg = "%s failed, retry?(default=y/n)\n" % info(len(failedlist))
return 'y'#raw_input(msg)
return 'n'
def fetchdata_and_make_mdx(arg, urls=None, suffix=''):
alpha = arg['alp']
dref = arg['dref']
url = ''.join(['/index.php?l=', alpha, '&allowed_in_frame=0'])
page = getpage(url)
mdict = {}
words = []
getwords(page, mdict, words, dref)
if not urls:
pgi = SoupStrainer('div', class_='paging')
ul = BeautifulSoup(page, parse_only=pgi).ul
if ul:
count = len(ul.find_all('li'))
urls = [''.join(['/index.php?l=', alpha, '&p=', str(i), '&allowed_in_frame=0']) for i in xrange(1, count)]
else:
urls = []
l = len(urls)
failedlist = []
cmd = 'y' if urls else 'n'
round = 0
while round<20 and (not cmd or cmd.lower()=='y'):
print "Block %s: %s downloading...using 1 thread" % (alpha, info(len(urls)))
cmd = startdownload(urls, failedlist, mdict, words, dref)
urls = failedlist
failedlist = []
round += 1
f = len(urls)
print "%s downloaded" % info(l-f),
mdict.clear()
if urls:
dump('\n'.join(urls), ''.join([arg['dir'], 'failed.txt']))
print ", %s failed, please look at failed.txt." % info(f)
dumpwords(arg['dir'], words, '.part', False)
else:
print ", 0 word failed"
dumpwords(arg['dir'], words, suffix)
def start(arg):
import socket
socket.setdefaulttimeout(120)
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
sdir = arg['dir']
fp1 = fullpath('rawhtml.txt.part', base_dir=sdir)
fp2 = fullpath('failed.txt', base_dir=sdir)
fp3 = fullpath('rawhtml.txt', base_dir=sdir)
if path.exists(fp1) and path.exists(fp2):
print ("Continue last failed")
urls = getwordlist('failed.txt', sdir)
fetchdata_and_make_mdx(arg, urls, '.part')
elif not path.exists(fp3):
print ("New session started")
fetchdata_and_make_mdx(arg)
def multiprocess_fetcher():
pl = [chr(i) for i in xrange(ord('a'), ord('z')+1)]
times = len(pl)
dir = fullpath('ety')
if not path.exists(dir):
os.mkdir(dir)
for i in xrange(1, times+1):
subdir = ''.join(['ety', path.sep, '%d'%i])
subpath = fullpath(subdir)
if not path.exists(subpath):
os.mkdir(subpath)
pool = Pool(MAX_PROCESS)
leni = times+1
dref = Manager().dict()
while 1:
args = []
for i in xrange(1, times+1):
sdir = ''.join(['ety', path.sep, '%d'%i, path.sep])
file = fullpath(sdir, 'rawhtml.txt')
if not(path.exists(file) and os.stat(file).st_size):
param = {}
param['alp'] = pl[i-1]
param['dir'] = sdir
param['dref'] = dref
args.append(param)
lenr = len(args)
if len(args) > 0:
if lenr >= leni:
print "The following parts cann't be downloaded:"
for arg in args:
print arg['alp']
times = -1
break
else:
pool.map(start, args)
else:
break
leni = lenr
return times
def formatEntry(head, line):
p = re.compile(r'(_{2,})')
line = p.sub(r'<span class="x0h">\1</span>', line)
p = re.compile(r'(<a href="entry://[^<>"]+">[^<>]+</a>)\s*\(([a-zA-Z \,\.]+?\.)\s*\)', re.I)
line = p.sub(r'\1, <span class="h63">\2</span>', line)
p = re.compile(r'(<a href="entry://[^<>"]+">[^<>]+</a>)\s*\(([a-zA-Z \,\.]+?\.)(\d+)\s*\)', re.I)
line = p.sub(r'\1, <span class="h63">\2</span>\3', line)
p = re.compile(r'(<a href="entry://[^<>"]+">[^<>]+</a>)\s*(\(\d+\))', re.I)
line = p.sub(r'\1\2', line)
p = re.compile(r'(<a href="entry://[^<>"]+">[^<>]+</a>)\s*\(([a-zA-Z ]+\.[a-zA-Z \,\.]+?\.?)\s*\)', re.I)
line = p.sub(r'\1 (<span class="myY">\2</span>)', line)
p = re.compile(r'\s+')
line = p.sub(' ', line)
if head.startswith('appendix-'):
title = ''
else:
title = ''.join(['<div class="SCA">', head, '</div>'])
return ''.join(['<link rel="stylesheet"href="ety.css"type="text/css"><div class="RmY">', title, line, '</div>'])
def formatabbr(page):
srd = SoupStrainer('div', id='container')
div = BeautifulSoup(page, parse_only=srd).div
nav = div.find('div', id='navigation')
nav.decompose()
tbl = div.find('table')
tbl.name = 'div'
tbl.attrs.clear()
tbl['class'] = 'oH1'
tdr = div.find_all(name=re.compile(r't[dr]', re.I))
for t in tdr:
t.unwrap()
for p in div.find_all('p'):
p['class'] = 'ZFY'
p.name = 'div'
blank = div.find('div', class_='blank')
if blank:
blank.decompose()
ft = div.find('div', id='footer')
if ft:
ft.decompose()
formatcontent(div)
div.attrs.clear()
div['class'] = 'RmY'
text = cleansp(div.encode('iso-8859-1'))
div.decompose()
return ''.join(['<link rel="stylesheet"href="ety.css"type="text/css">', text])
def combinefiles(times):
dir = ''.join(['ety', path.sep])
fw = open(fullpath(''.join([dir, 'ety.txt'])), 'w')
fww = open(fullpath(''.join([dir, 'words.txt'])), 'w')
try:
for i in xrange(1, times+1):
sdir = ''.join([dir, '%d'%i, path.sep])
file = fullpath('rawhtml.txt', base_dir=sdir)
lines = []
for line in fileinput.input(file):
line = line.strip()
if line == '</>':
fw.write('\n'.join([lines[0],
formatEntry(lines[0], lines[1]), line, '']))
fww.write('\n'.join([lines[0], '']))
del lines[:]
elif line:
lines.append(line)
page = getpage('/abbr.php?allowed_in_frame=0')
fw.write('\n'.join(['Introduction and abbreviations', formatabbr(page), '</>', '']))
fww.write('Introduction and abbreviations')
finally:
fw.close()
fww.close()
if __name__=="__main__":
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print "Start at %s" % datetime.now()
times = multiprocess_fetcher()
if times >= 0:
combinefiles(times)
print "Done!"
print "Finished at %s" % datetime.now()