@@ -0,0 +1,221 @@
1+import json
2+from requests import get
3+from urllib import urlencode
4+from datetime import datetime
5+
6+resultCount=2
7+urlSearch = 'https://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}'
8+urlDetail = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&props=labels%7Cinfo%7Csitelinks%7Csitelinks%2Furls%7Cdescriptions%7Cclaims&{query}'
9+urlMap = 'https://www.openstreetmap.org/?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M'
10+
11+def request(query, params):
12+ params['url'] = urlSearch.format(query=urlencode({'srsearch': query, 'srlimit': resultCount}))
13+ print params['url']
14+ return params
15+
16+
17+def response(resp):
18+ results = []
19+ search_res = json.loads(resp.text)
20+
21+ wikidata_ids = set()
22+ for r in search_res.get('query', {}).get('search', {}):
23+ wikidata_ids.add(r.get('title', ''))
24+
25+ language = resp.search_params['language'].split('_')[0]
26+ if language == 'all':
27+ language = 'en'
28+ url = urlDetail.format(query=urlencode({'ids': '|'.join(wikidata_ids), 'languages': language + '|en'}))
29+
30+ before = datetime.now()
31+ htmlresponse = get(url)
32+ print datetime.now() - before
33+ jsonresponse = json.loads(htmlresponse.content)
34+ for wikidata_id in wikidata_ids:
35+ results = results + getDetail(jsonresponse, wikidata_id, language)
36+
37+ return results
38+
39+def getDetail(jsonresponse, wikidata_id, language):
40+ result = jsonresponse.get('entities', {}).get(wikidata_id, {})
41+
42+ title = result.get('labels', {}).get(language, {}).get('value', None)
43+ if title == None:
44+ title = result.get('labels', {}).get('en', {}).get('value', wikidata_id)
45+ results = []
46+ urls = []
47+ attributes = []
48+
49+ description = result.get('descriptions', {}).get(language, {}).get('value', '')
50+ if description == '':
51+ description = result.get('descriptions', {}).get('en', {}).get('value', '')
52+
53+ claims = result.get('claims', {})
54+ official_website = get_string(claims, 'P856', None)
55+ if official_website != None:
56+ urls.append({ 'title' : 'Official site', 'url': official_website })
57+ results.append({ 'title': title, 'url' : official_website })
58+
59+ if language != 'en':
60+ add_url(urls, 'Wikipedia (' + language + ')', get_wikilink(result, language + 'wiki'))
61+ wikipedia_en_link = get_wikilink(result, 'enwiki')
62+ add_url(urls, 'Wikipedia (en)', wikipedia_en_link)
63+
64+ if language != 'en':
65+ add_url(urls, 'Wiki voyage (' + language + ')', get_wikilink(result, language + 'wikivoyage'))
66+ add_url(urls, 'Wiki voyage (en)', get_wikilink(result, 'enwikivoyage'))
67+
68+ if language != 'en':
69+ add_url(urls, 'Wikiquote (' + language + ')', get_wikilink(result, language + 'wikiquote'))
70+ add_url(urls, 'Wikiquote (en)', get_wikilink(result, 'enwikiquote'))
71+
72+ add_url(urls, 'Commons wiki', get_wikilink(result, 'commonswiki'))
73+
74+ add_url(urls, 'Location', get_geolink(claims, 'P625', None))
75+
76+ add_url(urls, 'Wikidata', 'https://www.wikidata.org/wiki/' + wikidata_id + '?uselang='+ language)
77+
78+ musicbrainz_work_id = get_string(claims, 'P435')
79+ if musicbrainz_work_id != None:
80+ add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/work/' + musicbrainz_work_id)
81+
82+ musicbrainz_artist_id = get_string(claims, 'P434')
83+ if musicbrainz_artist_id != None:
84+ add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/artist/' + musicbrainz_artist_id)
85+
86+ musicbrainz_release_group_id = get_string(claims, 'P436')
87+ if musicbrainz_release_group_id != None:
88+ add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/release-group/' + musicbrainz_release_group_id)
89+
90+ musicbrainz_label_id = get_string(claims, 'P966')
91+ if musicbrainz_label_id != None:
92+ add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/label/' + musicbrainz_label_id)
93+
94+ # musicbrainz_area_id = get_string(claims, 'P982')
95+ # P1407 MusicBrainz series ID
96+ # P1004 MusicBrainz place ID
97+ # P1330 MusicBrainz instrument ID
98+ # P1407 MusicBrainz series ID
99+
100+ postal_code = get_string(claims, 'P281', None)
101+ if postal_code != None:
102+ attributes.append({'label' : 'Postal code(s)', 'value' : postal_code})
103+
104+ date_of_birth = get_time(claims, 'P569', None)
105+ if date_of_birth != None:
106+ attributes.append({'label' : 'Date of birth', 'value' : date_of_birth})
107+
108+ date_of_death = get_time(claims, 'P570', None)
109+ if date_of_death != None:
110+ attributes.append({'label' : 'Date of death', 'value' : date_of_death})
111+
112+
113+ results.append({
114+ 'infobox' : title,
115+ 'id' : wikipedia_en_link,
116+ 'content' : description,
117+ 'attributes' : attributes,
118+ 'urls' : urls
119+ })
120+
121+ return results
122+
123+
124+def add_url(urls, title, url):
125+ if url != None:
126+ urls.append({'title' : title, 'url' : url})
127+
128+
129+def get_mainsnak(claims, propertyName):
130+ propValue = claims.get(propertyName, {})
131+ if len(propValue) == 0:
132+ return None
133+
134+ propValue = propValue[0].get('mainsnak', None)
135+ return propValue
136+
137+
138+def get_string(claims, propertyName, defaultValue=None):
139+ propValue = claims.get(propertyName, {})
140+ if len(propValue) == 0:
141+ return defaultValue
142+
143+ result = []
144+ for e in propValue:
145+ mainsnak = e.get('mainsnak', {})
146+
147+ datatype = mainsnak.get('datatype', '')
148+ datavalue = mainsnak.get('datavalue', {})
149+ if datavalue != None:
150+ result.append(datavalue.get('value', ''))
151+
152+ if len(result) == 0:
153+ return defaultValue
154+ else:
155+ return ', '.join(result)
156+
157+
158+def get_time(claims, propertyName, defaultValue=None):
159+ propValue = claims.get(propertyName, {})
160+ if len(propValue) == 0:
161+ return defaultValue
162+
163+ result = []
164+ for e in propValue:
165+ mainsnak = e.get('mainsnak', {})
166+
167+ datatype = mainsnak.get('datatype', '')
168+ datavalue = mainsnak.get('datavalue', {})
169+ if datavalue != None:
170+ value = datavalue.get('value', '')
171+ result.append(value.get('time', ''))
172+
173+ if len(result) == 0:
174+ return defaultValue
175+ else:
176+ return ', '.join(result)
177+
178+
179+def get_geolink(claims, propertyName, defaultValue=''):
180+ mainsnak = get_mainsnak(claims, propertyName)
181+
182+ if mainsnak == None:
183+ return defaultValue
184+
185+ datatype = mainsnak.get('datatype', '')
186+ datavalue = mainsnak.get('datavalue', {})
187+
188+ if datatype != 'globe-coordinate':
189+ return defaultValue
190+
191+ value = datavalue.get('value', {})
192+
193+ precision = value.get('precision', 0.0002)
194+
195+ # there is no zoom information, deduce from precision (error prone)
196+ # samples :
197+ # 13 --> 5
198+ # 1 --> 6
199+ # 0.016666666666667 --> 9
200+ # 0.00027777777777778 --> 19
201+ # wolframalpha : quadratic fit { {13, 5}, {1, 6}, {0.0166666, 9}, {0.0002777777,19}}
202+ # 14.1186-8.8322 x+0.625447 x^2
203+ if precision < 0.0003:
204+ zoom = 19
205+ else:
206+ zoom = int(15 - precision*8.8322 + precision*precision*0.625447)
207+
208+ url = urlMap.replace('{latitude}', str(value.get('latitude',0))).replace('{longitude}', str(value.get('longitude',0))).replace('{zoom}', str(zoom))
209+
210+ return url
211+
212+
213+def get_wikilink(result, wikiid):
214+ url = result.get('sitelinks', {}).get(wikiid, {}).get('url', None)
215+ if url == None:
216+ return url
217+ elif url.startswith('http://'):
218+ url = url.replace('http://', 'https://')
219+ elif url.startswith('//'):
220+ url = 'https:' + url
221+ return url