-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_search_links.py
37 lines (34 loc) · 1.1 KB
/
fetch_search_links.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
#!/usr/bin/python
import simplejson as json
import os
import pprint
import urllib2
import sha
def main():
existing_results = set(os.listdir('results/'))
for fn in os.listdir('searches/'):
try:
contents = json.load(open('searches/' + fn))
results = contents['bossresponse']['news']['results']
except KeyError, e:
print 'skipping', fn
continue
except json.decoder.JSONDecodeError, e:
print 'skipping', fn
continue
date = fn[-8:]
for result in results:
url = result['url']
out_fn = date + '.' + sha.sha(url).hexdigest()
if out_fn in existing_results:
print 'skipping', url
else:
print 'getting', url
try:
open('results/' + out_fn, 'w').write(
urllib2.urlopen(url, timeout=20).read())
existing_results.add(out_fn)
except Exception, e:
print 'skipping', url, 'because', e
if __name__ == '__main__':
main()