|
@@ -1,8 +1,15 @@
|
1
|
1
|
import json
|
2
|
|
-from urllib import urlencode
|
|
2
|
+
|
|
3
|
+from searx import logger
|
3
|
4
|
from searx.poolrequests import get
|
4
|
5
|
from searx.utils import format_date_by_locale
|
5
|
6
|
|
|
7
|
+from datetime import datetime
|
|
8
|
+from dateutil.parser import parse as dateutil_parse
|
|
9
|
+from urllib import urlencode
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+logger = logger.getChild('wikidata')
|
6
|
13
|
result_count = 1
|
7
|
14
|
wikidata_host = 'https://www.wikidata.org'
|
8
|
15
|
wikidata_api = wikidata_host + '/w/api.php'
|
|
@@ -164,14 +171,12 @@ def getDetail(jsonresponse, wikidata_id, language, locale):
|
164
|
171
|
if postal_code is not None:
|
165
|
172
|
attributes.append({'label': 'Postal code(s)', 'value': postal_code})
|
166
|
173
|
|
167
|
|
- date_of_birth = get_time(claims, 'P569', None)
|
|
174
|
+ date_of_birth = get_time(claims, 'P569', locale, None)
|
168
|
175
|
if date_of_birth is not None:
|
169
|
|
- date_of_birth = format_date_by_locale(date_of_birth[8:], locale)
|
170
|
176
|
attributes.append({'label': 'Date of birth', 'value': date_of_birth})
|
171
|
177
|
|
172
|
|
- date_of_death = get_time(claims, 'P570', None)
|
|
178
|
+ date_of_death = get_time(claims, 'P570', locale, None)
|
173
|
179
|
if date_of_death is not None:
|
174
|
|
- date_of_death = format_date_by_locale(date_of_death[8:], locale)
|
175
|
180
|
attributes.append({'label': 'Date of death', 'value': date_of_death})
|
176
|
181
|
|
177
|
182
|
if len(attributes) == 0 and len(urls) == 2 and len(description) == 0:
|
|
@@ -229,7 +234,7 @@ def get_string(claims, propertyName, defaultValue=None):
|
229
|
234
|
return result[0]
|
230
|
235
|
|
231
|
236
|
|
232
|
|
-def get_time(claims, propertyName, defaultValue=None):
|
|
237
|
+def get_time(claims, propertyName, locale, defaultValue=None):
|
233
|
238
|
propValue = claims.get(propertyName, {})
|
234
|
239
|
if len(propValue) == 0:
|
235
|
240
|
return defaultValue
|
|
@@ -244,9 +249,22 @@ def get_time(claims, propertyName, defaultValue=None):
|
244
|
249
|
result.append(value.get('time', ''))
|
245
|
250
|
|
246
|
251
|
if len(result) == 0:
|
247
|
|
- return defaultValue
|
|
252
|
+ date_string = defaultValue
|
248
|
253
|
else:
|
249
|
|
- return ', '.join(result)
|
|
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)
|
250
|
268
|
|
251
|
269
|
|
252
|
270
|
def get_geolink(claims, propertyName, defaultValue=''):
|