-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_data.py
455 lines (359 loc) · 14.3 KB
/
generate_data.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
from lxml import html
import requests
import socket
from multiprocessing.pool import Pool
from multiprocessing.pool import ThreadPool
import hashlib
import os, errno
import re
import time
import argparse
import math
from itertools import izip
from itertools import repeat
import json
from selenium import webdriver
import os.path as path
import sys
base_dir = path.dirname(path.abspath(__file__))
driver_path = base_dir + '/driver/chromedriver'
sys.path.append(base_dir)
from utils.guardian import process_html_guardian, get_documents_guardian
from utils.bbc import process_html_bbc
from utils.misc import mkdirp
from utils.misc import ProgressBar
def WriteUrls(filename, urls):
"""Writes a list of URLs to a file.
Args:
filename: The filename to the file where the URLs should be written.
urls: The list of URLs to write.
"""
with open(filename, 'w') as f:
f.writelines(url + '\n' for url in urls)
wayback_pattern = re.compile(r'web/([^/]*)/')
def WaybackUrl(urls, max_attempts=6):
"""Retrieves the URL for the latest historic copy using Wayback Machine.
Args:
urls: The URL for a specific page (canonical URL + forwarding URL's).
max_attempts: The maximum attempts at requesting the URL.
Returns:
The URL or None if no copy is stored for the URL.
Raises:
RuntimeError: Failed to retrieve the URL.
"""
if not urls:
return None
url = urls[0]
index_collection_url = 'http://archive.org/wayback/available'
payload = {'url': url}
attempts = 0
while attempts < max_attempts:
try:
entry_req = requests.get(index_collection_url, params=payload,
allow_redirects=False)
if entry_req.status_code != requests.codes.ok:
return WaybackUrl(urls[1:], max_attempts)
entry = entry_req.json()
if 'closest' not in entry['archived_snapshots']:
return WaybackUrl(urls[1:], max_attempts)
wayback_url = entry['archived_snapshots']['closest']['url']
wayback_url = wayback_pattern.sub(r'web/\g<1>id_/', wayback_url, 1)
return wayback_url
except requests.exceptions.ConnectionError:
pass
# Exponential back-off.
time.sleep(math.pow(2, attempts))
attempts += 1
raise RuntimeError('Failed to download URL for %s after %d attempts.'
'Please run the script again.' %
(url, max_attempts))
def Hashhex(s):
"""Returns a heximal formated SHA1 hash of the input string.
Args:
s: The string to hash.
Returns:
A heximal formatted hash of the input string.
"""
h = hashlib.sha1()
h.update(s)
return h.hexdigest()
def ReadMultipleUrls(filename):
"""Reads a list of URL lists.
Each line in the filename should contain a list of URLs separated by comma.
Args:
filename: The filename containing the URLs.
Returns:
A list of list of URLs.
"""
with open(filename) as f:
return [line.strip('\n').split(',') for line in f]
def GetNextPage(content, corpus):
"""Reads the content of a live blog and returns the url of the next page of the live blog.
Args:
content: html content of a live blog
corpus: The corpus name of the live blog (eg. bbc, guardian)
Returns:
The url of the next page.
"""
tree = html.fromstring(content)
if corpus == 'guardian':
older_part = tree.xpath('//div[@class="liveblog-navigation__older"]')
url = ''
if len(older_part) >= 1:
older_part_link = older_part[0].xpath(".//a")[0].get("href")
if older_part_link:
if corpus == 'guardian':
url = "http://www.theguardian.com" + older_part_link[:older_part_link.rfind('#')]
return url
def extend_url(url, web_driver, corpus):
"""When the content is spread across multiple ajax pages.
Reads the content of a bbc live blog and returns the html content of all the updates.
Args:
url: Url of a live blog
driver: The selenium web driver to do dynamic crawling.
Returns:
The html content of the overall live blog.
"""
web_driver.get(url)
while(1):
try:
if corpus == 'bbc':
web_driver.find_elements_by_xpath('//div[@class="lx-stream__show-more lx-stream-show-more"]/button')[0].click()
except:
break
return web_driver.page_source
def extend_guardian_docs(url, content, corpus, json_docs):
#Loop over next pages to append the documents list
while(1):
url = GetNextPage(content, corpus)
if url:
req = requests.get(url, allow_redirects=True, timeout=5)
if req.status_code == requests.codes.ok:
content = req.text.encode(req.encoding)
tree = html.fromstring(content)
block_data = get_documents_guardian(tree)
for item in block_data:
json_docs.append(item)
else:
break
return json_docs
def DownloadUrl(data_path, url, corpus, web_driver=None, max_attempts=5, timeout=5, ):
"""Downloads a URL.
Args:
url: The URL.
corpus: The corpus of the URL.
max_attempts: Max attempts for downloading the URL.
timeout: Connection timeout in seconds for each attempt.
Returns:
The HTML at the URL or None if the request failed.
"""
attempts = 0
while attempts < max_attempts:
try:
hash_val = Hashhex(url)
filename = '%s/downloads/%s/%s.json' % (data_path, corpus, hash_val)
if os.path.exists(filename):
print('Skip, file already exists: %s' % (hash_val))
break
if corpus == 'guardian':
req = requests.get(url, allow_redirects=True, timeout=timeout)
if req.status_code == requests.codes.ok:
content = req.text.encode(req.encoding)
json_data = process_html_guardian(hash_val, url, content)
json_data['documents'] = extend_guardian_docs(url, content, corpus, json_data['documents'])
elif (req.status_code in [301, 302, 404, 503] and attempts == max_attempts - 1):
return None
if corpus == 'bbc':
content = extend_url(url, web_driver, corpus)
json_data = process_html_bbc(hash_val, url, content)
with open(filename, 'w') as f:
f.write(json.dumps(json_data))
return json_data
except requests.exceptions.ConnectionError:
pass
except requests.exceptions.ContentDecodingError:
return None
except requests.exceptions.ChunkedEncodingError:
return None
except requests.exceptions.Timeout:
pass
except socket.timeout:
pass
# Exponential back-off.
time.sleep(math.pow(2, attempts))
attempts += 1
return None
def UrlMode(data_path, corpus, request_parallelism):
"""Finds Wayback Machine URLs and writes them to disk.
Args:
corpus: A corpus.
request_parallelism: The number of concurrent requests.
"""
print 'Finding Wayback Machine URLs for the %s set:' % corpus
old_urls_filename = '%s/urls/%s.txt' % (data_path, corpus)
new_urls_filename = '%s/urls/wayback_%s_urls.txt' % (data_path, corpus)
urls = ReadMultipleUrls(old_urls_filename)
p = ThreadPool(request_parallelism)
results = p.imap_unordered(WaybackUrl, urls)
progress_bar = ProgressBar(len(urls))
new_urls = []
for result in results:
if result:
new_urls.append(result)
progress_bar.Increment()
WriteUrls(new_urls_filename, new_urls)
def DownloadMapper(t):
"""Downloads an URL and checks that metadata is available for the URL.
Args:
t: a tuple (data_path, url, corpus).
Returns:
A pair of URL and content.
"""
data_path, url, corpus, web_driver = t
return url, DownloadUrl(data_path, url, corpus, web_driver)
def ReadUrls(filename):
"""Reads a list of URLs.
Args:
filename: The filename containing the URLs.
Returns:
A list of URLs.
"""
with open(filename) as f:
return [line.strip('\n') for line in f]
def DownloadMode(data_path, corpus):
"""Downloads the URLs for the specified corpus.
Args:
corpus: A corpus.
request_parallelism: The number of concurrent download requests.
"""
web_driver = None
if corpus == 'bbc':
web_driver = webdriver.Chrome(driver_path)
missing_urls = []
print 'Downloading URLs for the %s set:' % corpus
urls_filename = '%s/urls/%s_urls.txt' % (data_path, corpus)
urls = ReadUrls(urls_filename)
missing_urls_filename = '%s/urls/missing_%s_urls.txt' % (data_path, corpus)
if os.path.exists(missing_urls_filename):
print 'Only downloading missing URLs'
urls = list(set(urls).intersection(ReadUrls(missing_urls_filename)))
progress_bar = ProgressBar(len(urls))
collected_urls = []
try:
for url in urls:
try:
url, json_data = DownloadMapper((data_path, url, corpus, web_driver))
collected_urls.append(url)
except:
pass
progress_bar.Increment()
except KeyboardInterrupt:
if web_driver:
web_driver.close()
print 'Interrupted by user'
missing_urls.extend(set(urls) - set(collected_urls))
WriteUrls(missing_urls_filename, missing_urls)
if missing_urls:
print ('%d URLs couldn\'t be downloaded, see %s/missing_urls.txt.'
% (len(missing_urls), corpus))
print 'Try and run the command again to download the missing URLs.'
if web_driver:
web_driver.close()
def DownloadMode_parallel(data_path, corpus, request_parallelism):
"""Downloads the URLs for the specified corpus.
Args:
corpus: A corpus.
request_parallelism: The number of concurrent download requests.
"""
missing_urls = []
print 'Downloading URLs for the %s set:' % corpus
urls_filename = '%s/urls/wayback_%s_urls.txt' % (data_path, corpus)
urls = ReadUrls(urls_filename)
missing_urls_filename = '%s/missing_%s_urls.txt' % (data_path, corpus)
if os.path.exists(missing_urls_filename):
print 'Only downloading missing URLs'
urls = list(set(urls).intersection(ReadUrls(missing_urls_filename)))
p = ThreadPool(request_parallelism)
results = p.imap_unordered(DownloadMapper, izip(repeat(data_path), urls, repeat(corpus)))
progress_bar = ProgressBar(len(urls))
collected_urls = []
try:
for url, story_html in results:
if story_html:
collected_urls.append(url)
progress_bar.Increment()
except KeyboardInterrupt:
print 'Interrupted by user'
missing_urls.extend(set(urls) - set(collected_urls))
WriteUrls('%s/missing_urls.txt' % corpus, missing_urls)
if missing_urls:
print ('%d URLs couldn\'t be downloaded, see %s/missing_urls.txt.'
% (len(missing_urls), corpus))
print 'Try and run the command again to download the missing URLs.'
def get_urls(corpus, tree, urls):
"""Given the content of a page and corpus type get all urls.
Args:
corpus: Name of the corpus (eg. bbc, guardian)
tree: html content of the url
urls: urls to be appended to
Return:
append the list of urls
"""
if corpus == 'guardian':
pattern = "www.theguardian.com"
for item in tree.xpath("//a[@data-link-name='article']"):
url = item.get("href")
if re.search(pattern, url) and url not in urls:
urls.append(url)
return urls
def BootCatUrls(seed_list):
urls = []
return urls
def FetchMode(data_path, corpus):
"""Get the URLs for the specified corpus.
Args:
corpus: A corpus.
"""
urls = []
urls_filename = '%s/urls/%s_urls.txt' % (data_path, corpus)
if corpus == 'guardian':
req_url = 'http://www.theguardian.com/tone/minutebyminute/?page=1'
while(1):
print(req_url)
req = requests.get(req_url, allow_redirects=True, timeout=5)
if req.status_code == requests.codes.ok:
content = req.text.encode(req.encoding)
tree = html.fromstring(content)
urls = get_urls(corpus, tree, urls)
next_ref = tree.xpath("//a[@data-link-name='Pagination view next']")
if next_ref:
req_url = next_ref[0].get("href")
else:
break
elif (req.status_code in [301, 302, 404, 503]):
print(req.status_code)
break
if corpus == 'bbc':
urls = BootCatUrls()
WriteUrls(urls_filename, list(set(urls)))
def main():
parser = argparse.ArgumentParser(description='Generate the Summarization Corpus')
parser.add_argument('--corpus', choices=['bbc', 'guardian'], required=True)
parser.add_argument('--data_type', choices=['raw', 'processed'])
parser.add_argument('--mode', choices=['fetch_urls', 'download', 'archive_urls'], required=True)
parser.add_argument('--request_parallelism', type=int, default=1)
args = parser.parse_args()
if args.mode == 'fetch_urls':
data_path = path.join(base_dir, 'data/%s/' % ('raw'))
FetchMode(data_path, args.corpus)
elif args.mode == 'download':
data_path = path.join(base_dir, 'data/%s/' % (args.data_type))
download_path = path.join(data_path, 'downloads/%s' % (args.corpus))
print("Download Path:", download_path)
if not os.path.isdir(download_path):
mkdirp(download_path)
DownloadMode(data_path, args.corpus)
elif args.mode == 'archive_urls':
UrlMode(path.join(base_dir, 'data/processed/'), args.corpus, args.request_parallelism)
if __name__ == '__main__':
main()