-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlumen.py
68 lines (56 loc) · 2.43 KB
/
lumen.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
""" Python API wrapper for the Lumen Database """
from urllib.parse import urlencode
import time
import requests
import logging
from requests import HTTPError
class Lumen():
def __init__(self, api_key=None):
self.api_key = api_key
self.session = requests.session()
self.session.headers = {
'User-Agent': "QUT_DMRC_LUMENAPI/0.2",
'X-Authentication-Token': self.api_key,
'Accept': 'application/json'
}
def search(self, params={}):
"""
"""
delay = 20
url= 'https://Lumendatabase.org/notices/search.json'
# This should be handled by the header User-Agent
#params['authentication_token'] = self.api_key
if len(params) > 0:
url = url + '?{}'.format(urlencode(params))
while True:
r = self.session.get(url=url)
if r.status_code == 200:
return r.json()
elif r.status_code == 429:
# Hit rate limit. Pause and try again (with an exponential backoff)
logging.error("Hit lumen rate limit. Sleeping {} seconds.".format(delay))
time.sleep(delay)
delay = delay * 2
elif r.status_code == 422:
# Lumen's most common error type.
try:
error = "Lumen returned error 422 Unprocessable Entity. Details: {}".format(r.status_code, r.text)
except:
error = "Lumen returned error 422 Unprocessable Entity. No details available."
logging.exception(error)
raise HTTPError(r.url, r.status_code, error, r.headers)
else:
error = "Unknown error fetching lumen data. HTTP error received: {} {}".format(r.status_code, r.text)
logging.exception(error)
raise HTTPError(r.url, r.status_code, error, r.headers)
def get(self, notice_id):
""" GET https://lumendatabase.org/notices/<notice id>.json """
url = "https://lumendatabase.org/notices/{}.json".format(notice_id)
r = self.session.get(url=url)
if r.status_code == 200:
json_results = r.json()
return json_results.get('dmca')
else:
error = "Unable to get notice. HTTP error received: {} {}".format(r.status_code, r.text)
logging.exception(error)
raise HTTPError(r.url, r.status_code, error, r.headers)