-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_freebase.py
46 lines (37 loc) · 949 Bytes
/
api_freebase.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 os
import json
import requests
import requests.packages.urllib3
import pydash as _
def obtain(search_term, category):
api_key = os.getenv('FREEBASE_API_KEY')
requests.packages.urllib3.disable_warnings()
params = {
'query': search_term,
'output': '(description)'
}
if api_key:
params = _.assign({'api_key': api_key}, params)
request = requests.get(
'https://www.googleapis.com/freebase/v1/search', params, verify=False
)
if request.status_code != requests.codes.ok:
return None
try:
payload = json.loads(request.text)
except ValueError:
return None
return {
"metadata": {
"type": "freebase-information",
"term": search_term,
"category": category
},
"data": {
"name": _.get(payload, 'result.0.name', None),
"description": _.get(
payload,
"result.0.output.description./common/topic/description.0"
)
}
}