|
@@ -1,56 +1,86 @@
|
1
|
|
-import json
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+"""
|
|
3
|
+ Wikidata
|
|
4
|
+
|
|
5
|
+ @website https://wikidata.org
|
|
6
|
+ @provide-api yes (https://wikidata.org/w/api.php)
|
|
7
|
+
|
|
8
|
+ @using-api partially (most things require scraping)
|
|
9
|
+ @results JSON, HTML
|
|
10
|
+ @stable no (html can change)
|
|
11
|
+ @parse url, infobox
|
|
12
|
+"""
|
2
|
13
|
|
3
|
14
|
from searx import logger
|
4
|
15
|
from searx.poolrequests import get
|
5
|
|
-from searx.utils import format_date_by_locale
|
|
16
|
+from searx.engines.xpath import extract_text
|
6
|
17
|
|
7
|
|
-from datetime import datetime
|
8
|
|
-from dateutil.parser import parse as dateutil_parse
|
|
18
|
+from json import loads
|
|
19
|
+from lxml.html import fromstring
|
9
|
20
|
from urllib import urlencode
|
10
|
21
|
|
11
|
|
-
|
12
|
22
|
logger = logger.getChild('wikidata')
|
13
|
23
|
result_count = 1
|
|
24
|
+
|
|
25
|
+# urls
|
14
|
26
|
wikidata_host = 'https://www.wikidata.org'
|
|
27
|
+url_search = wikidata_host \
|
|
28
|
+ + '/wiki/Special:ItemDisambiguation?{query}'
|
|
29
|
+
|
15
|
30
|
wikidata_api = wikidata_host + '/w/api.php'
|
16
|
|
-url_search = wikidata_api \
|
17
|
|
- + '?action=query&list=search&format=json'\
|
18
|
|
- + '&srnamespace=0&srprop=sectiontitle&{query}'
|
19
|
31
|
url_detail = wikidata_api\
|
20
|
|
- + '?action=wbgetentities&format=json'\
|
21
|
|
- + '&props=labels%7Cinfo%7Csitelinks'\
|
22
|
|
- + '%7Csitelinks%2Furls%7Cdescriptions%7Cclaims'\
|
23
|
|
- + '&{query}'
|
|
32
|
+ + '?action=parse&format=json&{query}'\
|
|
33
|
+ + '&redirects=1&prop=text%7Cdisplaytitle%7Clanglinks%7Crevid'\
|
|
34
|
+ + '&disableeditsection=1&disabletidy=1&preview=1§ionpreview=1&disabletoc=1&utf8=1&formatversion=2'
|
|
35
|
+
|
24
|
36
|
url_map = 'https://www.openstreetmap.org/'\
|
25
|
37
|
+ '?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M'
|
|
38
|
+url_image = 'https://commons.wikimedia.org/wiki/Special:FilePath/{filename}?width=500&height=400'
|
|
39
|
+
|
|
40
|
+# xpaths
|
|
41
|
+wikidata_ids_xpath = '//div/ul[@class="wikibase-disambiguation"]/li/a/@title'
|
|
42
|
+title_xpath = '//*[contains(@class,"wikibase-title-label")]'
|
|
43
|
+description_xpath = '//div[contains(@class,"wikibase-entitytermsview-heading-description")]'
|
|
44
|
+property_xpath = '//div[@id="{propertyid}"]'
|
|
45
|
+label_xpath = './/div[contains(@class,"wikibase-statementgroupview-property-label")]/a'
|
|
46
|
+url_xpath = './/a[contains(@class,"external free") or contains(@class, "wb-external-id")]'
|
|
47
|
+wikilink_xpath = './/ul[contains(@class,"wikibase-sitelinklistview-listview")]'\
|
|
48
|
+ + '/li[contains(@data-wb-siteid,"{wikiid}")]//a/@href'
|
|
49
|
+property_row_xpath = './/div[contains(@class,"wikibase-statementview")]'
|
|
50
|
+preferred_rank_xpath = './/span[contains(@class,"wikibase-rankselector-preferred")]'
|
|
51
|
+value_xpath = './/div[contains(@class,"wikibase-statementview-mainsnak")]'\
|
|
52
|
+ + '/*/div[contains(@class,"wikibase-snakview-value")]'
|
|
53
|
+language_fallback_xpath = '//sup[contains(@class,"wb-language-fallback-indicator")]'
|
|
54
|
+calendar_name_xpath = './/sup[contains(@class,"wb-calendar-name")]'
|
26
|
55
|
|
27
|
56
|
|
28
|
57
|
def request(query, params):
|
|
58
|
+ language = params['language'].split('_')[0]
|
|
59
|
+ if language == 'all':
|
|
60
|
+ language = 'en'
|
|
61
|
+
|
29
|
62
|
params['url'] = url_search.format(
|
30
|
|
- query=urlencode({'srsearch': query,
|
31
|
|
- 'srlimit': result_count}))
|
|
63
|
+ query=urlencode({'label': query,
|
|
64
|
+ 'language': language}))
|
32
|
65
|
return params
|
33
|
66
|
|
34
|
67
|
|
35
|
68
|
def response(resp):
|
36
|
69
|
results = []
|
37
|
|
- search_res = json.loads(resp.text)
|
38
|
|
-
|
39
|
|
- wikidata_ids = set()
|
40
|
|
- for r in search_res.get('query', {}).get('search', {}):
|
41
|
|
- wikidata_ids.add(r.get('title', ''))
|
|
70
|
+ html = fromstring(resp.content)
|
|
71
|
+ wikidata_ids = html.xpath(wikidata_ids_xpath)
|
42
|
72
|
|
43
|
73
|
language = resp.search_params['language'].split('_')[0]
|
44
|
74
|
if language == 'all':
|
45
|
75
|
language = 'en'
|
46
|
76
|
|
47
|
|
- url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
|
48
|
|
- 'languages': language + '|en'}))
|
49
|
|
-
|
50
|
|
- htmlresponse = get(url)
|
51
|
|
- jsonresponse = json.loads(htmlresponse.content)
|
52
|
|
- for wikidata_id in wikidata_ids:
|
53
|
|
- results = results + getDetail(jsonresponse, wikidata_id, language, resp.search_params['language'])
|
|
77
|
+ # TODO: make requests asynchronous to avoid timeout when result_count > 1
|
|
78
|
+ for wikidata_id in wikidata_ids[:result_count]:
|
|
79
|
+ url = url_detail.format(query=urlencode({'page': wikidata_id,
|
|
80
|
+ 'uselang': language}))
|
|
81
|
+ htmlresponse = get(url)
|
|
82
|
+ jsonresponse = loads(htmlresponse.content)
|
|
83
|
+ results += getDetail(jsonresponse, wikidata_id, language, resp.search_params['language'])
|
54
|
84
|
|
55
|
85
|
return results
|
56
|
86
|
|
|
@@ -60,124 +90,206 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
|
60
|
90
|
urls = []
|
61
|
91
|
attributes = []
|
62
|
92
|
|
63
|
|
- result = jsonresponse.get('entities', {}).get(wikidata_id, {})
|
|
93
|
+ title = jsonresponse.get('parse', {}).get('displaytitle', {})
|
|
94
|
+ result = jsonresponse.get('parse', {}).get('text', {})
|
64
|
95
|
|
65
|
|
- title = result.get('labels', {}).get(language, {}).get('value', None)
|
66
|
|
- if title is None:
|
67
|
|
- title = result.get('labels', {}).get('en', {}).get('value', None)
|
68
|
|
- if title is None:
|
|
96
|
+ if not title or not result:
|
69
|
97
|
return results
|
70
|
98
|
|
71
|
|
- description = result\
|
72
|
|
- .get('descriptions', {})\
|
73
|
|
- .get(language, {})\
|
74
|
|
- .get('value', None)
|
|
99
|
+ title = fromstring(title)
|
|
100
|
+ for elem in title.xpath(language_fallback_xpath):
|
|
101
|
+ elem.getparent().remove(elem)
|
|
102
|
+ title = extract_text(title.xpath(title_xpath))
|
75
|
103
|
|
76
|
|
- if description is None:
|
77
|
|
- description = result\
|
78
|
|
- .get('descriptions', {})\
|
79
|
|
- .get('en', {})\
|
80
|
|
- .get('value', '')
|
|
104
|
+ result = fromstring(result)
|
|
105
|
+ for elem in result.xpath(language_fallback_xpath):
|
|
106
|
+ elem.getparent().remove(elem)
|
81
|
107
|
|
82
|
|
- claims = result.get('claims', {})
|
83
|
|
- official_website = get_string(claims, 'P856', None)
|
84
|
|
- if official_website is not None:
|
85
|
|
- urls.append({'title': 'Official site', 'url': official_website})
|
86
|
|
- results.append({'title': title, 'url': official_website})
|
|
108
|
+ description = extract_text(result.xpath(description_xpath))
|
87
|
109
|
|
88
|
|
- wikipedia_link_count = 0
|
89
|
|
- wikipedia_link = get_wikilink(result, language + 'wiki')
|
90
|
|
- wikipedia_link_count += add_url(urls,
|
91
|
|
- 'Wikipedia (' + language + ')',
|
92
|
|
- wikipedia_link)
|
93
|
|
- if language != 'en':
|
94
|
|
- wikipedia_en_link = get_wikilink(result, 'enwiki')
|
95
|
|
- wikipedia_link_count += add_url(urls,
|
96
|
|
- 'Wikipedia (en)',
|
97
|
|
- wikipedia_en_link)
|
98
|
|
- if wikipedia_link_count == 0:
|
99
|
|
- misc_language = get_wiki_firstlanguage(result, 'wiki')
|
100
|
|
- if misc_language is not None:
|
101
|
|
- add_url(urls,
|
102
|
|
- 'Wikipedia (' + misc_language + ')',
|
103
|
|
- get_wikilink(result, misc_language + 'wiki'))
|
|
110
|
+ # URLS
|
104
|
111
|
|
105
|
|
- if language != 'en':
|
106
|
|
- add_url(urls,
|
107
|
|
- 'Wiki voyage (' + language + ')',
|
108
|
|
- get_wikilink(result, language + 'wikivoyage'))
|
|
112
|
+ # official website
|
|
113
|
+ add_url(urls, result, 'P856', results=results)
|
109
|
114
|
|
110
|
|
- add_url(urls,
|
111
|
|
- 'Wiki voyage (en)',
|
112
|
|
- get_wikilink(result, 'enwikivoyage'))
|
|
115
|
+ # wikipedia
|
|
116
|
+ wikipedia_link_count = 0
|
|
117
|
+ wikipedia_link = get_wikilink(result, language + 'wiki')
|
|
118
|
+ if wikipedia_link:
|
|
119
|
+ wikipedia_link_count += 1
|
|
120
|
+ urls.append({'title': 'Wikipedia (' + language + ')',
|
|
121
|
+ 'url': wikipedia_link})
|
113
|
122
|
|
114
|
123
|
if language != 'en':
|
115
|
|
- add_url(urls,
|
116
|
|
- 'Wikiquote (' + language + ')',
|
117
|
|
- get_wikilink(result, language + 'wikiquote'))
|
118
|
|
-
|
119
|
|
- add_url(urls,
|
120
|
|
- 'Wikiquote (en)',
|
121
|
|
- get_wikilink(result, 'enwikiquote'))
|
122
|
|
-
|
123
|
|
- add_url(urls,
|
124
|
|
- 'Commons wiki',
|
125
|
|
- get_wikilink(result, 'commonswiki'))
|
126
|
|
-
|
127
|
|
- add_url(urls,
|
128
|
|
- 'Location',
|
129
|
|
- get_geolink(claims, 'P625', None))
|
130
|
|
-
|
131
|
|
- add_url(urls,
|
132
|
|
- 'Wikidata',
|
133
|
|
- 'https://www.wikidata.org/wiki/'
|
134
|
|
- + wikidata_id + '?uselang=' + language)
|
135
|
|
-
|
136
|
|
- musicbrainz_work_id = get_string(claims, 'P435')
|
137
|
|
- if musicbrainz_work_id is not None:
|
138
|
|
- add_url(urls,
|
139
|
|
- 'MusicBrainz',
|
140
|
|
- 'http://musicbrainz.org/work/'
|
141
|
|
- + musicbrainz_work_id)
|
142
|
|
-
|
143
|
|
- musicbrainz_artist_id = get_string(claims, 'P434')
|
144
|
|
- if musicbrainz_artist_id is not None:
|
145
|
|
- add_url(urls,
|
146
|
|
- 'MusicBrainz',
|
147
|
|
- 'http://musicbrainz.org/artist/'
|
148
|
|
- + musicbrainz_artist_id)
|
149
|
|
-
|
150
|
|
- musicbrainz_release_group_id = get_string(claims, 'P436')
|
151
|
|
- if musicbrainz_release_group_id is not None:
|
152
|
|
- add_url(urls,
|
153
|
|
- 'MusicBrainz',
|
154
|
|
- 'http://musicbrainz.org/release-group/'
|
155
|
|
- + musicbrainz_release_group_id)
|
156
|
|
-
|
157
|
|
- musicbrainz_label_id = get_string(claims, 'P966')
|
158
|
|
- if musicbrainz_label_id is not None:
|
159
|
|
- add_url(urls,
|
160
|
|
- 'MusicBrainz',
|
161
|
|
- 'http://musicbrainz.org/label/'
|
162
|
|
- + musicbrainz_label_id)
|
163
|
|
-
|
164
|
|
- # musicbrainz_area_id = get_string(claims, 'P982')
|
165
|
|
- # P1407 MusicBrainz series ID
|
166
|
|
- # P1004 MusicBrainz place ID
|
167
|
|
- # P1330 MusicBrainz instrument ID
|
168
|
|
- # P1407 MusicBrainz series ID
|
169
|
|
-
|
170
|
|
- postal_code = get_string(claims, 'P281', None)
|
171
|
|
- if postal_code is not None:
|
172
|
|
- attributes.append({'label': 'Postal code(s)', 'value': postal_code})
|
173
|
|
-
|
174
|
|
- date_of_birth = get_time(claims, 'P569', locale, None)
|
175
|
|
- if date_of_birth is not None:
|
176
|
|
- attributes.append({'label': 'Date of birth', 'value': date_of_birth})
|
177
|
|
-
|
178
|
|
- date_of_death = get_time(claims, 'P570', locale, None)
|
179
|
|
- if date_of_death is not None:
|
180
|
|
- attributes.append({'label': 'Date of death', 'value': date_of_death})
|
|
124
|
+ wikipedia_en_link = get_wikilink(result, 'enwiki')
|
|
125
|
+ if wikipedia_en_link:
|
|
126
|
+ wikipedia_link_count += 1
|
|
127
|
+ urls.append({'title': 'Wikipedia (en)',
|
|
128
|
+ 'url': wikipedia_en_link})
|
|
129
|
+
|
|
130
|
+ # TODO: get_wiki_firstlanguage
|
|
131
|
+ # if wikipedia_link_count == 0:
|
|
132
|
+
|
|
133
|
+ # more wikis
|
|
134
|
+ add_url(urls, result, default_label='Wikivoyage (' + language + ')', link_type=language + 'wikivoyage')
|
|
135
|
+ add_url(urls, result, default_label='Wikiquote (' + language + ')', link_type=language + 'wikiquote')
|
|
136
|
+ add_url(urls, result, default_label='Wikimedia Commons', link_type='commonswiki')
|
|
137
|
+
|
|
138
|
+ add_url(urls, result, 'P625', 'OpenStreetMap', link_type='geo')
|
|
139
|
+
|
|
140
|
+ # musicbrainz
|
|
141
|
+ add_url(urls, result, 'P434', 'MusicBrainz', 'http://musicbrainz.org/artist/')
|
|
142
|
+ add_url(urls, result, 'P435', 'MusicBrainz', 'http://musicbrainz.org/work/')
|
|
143
|
+ add_url(urls, result, 'P436', 'MusicBrainz', 'http://musicbrainz.org/release-group/')
|
|
144
|
+ add_url(urls, result, 'P966', 'MusicBrainz', 'http://musicbrainz.org/label/')
|
|
145
|
+
|
|
146
|
+ # IMDb
|
|
147
|
+ add_url(urls, result, 'P345', 'IMDb', 'https://www.imdb.com/', link_type='imdb')
|
|
148
|
+ # source code repository
|
|
149
|
+ add_url(urls, result, 'P1324')
|
|
150
|
+ # blog
|
|
151
|
+ add_url(urls, result, 'P1581')
|
|
152
|
+ # social media links
|
|
153
|
+ add_url(urls, result, 'P2397', 'YouTube', 'https://www.youtube.com/channel/')
|
|
154
|
+ add_url(urls, result, 'P1651', 'YouTube', 'https://www.youtube.com/watch?v=')
|
|
155
|
+ add_url(urls, result, 'P2002', 'Twitter', 'https://twitter.com/')
|
|
156
|
+ add_url(urls, result, 'P2013', 'Facebook', 'https://facebook.com/')
|
|
157
|
+ add_url(urls, result, 'P2003', 'Instagram', 'https://instagram.com/')
|
|
158
|
+
|
|
159
|
+ urls.append({'title': 'Wikidata',
|
|
160
|
+ 'url': 'https://www.wikidata.org/wiki/'
|
|
161
|
+ + wikidata_id + '?uselang=' + language})
|
|
162
|
+
|
|
163
|
+ # INFOBOX ATTRIBUTES (ROWS)
|
|
164
|
+
|
|
165
|
+ # DATES
|
|
166
|
+ # inception date
|
|
167
|
+ add_attribute(attributes, result, 'P571', date=True)
|
|
168
|
+ # dissolution date
|
|
169
|
+ add_attribute(attributes, result, 'P576', date=True)
|
|
170
|
+ # start date
|
|
171
|
+ add_attribute(attributes, result, 'P580', date=True)
|
|
172
|
+ # end date
|
|
173
|
+ add_attribute(attributes, result, 'P582', date=True)
|
|
174
|
+ # date of birth
|
|
175
|
+ add_attribute(attributes, result, 'P569', date=True)
|
|
176
|
+ # date of death
|
|
177
|
+ add_attribute(attributes, result, 'P570', date=True)
|
|
178
|
+ # date of spacecraft launch
|
|
179
|
+ add_attribute(attributes, result, 'P619', date=True)
|
|
180
|
+ # date of spacecraft landing
|
|
181
|
+ add_attribute(attributes, result, 'P620', date=True)
|
|
182
|
+
|
|
183
|
+ # nationality
|
|
184
|
+ add_attribute(attributes, result, 'P27')
|
|
185
|
+ # country of origin
|
|
186
|
+ add_attribute(attributes, result, 'P495')
|
|
187
|
+ # country
|
|
188
|
+ add_attribute(attributes, result, 'P17')
|
|
189
|
+ # headquarters
|
|
190
|
+ add_attribute(attributes, result, 'Q180')
|
|
191
|
+
|
|
192
|
+ # PLACES
|
|
193
|
+ # capital
|
|
194
|
+ add_attribute(attributes, result, 'P36', trim=True)
|
|
195
|
+ # head of state
|
|
196
|
+ add_attribute(attributes, result, 'P35', trim=True)
|
|
197
|
+ # head of government
|
|
198
|
+ add_attribute(attributes, result, 'P6', trim=True)
|
|
199
|
+ # type of government
|
|
200
|
+ add_attribute(attributes, result, 'P122')
|
|
201
|
+ # official language
|
|
202
|
+ add_attribute(attributes, result, 'P37')
|
|
203
|
+ # population
|
|
204
|
+ add_attribute(attributes, result, 'P1082', trim=True)
|
|
205
|
+ # area
|
|
206
|
+ add_attribute(attributes, result, 'P2046')
|
|
207
|
+ # currency
|
|
208
|
+ add_attribute(attributes, result, 'P38', trim=True)
|
|
209
|
+ # heigth (building)
|
|
210
|
+ add_attribute(attributes, result, 'P2048')
|
|
211
|
+
|
|
212
|
+ # MEDIA
|
|
213
|
+ # platform (videogames)
|
|
214
|
+ add_attribute(attributes, result, 'P400')
|
|
215
|
+ # author
|
|
216
|
+ add_attribute(attributes, result, 'P50')
|
|
217
|
+ # creator
|
|
218
|
+ add_attribute(attributes, result, 'P170')
|
|
219
|
+ # director
|
|
220
|
+ add_attribute(attributes, result, 'P57')
|
|
221
|
+ # performer
|
|
222
|
+ add_attribute(attributes, result, 'P175')
|
|
223
|
+ # developer
|
|
224
|
+ add_attribute(attributes, result, 'P178')
|
|
225
|
+ # producer
|
|
226
|
+ add_attribute(attributes, result, 'P162')
|
|
227
|
+ # manufacturer
|
|
228
|
+ add_attribute(attributes, result, 'P176')
|
|
229
|
+ # screenwriter
|
|
230
|
+ add_attribute(attributes, result, 'P58')
|
|
231
|
+ # production company
|
|
232
|
+ add_attribute(attributes, result, 'P272')
|
|
233
|
+ # record label
|
|
234
|
+ add_attribute(attributes, result, 'P264')
|
|
235
|
+ # publisher
|
|
236
|
+ add_attribute(attributes, result, 'P123')
|
|
237
|
+ # original network
|
|
238
|
+ add_attribute(attributes, result, 'P449')
|
|
239
|
+ # distributor
|
|
240
|
+ add_attribute(attributes, result, 'P750')
|
|
241
|
+ # composer
|
|
242
|
+ add_attribute(attributes, result, 'P86')
|
|
243
|
+ # publication date
|
|
244
|
+ add_attribute(attributes, result, 'P577', date=True)
|
|
245
|
+ # genre
|
|
246
|
+ add_attribute(attributes, result, 'P136')
|
|
247
|
+ # original language
|
|
248
|
+ add_attribute(attributes, result, 'P364')
|
|
249
|
+ # isbn
|
|
250
|
+ add_attribute(attributes, result, 'Q33057')
|
|
251
|
+ # software license
|
|
252
|
+ add_attribute(attributes, result, 'P275')
|
|
253
|
+ # programming language
|
|
254
|
+ add_attribute(attributes, result, 'P277')
|
|
255
|
+ # version
|
|
256
|
+ add_attribute(attributes, result, 'P348', trim=True)
|
|
257
|
+ # narrative location
|
|
258
|
+ add_attribute(attributes, result, 'P840')
|
|
259
|
+
|
|
260
|
+ # LANGUAGES
|
|
261
|
+ # number of speakers
|
|
262
|
+ add_attribute(attributes, result, 'P1098')
|
|
263
|
+ # writing system
|
|
264
|
+ add_attribute(attributes, result, 'P282')
|
|
265
|
+ # regulatory body
|
|
266
|
+ add_attribute(attributes, result, 'P1018')
|
|
267
|
+ # language code
|
|
268
|
+ add_attribute(attributes, result, 'P218')
|
|
269
|
+
|
|
270
|
+ # OTHER
|
|
271
|
+ # ceo
|
|
272
|
+ add_attribute(attributes, result, 'P169', trim=True)
|
|
273
|
+ # founder
|
|
274
|
+ add_attribute(attributes, result, 'P112')
|
|
275
|
+ # legal form (company/organization)
|
|
276
|
+ add_attribute(attributes, result, 'P1454')
|
|
277
|
+ # operator
|
|
278
|
+ add_attribute(attributes, result, 'P137')
|
|
279
|
+ # crew members (tripulation)
|
|
280
|
+ add_attribute(attributes, result, 'P1029')
|
|
281
|
+ # taxon
|
|
282
|
+ add_attribute(attributes, result, 'P225')
|
|
283
|
+ # chemical formula
|
|
284
|
+ add_attribute(attributes, result, 'P274')
|
|
285
|
+ # winner (sports/contests)
|
|
286
|
+ add_attribute(attributes, result, 'P1346')
|
|
287
|
+ # number of deaths
|
|
288
|
+ add_attribute(attributes, result, 'P1120')
|
|
289
|
+ # currency code
|
|
290
|
+ add_attribute(attributes, result, 'P498')
|
|
291
|
+
|
|
292
|
+ image = add_image(result)
|
181
|
293
|
|
182
|
294
|
if len(attributes) == 0 and len(urls) == 2 and len(description) == 0:
|
183
|
295
|
results.append({
|
|
@@ -190,6 +302,7 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
|
190
|
302
|
'infobox': title,
|
191
|
303
|
'id': wikipedia_link,
|
192
|
304
|
'content': description,
|
|
305
|
+ 'img_src': image,
|
193
|
306
|
'attributes': attributes,
|
194
|
307
|
'urls': urls
|
195
|
308
|
})
|
|
@@ -197,92 +310,151 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
|
197
|
310
|
return results
|
198
|
311
|
|
199
|
312
|
|
200
|
|
-def add_url(urls, title, url):
|
201
|
|
- if url is not None:
|
202
|
|
- urls.append({'title': title, 'url': url})
|
203
|
|
- return 1
|
|
313
|
+# only returns first match
|
|
314
|
+def add_image(result):
|
|
315
|
+ # P15: route map, P242: locator map, P154: logo, P18: image, P242: map, P41: flag, P2716: collage, P2910: icon
|
|
316
|
+ property_ids = ['P15', 'P242', 'P154', 'P18', 'P242', 'P41', 'P2716', 'P2910']
|
|
317
|
+
|
|
318
|
+ for property_id in property_ids:
|
|
319
|
+ image = result.xpath(property_xpath.replace('{propertyid}', property_id))
|
|
320
|
+ if image:
|
|
321
|
+ image_name = image[0].xpath(value_xpath)
|
|
322
|
+ image_src = url_image.replace('{filename}', extract_text(image_name[0]))
|
|
323
|
+ return image_src
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+# setting trim will only returned high ranked rows OR the first row
|
|
327
|
+def add_attribute(attributes, result, property_id, default_label=None, date=False, trim=False):
|
|
328
|
+ attribute = result.xpath(property_xpath.replace('{propertyid}', property_id))
|
|
329
|
+ if attribute:
|
|
330
|
+
|
|
331
|
+ if default_label:
|
|
332
|
+ label = default_label
|
|
333
|
+ else:
|
|
334
|
+ label = extract_text(attribute[0].xpath(label_xpath))
|
|
335
|
+ label = label[0].upper() + label[1:]
|
|
336
|
+
|
|
337
|
+ if date:
|
|
338
|
+ trim = True
|
|
339
|
+ # remove calendar name
|
|
340
|
+ calendar_name = attribute[0].xpath(calendar_name_xpath)
|
|
341
|
+ for calendar in calendar_name:
|
|
342
|
+ calendar.getparent().remove(calendar)
|
|
343
|
+
|
|
344
|
+ concat_values = ""
|
|
345
|
+ values = []
|
|
346
|
+ first_value = None
|
|
347
|
+ for row in attribute[0].xpath(property_row_xpath):
|
|
348
|
+ if not first_value or not trim or row.xpath(preferred_rank_xpath):
|
|
349
|
+
|
|
350
|
+ value = row.xpath(value_xpath)
|
|
351
|
+ if not value:
|
|
352
|
+ continue
|
|
353
|
+ value = extract_text(value)
|
|
354
|
+
|
|
355
|
+ # save first value in case no ranked row is found
|
|
356
|
+ if trim and not first_value:
|
|
357
|
+ first_value = value
|
|
358
|
+ else:
|
|
359
|
+ # to avoid duplicate values
|
|
360
|
+ if value not in values:
|
|
361
|
+ concat_values += value + ", "
|
|
362
|
+ values.append(value)
|
|
363
|
+
|
|
364
|
+ if trim and not values:
|
|
365
|
+ attributes.append({'label': label,
|
|
366
|
+ 'value': first_value})
|
|
367
|
+ else:
|
|
368
|
+ attributes.append({'label': label,
|
|
369
|
+ 'value': concat_values[:-2]})
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+# requires property_id unless it's a wiki link (defined in link_type)
|
|
373
|
+def add_url(urls, result, property_id=None, default_label=None, url_prefix=None, results=None, link_type=None):
|
|
374
|
+ links = []
|
|
375
|
+
|
|
376
|
+ # wiki links don't have property in wikidata page
|
|
377
|
+ if link_type and 'wiki' in link_type:
|
|
378
|
+ links.append(get_wikilink(result, link_type))
|
204
|
379
|
else:
|
205
|
|
- return 0
|
|
380
|
+ dom_element = result.xpath(property_xpath.replace('{propertyid}', property_id))
|
|
381
|
+ if dom_element:
|
|
382
|
+ dom_element = dom_element[0]
|
|
383
|
+ if not default_label:
|
|
384
|
+ label = extract_text(dom_element.xpath(label_xpath))
|
|
385
|
+ label = label[0].upper() + label[1:]
|
|
386
|
+
|
|
387
|
+ if link_type == 'geo':
|
|
388
|
+ links.append(get_geolink(dom_element))
|
|
389
|
+
|
|
390
|
+ elif link_type == 'imdb':
|
|
391
|
+ links.append(get_imdblink(dom_element, url_prefix))
|
|
392
|
+
|
|
393
|
+ else:
|
|
394
|
+ url_results = dom_element.xpath(url_xpath)
|
|
395
|
+ for link in url_results:
|
|
396
|
+ if link is not None:
|
|
397
|
+ if url_prefix:
|
|
398
|
+ link = url_prefix + extract_text(link)
|
|
399
|
+ else:
|
|
400
|
+ link = extract_text(link)
|
|
401
|
+ links.append(link)
|
|
402
|
+
|
|
403
|
+ # append urls
|
|
404
|
+ for url in links:
|
|
405
|
+ if url is not None:
|
|
406
|
+ urls.append({'title': default_label or label,
|
|
407
|
+ 'url': url})
|
|
408
|
+ if results is not None:
|
|
409
|
+ results.append({'title': default_label or label,
|
|
410
|
+ 'url': url})
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+def get_imdblink(result, url_prefix):
|
|
414
|
+ imdb_id = result.xpath(value_xpath)
|
|
415
|
+ if imdb_id:
|
|
416
|
+ imdb_id = extract_text(imdb_id)
|
|
417
|
+ id_prefix = imdb_id[:2]
|
|
418
|
+ if id_prefix == 'tt':
|
|
419
|
+ url = url_prefix + 'title/' + imdb_id
|
|
420
|
+ elif id_prefix == 'nm':
|
|
421
|
+ url = url_prefix + 'name/' + imdb_id
|
|
422
|
+ elif id_prefix == 'ch':
|
|
423
|
+ url = url_prefix + 'character/' + imdb_id
|
|
424
|
+ elif id_prefix == 'co':
|
|
425
|
+ url = url_prefix + 'company/' + imdb_id
|
|
426
|
+ elif id_prefix == 'ev':
|
|
427
|
+ url = url_prefix + 'event/' + imdb_id
|
|
428
|
+ else:
|
|
429
|
+ url = None
|
|
430
|
+ return url
|
206
|
431
|
|
207
|
432
|
|
208
|
|
-def get_mainsnak(claims, propertyName):
|
209
|
|
- propValue = claims.get(propertyName, {})
|
210
|
|
- if len(propValue) == 0:
|
|
433
|
+def get_geolink(result):
|
|
434
|
+ coordinates = result.xpath(value_xpath)
|
|
435
|
+ if not coordinates:
|
211
|
436
|
return None
|
212
|
|
-
|
213
|
|
- propValue = propValue[0].get('mainsnak', None)
|
214
|
|
- return propValue
|
215
|
|
-
|
216
|
|
-
|
217
|
|
-def get_string(claims, propertyName, defaultValue=None):
|
218
|
|
- propValue = claims.get(propertyName, {})
|
219
|
|
- if len(propValue) == 0:
|
220
|
|
- return defaultValue
|
221
|
|
-
|
222
|
|
- result = []
|
223
|
|
- for e in propValue:
|
224
|
|
- mainsnak = e.get('mainsnak', {})
|
225
|
|
-
|
226
|
|
- datavalue = mainsnak.get('datavalue', {})
|
227
|
|
- if datavalue is not None:
|
228
|
|
- result.append(datavalue.get('value', ''))
|
229
|
|
-
|
230
|
|
- if len(result) == 0:
|
231
|
|
- return defaultValue
|
232
|
|
- else:
|
233
|
|
- # TODO handle multiple urls
|
234
|
|
- return result[0]
|
235
|
|
-
|
236
|
|
-
|
237
|
|
-def get_time(claims, propertyName, locale, defaultValue=None):
|
238
|
|
- propValue = claims.get(propertyName, {})
|
239
|
|
- if len(propValue) == 0:
|
240
|
|
- return defaultValue
|
241
|
|
-
|
242
|
|
- result = []
|
243
|
|
- for e in propValue:
|
244
|
|
- mainsnak = e.get('mainsnak', {})
|
245
|
|
-
|
246
|
|
- datavalue = mainsnak.get('datavalue', {})
|
247
|
|
- if datavalue is not None:
|
248
|
|
- value = datavalue.get('value', '')
|
249
|
|
- result.append(value.get('time', ''))
|
250
|
|
-
|
251
|
|
- if len(result) == 0:
|
252
|
|
- date_string = defaultValue
|
253
|
|
- else:
|
254
|
|
- date_string = ', '.join(result)
|
255
|
|
-
|
256
|
|
- try:
|
257
|
|
- parsed_date = datetime.strptime(date_string, "+%Y-%m-%dT%H:%M:%SZ")
|
258
|
|
- except:
|
259
|
|
- if date_string.startswith('-'):
|
260
|
|
- return date_string.split('T')[0]
|
261
|
|
- try:
|
262
|
|
- parsed_date = dateutil_parse(date_string, fuzzy=False, default=False)
|
263
|
|
- except:
|
264
|
|
- logger.debug('could not parse date %s', date_string)
|
265
|
|
- return date_string.split('T')[0]
|
266
|
|
-
|
267
|
|
- return format_date_by_locale(parsed_date, locale)
|
268
|
|
-
|
269
|
|
-
|
270
|
|
-def get_geolink(claims, propertyName, defaultValue=''):
|
271
|
|
- mainsnak = get_mainsnak(claims, propertyName)
|
272
|
|
-
|
273
|
|
- if mainsnak is None:
|
274
|
|
- return defaultValue
|
275
|
|
-
|
276
|
|
- datatype = mainsnak.get('datatype', '')
|
277
|
|
- datavalue = mainsnak.get('datavalue', {})
|
278
|
|
-
|
279
|
|
- if datatype != 'globe-coordinate':
|
280
|
|
- return defaultValue
|
281
|
|
-
|
282
|
|
- value = datavalue.get('value', {})
|
283
|
|
-
|
284
|
|
- precision = value.get('precision', 0.0002)
|
285
|
|
-
|
|
437
|
+ coordinates = extract_text(coordinates[0])
|
|
438
|
+ latitude, longitude = coordinates.split(',')
|
|
439
|
+
|
|
440
|
+ # convert to decimal
|
|
441
|
+ lat = int(latitude[:latitude.find(u'°')])
|
|
442
|
+ if latitude.find('\'') >= 0:
|
|
443
|
+ lat += int(latitude[latitude.find(u'°') + 1:latitude.find('\'')] or 0) / 60.0
|
|
444
|
+ if latitude.find('"') >= 0:
|
|
445
|
+ lat += float(latitude[latitude.find('\'') + 1:latitude.find('"')] or 0) / 3600.0
|
|
446
|
+ if latitude.find('S') >= 0:
|
|
447
|
+ lat *= -1
|
|
448
|
+ lon = int(longitude[:longitude.find(u'°')])
|
|
449
|
+ if longitude.find('\'') >= 0:
|
|
450
|
+ lon += int(longitude[longitude.find(u'°') + 1:longitude.find('\'')] or 0) / 60.0
|
|
451
|
+ if longitude.find('"') >= 0:
|
|
452
|
+ lon += float(longitude[longitude.find('\'') + 1:longitude.find('"')] or 0) / 3600.0
|
|
453
|
+ if longitude.find('W') >= 0:
|
|
454
|
+ lon *= -1
|
|
455
|
+
|
|
456
|
+ # TODO: get precision
|
|
457
|
+ precision = 0.0002
|
286
|
458
|
# there is no zoom information, deduce from precision (error prone)
|
287
|
459
|
# samples :
|
288
|
460
|
# 13 --> 5
|
|
@@ -298,26 +470,20 @@ def get_geolink(claims, propertyName, defaultValue=''):
|
298
|
470
|
zoom = int(15 - precision * 8.8322 + precision * precision * 0.625447)
|
299
|
471
|
|
300
|
472
|
url = url_map\
|
301
|
|
- .replace('{latitude}', str(value.get('latitude', 0)))\
|
302
|
|
- .replace('{longitude}', str(value.get('longitude', 0)))\
|
|
473
|
+ .replace('{latitude}', str(lat))\
|
|
474
|
+ .replace('{longitude}', str(lon))\
|
303
|
475
|
.replace('{zoom}', str(zoom))
|
304
|
476
|
|
305
|
477
|
return url
|
306
|
478
|
|
307
|
479
|
|
308
|
480
|
def get_wikilink(result, wikiid):
|
309
|
|
- url = result.get('sitelinks', {}).get(wikiid, {}).get('url', None)
|
310
|
|
- if url is None:
|
311
|
|
- return url
|
312
|
|
- elif url.startswith('http://'):
|
|
481
|
+ url = result.xpath(wikilink_xpath.replace('{wikiid}', wikiid))
|
|
482
|
+ if not url:
|
|
483
|
+ return None
|
|
484
|
+ url = url[0]
|
|
485
|
+ if url.startswith('http://'):
|
313
|
486
|
url = url.replace('http://', 'https://')
|
314
|
487
|
elif url.startswith('//'):
|
315
|
488
|
url = 'https:' + url
|
316
|
489
|
return url
|
317
|
|
-
|
318
|
|
-
|
319
|
|
-def get_wiki_firstlanguage(result, wikipatternid):
|
320
|
|
- for k in result.get('sitelinks', {}).keys():
|
321
|
|
- if k.endswith(wikipatternid) and len(k) == (2 + len(wikipatternid)):
|
322
|
|
- return k[0:2]
|
323
|
|
- return None
|