-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwashpost.py
46 lines (41 loc) · 1.17 KB
/
washpost.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
import secret
import httplib, urllib, base64
import cache
import time
import re
def fetch(name):
print "fetch trove",name
params = urllib.urlencode({
'key': secret.washpo(),
'variant': name,
'resource_type': 'entity',
'resource_subtype': 'person'})
try:
print "searching",name
headers = {}
tries = 10
while (tries > 0) :
conn = httplib.HTTPConnection('api.washingtonpost.com')
conn.request("GET", "/trove/v1/resources?%s" % params, "", headers)
response = conn.getresponse()
data = response.read()
conn.close()
match = re.search("Try again in (\d+) sec",data)
if (match):
s=int(match.group(1))
time.sleep(s + 1)
tries = tries -1
else:
print "OK",data
time.sleep(5)
return data
except Exception as e:
print e
return None
def trove(name) :
headers = {}
key= "wpt2_" + name
#print key
f=lambda : fetch(name)
# cache.delcache(key)
return cache.cache(key,f)