|
@@ -2,13 +2,25 @@ import json
|
2
|
2
|
from requests import get
|
3
|
3
|
from urllib import urlencode
|
4
|
4
|
|
5
|
|
-resultCount=1
|
6
|
|
-urlSearch = 'https://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}'
|
7
|
|
-urlDetail = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&props=labels%7Cinfo%7Csitelinks%7Csitelinks%2Furls%7Cdescriptions%7Cclaims&{query}'
|
8
|
|
-urlMap = 'https://www.openstreetmap.org/?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M'
|
|
5
|
+result_count = 1
|
|
6
|
+wikidata_host = 'https://www.wikidata.org'
|
|
7
|
+wikidata_api = wikidata_host + '/w/api.php'
|
|
8
|
+url_search = wikidata_api \
|
|
9
|
+ + '?action=query&list=search&format=json'\
|
|
10
|
+ + '&srnamespace=0&srprop=sectiontitle&{query}'
|
|
11
|
+url_detail = wikidata_api\
|
|
12
|
+ + '?action=wbgetentities&format=json'\
|
|
13
|
+ + '&props=labels%7Cinfo%7Csitelinks'\
|
|
14
|
+ + '%7Csitelinks%2Furls%7Cdescriptions%7Cclaims'\
|
|
15
|
+ + '&{query}'
|
|
16
|
+url_map = 'https://www.openstreetmap.org/'\
|
|
17
|
+ + '?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M'
|
|
18
|
+
|
9
|
19
|
|
10
|
20
|
def request(query, params):
|
11
|
|
- params['url'] = urlSearch.format(query=urlencode({'srsearch': query, 'srlimit': resultCount}))
|
|
21
|
+ params['url'] = url_search.format(
|
|
22
|
+ query=urlencode({'srsearch': query,
|
|
23
|
+ 'srlimit': result_count}))
|
12
|
24
|
return params
|
13
|
25
|
|
14
|
26
|
|
|
@@ -23,7 +35,8 @@ def response(resp):
|
23
|
35
|
language = resp.search_params['language'].split('_')[0]
|
24
|
36
|
if language == 'all':
|
25
|
37
|
language = 'en'
|
26
|
|
- url = urlDetail.format(query=urlencode({'ids': '|'.join(wikidata_ids), 'languages': language + '|en'}))
|
|
38
|
+ url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
|
|
39
|
+ 'languages': language + '|en'}))
|
27
|
40
|
|
28
|
41
|
htmlresponse = get(url)
|
29
|
42
|
jsonresponse = json.loads(htmlresponse.content)
|
|
@@ -32,6 +45,7 @@ def response(resp):
|
32
|
45
|
|
33
|
46
|
return results
|
34
|
47
|
|
|
48
|
+
|
35
|
49
|
def getDetail(jsonresponse, wikidata_id, language):
|
36
|
50
|
results = []
|
37
|
51
|
urls = []
|
|
@@ -40,60 +54,103 @@ def getDetail(jsonresponse, wikidata_id, language):
|
40
|
54
|
result = jsonresponse.get('entities', {}).get(wikidata_id, {})
|
41
|
55
|
|
42
|
56
|
title = result.get('labels', {}).get(language, {}).get('value', None)
|
43
|
|
- if title == None:
|
|
57
|
+ if title is None:
|
44
|
58
|
title = result.get('labels', {}).get('en', {}).get('value', None)
|
45
|
|
- if title == None:
|
|
59
|
+ if title is None:
|
46
|
60
|
return results
|
47
|
61
|
|
48
|
|
- description = result.get('descriptions', {}).get(language, {}).get('value', None)
|
49
|
|
- if description == None:
|
50
|
|
- description = result.get('descriptions', {}).get('en', {}).get('value', '')
|
|
62
|
+ description = result\
|
|
63
|
+ .get('descriptions', {})\
|
|
64
|
+ .get(language, {})\
|
|
65
|
+ .get('value', None)
|
|
66
|
+
|
|
67
|
+ if description is None:
|
|
68
|
+ description = result\
|
|
69
|
+ .get('descriptions', {})\
|
|
70
|
+ .get('en', {})\
|
|
71
|
+ .get('value', '')
|
51
|
72
|
|
52
|
73
|
claims = result.get('claims', {})
|
53
|
74
|
official_website = get_string(claims, 'P856', None)
|
54
|
|
- if official_website != None:
|
55
|
|
- urls.append({ 'title' : 'Official site', 'url': official_website })
|
56
|
|
- results.append({ 'title': title, 'url' : official_website })
|
|
75
|
+ if official_website is not None:
|
|
76
|
+ urls.append({'title': 'Official site', 'url': official_website})
|
|
77
|
+ results.append({'title': title, 'url': official_website})
|
57
|
78
|
|
58
|
79
|
wikipedia_link_count = 0
|
59
|
80
|
if language != 'en':
|
60
|
|
- wikipedia_link_count += add_url(urls, 'Wikipedia (' + language + ')', get_wikilink(result, language + 'wiki'))
|
|
81
|
+ wikipedia_link_count += add_url(urls,
|
|
82
|
+ 'Wikipedia (' + language + ')',
|
|
83
|
+ get_wikilink(result, language +
|
|
84
|
+ 'wiki'))
|
61
|
85
|
wikipedia_en_link = get_wikilink(result, 'enwiki')
|
62
|
|
- wikipedia_link_count += add_url(urls, 'Wikipedia (en)', wikipedia_en_link)
|
|
86
|
+ wikipedia_link_count += add_url(urls,
|
|
87
|
+ 'Wikipedia (en)',
|
|
88
|
+ wikipedia_en_link)
|
63
|
89
|
if wikipedia_link_count == 0:
|
64
|
90
|
misc_language = get_wiki_firstlanguage(result, 'wiki')
|
65
|
|
- if misc_language != None:
|
66
|
|
- add_url(urls, 'Wikipedia (' + misc_language + ')', get_wikilink(result, misc_language + 'wiki'))
|
|
91
|
+ if misc_language is not None:
|
|
92
|
+ add_url(urls,
|
|
93
|
+ 'Wikipedia (' + misc_language + ')',
|
|
94
|
+ get_wikilink(result, misc_language + 'wiki'))
|
67
|
95
|
|
68
|
96
|
if language != 'en':
|
69
|
|
- add_url(urls, 'Wiki voyage (' + language + ')', get_wikilink(result, language + 'wikivoyage'))
|
70
|
|
- add_url(urls, 'Wiki voyage (en)', get_wikilink(result, 'enwikivoyage'))
|
|
97
|
+ add_url(urls,
|
|
98
|
+ 'Wiki voyage (' + language + ')',
|
|
99
|
+ get_wikilink(result, language + 'wikivoyage'))
|
|
100
|
+
|
|
101
|
+ add_url(urls,
|
|
102
|
+ 'Wiki voyage (en)',
|
|
103
|
+ get_wikilink(result, 'enwikivoyage'))
|
71
|
104
|
|
72
|
105
|
if language != 'en':
|
73
|
|
- add_url(urls, 'Wikiquote (' + language + ')', get_wikilink(result, language + 'wikiquote'))
|
74
|
|
- add_url(urls, 'Wikiquote (en)', get_wikilink(result, 'enwikiquote'))
|
|
106
|
+ add_url(urls,
|
|
107
|
+ 'Wikiquote (' + language + ')',
|
|
108
|
+ get_wikilink(result, language + 'wikiquote'))
|
75
|
109
|
|
76
|
|
- add_url(urls, 'Commons wiki', get_wikilink(result, 'commonswiki'))
|
|
110
|
+ add_url(urls,
|
|
111
|
+ 'Wikiquote (en)',
|
|
112
|
+ get_wikilink(result, 'enwikiquote'))
|
77
|
113
|
|
78
|
|
- add_url(urls, 'Location', get_geolink(claims, 'P625', None))
|
|
114
|
+ add_url(urls,
|
|
115
|
+ 'Commons wiki',
|
|
116
|
+ get_wikilink(result, 'commonswiki'))
|
79
|
117
|
|
80
|
|
- add_url(urls, 'Wikidata', 'https://www.wikidata.org/wiki/' + wikidata_id + '?uselang='+ language)
|
|
118
|
+ add_url(urls,
|
|
119
|
+ 'Location',
|
|
120
|
+ get_geolink(claims, 'P625', None))
|
|
121
|
+
|
|
122
|
+ add_url(urls,
|
|
123
|
+ 'Wikidata',
|
|
124
|
+ 'https://www.wikidata.org/wiki/'
|
|
125
|
+ + wikidata_id + '?uselang=' + language)
|
81
|
126
|
|
82
|
127
|
musicbrainz_work_id = get_string(claims, 'P435')
|
83
|
|
- if musicbrainz_work_id != None:
|
84
|
|
- add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/work/' + musicbrainz_work_id)
|
|
128
|
+ if musicbrainz_work_id is not None:
|
|
129
|
+ add_url(urls,
|
|
130
|
+ 'MusicBrainz',
|
|
131
|
+ 'http://musicbrainz.org/work/'
|
|
132
|
+ + musicbrainz_work_id)
|
85
|
133
|
|
86
|
134
|
musicbrainz_artist_id = get_string(claims, 'P434')
|
87
|
|
- if musicbrainz_artist_id != None:
|
88
|
|
- add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/artist/' + musicbrainz_artist_id)
|
|
135
|
+ if musicbrainz_artist_id is not None:
|
|
136
|
+ add_url(urls,
|
|
137
|
+ 'MusicBrainz',
|
|
138
|
+ 'http://musicbrainz.org/artist/'
|
|
139
|
+ + musicbrainz_artist_id)
|
89
|
140
|
|
90
|
141
|
musicbrainz_release_group_id = get_string(claims, 'P436')
|
91
|
|
- if musicbrainz_release_group_id != None:
|
92
|
|
- add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/release-group/' + musicbrainz_release_group_id)
|
|
142
|
+ if musicbrainz_release_group_id is not None:
|
|
143
|
+ add_url(urls,
|
|
144
|
+ 'MusicBrainz',
|
|
145
|
+ 'http://musicbrainz.org/release-group/'
|
|
146
|
+ + musicbrainz_release_group_id)
|
93
|
147
|
|
94
|
148
|
musicbrainz_label_id = get_string(claims, 'P966')
|
95
|
|
- if musicbrainz_label_id != None:
|
96
|
|
- add_url(urls, 'MusicBrainz', 'http://musicbrainz.org/label/' + musicbrainz_label_id)
|
|
149
|
+ if musicbrainz_label_id is not None:
|
|
150
|
+ add_url(urls,
|
|
151
|
+ 'MusicBrainz',
|
|
152
|
+ 'http://musicbrainz.org/label/'
|
|
153
|
+ + musicbrainz_label_id)
|
97
|
154
|
|
98
|
155
|
# musicbrainz_area_id = get_string(claims, 'P982')
|
99
|
156
|
# P1407 MusicBrainz series ID
|
|
@@ -102,42 +159,43 @@ def getDetail(jsonresponse, wikidata_id, language):
|
102
|
159
|
# P1407 MusicBrainz series ID
|
103
|
160
|
|
104
|
161
|
postal_code = get_string(claims, 'P281', None)
|
105
|
|
- if postal_code != None:
|
106
|
|
- attributes.append({'label' : 'Postal code(s)', 'value' : postal_code})
|
|
162
|
+ if postal_code is not None:
|
|
163
|
+ attributes.append({'label': 'Postal code(s)', 'value': postal_code})
|
107
|
164
|
|
108
|
165
|
date_of_birth = get_time(claims, 'P569', None)
|
109
|
|
- if date_of_birth != None:
|
110
|
|
- attributes.append({'label' : 'Date of birth', 'value' : date_of_birth})
|
|
166
|
+ if date_of_birth is not None:
|
|
167
|
+ attributes.append({'label': 'Date of birth', 'value': date_of_birth})
|
111
|
168
|
|
112
|
169
|
date_of_death = get_time(claims, 'P570', None)
|
113
|
|
- if date_of_death != None:
|
114
|
|
- attributes.append({'label' : 'Date of death', 'value' : date_of_death})
|
|
170
|
+ if date_of_death is not None:
|
|
171
|
+ attributes.append({'label': 'Date of death', 'value': date_of_death})
|
115
|
172
|
|
116
|
|
- if len(attributes)==0 and len(urls)==2 and len(description)==0:
|
|
173
|
+ if len(attributes) == 0 and len(urls) == 2 and len(description) == 0:
|
117
|
174
|
results.append({
|
118
|
|
- 'url': urls[0]['url'],
|
119
|
|
- 'title': title,
|
120
|
|
- 'content': description
|
121
|
|
- })
|
|
175
|
+ 'url': urls[0]['url'],
|
|
176
|
+ 'title': title,
|
|
177
|
+ 'content': description
|
|
178
|
+ })
|
122
|
179
|
else:
|
123
|
180
|
results.append({
|
124
|
|
- 'infobox' : title,
|
125
|
|
- 'id' : wikipedia_en_link,
|
126
|
|
- 'content' : description,
|
127
|
|
- 'attributes' : attributes,
|
128
|
|
- 'urls' : urls
|
129
|
|
- })
|
|
181
|
+ 'infobox': title,
|
|
182
|
+ 'id': wikipedia_en_link,
|
|
183
|
+ 'content': description,
|
|
184
|
+ 'attributes': attributes,
|
|
185
|
+ 'urls': urls
|
|
186
|
+ })
|
130
|
187
|
|
131
|
188
|
return results
|
132
|
189
|
|
133
|
190
|
|
134
|
191
|
def add_url(urls, title, url):
|
135
|
|
- if url != None:
|
136
|
|
- urls.append({'title' : title, 'url' : url})
|
|
192
|
+ if url is not None:
|
|
193
|
+ urls.append({'title': title, 'url': url})
|
137
|
194
|
return 1
|
138
|
195
|
else:
|
139
|
196
|
return 0
|
140
|
197
|
|
|
198
|
+
|
141
|
199
|
def get_mainsnak(claims, propertyName):
|
142
|
200
|
propValue = claims.get(propertyName, {})
|
143
|
201
|
if len(propValue) == 0:
|
|
@@ -157,7 +215,7 @@ def get_string(claims, propertyName, defaultValue=None):
|
157
|
215
|
mainsnak = e.get('mainsnak', {})
|
158
|
216
|
|
159
|
217
|
datavalue = mainsnak.get('datavalue', {})
|
160
|
|
- if datavalue != None:
|
|
218
|
+ if datavalue is not None:
|
161
|
219
|
result.append(datavalue.get('value', ''))
|
162
|
220
|
|
163
|
221
|
if len(result) == 0:
|
|
@@ -177,7 +235,7 @@ def get_time(claims, propertyName, defaultValue=None):
|
177
|
235
|
mainsnak = e.get('mainsnak', {})
|
178
|
236
|
|
179
|
237
|
datavalue = mainsnak.get('datavalue', {})
|
180
|
|
- if datavalue != None:
|
|
238
|
+ if datavalue is not None:
|
181
|
239
|
value = datavalue.get('value', '')
|
182
|
240
|
result.append(value.get('time', ''))
|
183
|
241
|
|
|
@@ -190,7 +248,7 @@ def get_time(claims, propertyName, defaultValue=None):
|
190
|
248
|
def get_geolink(claims, propertyName, defaultValue=''):
|
191
|
249
|
mainsnak = get_mainsnak(claims, propertyName)
|
192
|
250
|
|
193
|
|
- if mainsnak == None:
|
|
251
|
+ if mainsnak is None:
|
194
|
252
|
return defaultValue
|
195
|
253
|
|
196
|
254
|
datatype = mainsnak.get('datatype', '')
|
|
@@ -209,21 +267,25 @@ def get_geolink(claims, propertyName, defaultValue=''):
|
209
|
267
|
# 1 --> 6
|
210
|
268
|
# 0.016666666666667 --> 9
|
211
|
269
|
# 0.00027777777777778 --> 19
|
212
|
|
- # wolframalpha : quadratic fit { {13, 5}, {1, 6}, {0.0166666, 9}, {0.0002777777,19}}
|
|
270
|
+ # wolframalpha :
|
|
271
|
+ # quadratic fit { {13, 5}, {1, 6}, {0.0166666, 9}, {0.0002777777,19}}
|
213
|
272
|
# 14.1186-8.8322 x+0.625447 x^2
|
214
|
273
|
if precision < 0.0003:
|
215
|
274
|
zoom = 19
|
216
|
275
|
else:
|
217
|
276
|
zoom = int(15 - precision*8.8322 + precision*precision*0.625447)
|
218
|
277
|
|
219
|
|
- url = urlMap.replace('{latitude}', str(value.get('latitude',0))).replace('{longitude}', str(value.get('longitude',0))).replace('{zoom}', str(zoom))
|
|
278
|
+ url = url_map\
|
|
279
|
+ .replace('{latitude}', str(value.get('latitude', 0)))\
|
|
280
|
+ .replace('{longitude}', str(value.get('longitude', 0)))\
|
|
281
|
+ .replace('{zoom}', str(zoom))
|
220
|
282
|
|
221
|
283
|
return url
|
222
|
284
|
|
223
|
285
|
|
224
|
286
|
def get_wikilink(result, wikiid):
|
225
|
287
|
url = result.get('sitelinks', {}).get(wikiid, {}).get('url', None)
|
226
|
|
- if url == None:
|
|
288
|
+ if url is None:
|
227
|
289
|
return url
|
228
|
290
|
elif url.startswith('http://'):
|
229
|
291
|
url = url.replace('http://', 'https://')
|
|
@@ -231,8 +293,9 @@ def get_wikilink(result, wikiid):
|
231
|
293
|
url = 'https:' + url
|
232
|
294
|
return url
|
233
|
295
|
|
|
296
|
+
|
234
|
297
|
def get_wiki_firstlanguage(result, wikipatternid):
|
235
|
298
|
for k in result.get('sitelinks', {}).keys():
|
236
|
|
- if k.endswith(wikipatternid) and len(k)==(2+len(wikipatternid)):
|
|
299
|
+ if k.endswith(wikipatternid) and len(k) == (2+len(wikipatternid)):
|
237
|
300
|
return k[0:2]
|
238
|
301
|
return None
|