-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgknowledge.py
More file actions
46 lines (38 loc) · 1.15 KB
/
gknowledge.py
File metadata and controls
46 lines (38 loc) · 1.15 KB
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
import requests
import json
import urllib
key="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
def get_wikidata(query):
query=raw_input()
r=requests.get('https://www.wikidata.org/w/api.php?action=wbsearchentities&search='+query+'&language=en&format=json')
#r=requests.get('https://www.wikidata.org/w/api.php?action=wbgetentities&id='+id+'&languages=en&format=json')
print r.json()
def get_kgsearch_result(query):
url='https://kgsearch.googleapis.com/v1/entities:search?query='+query+'&key='+key+'&limit=1&indent=True'
r=requests.get(url)
mydata={}
try:
data=r.json()['itemListElement'][0]['result']
except:
return mydata
try:
mydata['type']=data['@type'][0]
except:
mydata['type']=None;
try:
mydata['name']=data['name']
except:
mydata['name']=None
try:
mydata['image']=data['image']['contentUrl']
except:
mydata['image']=None
try:
mydata['description']=data['description']
except:
mydata['description']=None
try:
mydata['shortdesc']=data['detailedDescription']['articleBody']
except:
mydata['shortdesc']=None
return mydata