Ver código fonte

[fix] dates before 1900

Adam Tauber 10 anos atrás
pai
commit
d7ea44ab8d
1 arquivos alterados com 6 adições e 16 exclusões
  1. 6
    16
      searx/engines/wikidata.py

+ 6
- 16
searx/engines/wikidata.py Ver arquivo

1
 import json
1
 import json
2
 from requests import get
2
 from requests import get
3
 from urllib import urlencode
3
 from urllib import urlencode
4
-import locale
5
-import dateutil.parser
4
+from searx.utils import format_date_by_locale
6
 
5
 
7
 result_count = 1
6
 result_count = 1
8
 wikidata_host = 'https://www.wikidata.org'
7
 wikidata_host = 'https://www.wikidata.org'
38
     if language == 'all':
37
     if language == 'all':
39
         language = 'en'
38
         language = 'en'
40
 
39
 
41
-    try:
42
-        locale.setlocale(locale.LC_ALL, str(resp.search_params['language']))
43
-    except:
44
-        try:
45
-            locale.setlocale(locale.LC_ALL, 'en_US')
46
-        except:
47
-            pass
48
-        pass
49
-
50
     url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
40
     url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
51
                                             'languages': language + '|en'}))
41
                                             'languages': language + '|en'}))
52
 
42
 
53
     htmlresponse = get(url)
43
     htmlresponse = get(url)
54
     jsonresponse = json.loads(htmlresponse.content)
44
     jsonresponse = json.loads(htmlresponse.content)
55
     for wikidata_id in wikidata_ids:
45
     for wikidata_id in wikidata_ids:
56
-        results = results + getDetail(jsonresponse, wikidata_id, language)
46
+        results = results + getDetail(jsonresponse, wikidata_id, language, resp.search_params['language'])
57
 
47
 
58
     return results
48
     return results
59
 
49
 
60
 
50
 
61
-def getDetail(jsonresponse, wikidata_id, language):
51
+def getDetail(jsonresponse, wikidata_id, language, locale):
62
     results = []
52
     results = []
63
     urls = []
53
     urls = []
64
     attributes = []
54
     attributes = []
176
 
166
 
177
     date_of_birth = get_time(claims, 'P569', None)
167
     date_of_birth = get_time(claims, 'P569', None)
178
     if date_of_birth is not None:
168
     if date_of_birth is not None:
179
-        date_of_birth = dateutil.parser.parse(date_of_birth[8:]).strftime(locale.nl_langinfo(locale.D_FMT))
169
+        date_of_birth = format_date_by_locale(date_of_birth[8:], locale)
180
         attributes.append({'label': 'Date of birth', 'value': date_of_birth})
170
         attributes.append({'label': 'Date of birth', 'value': date_of_birth})
181
 
171
 
182
     date_of_death = get_time(claims, 'P570', None)
172
     date_of_death = get_time(claims, 'P570', None)
183
     if date_of_death is not None:
173
     if date_of_death is not None:
184
-        date_of_death = dateutil.parser.parse(date_of_death[8:]).strftime(locale.nl_langinfo(locale.D_FMT))
174
+        date_of_death = format_date_by_locale(date_of_death[8:], locale)
185
         attributes.append({'label': 'Date of death', 'value': date_of_death})
175
         attributes.append({'label': 'Date of death', 'value': date_of_death})
186
 
176
 
187
     if len(attributes) == 0 and len(urls) == 2 and len(description) == 0:
177
     if len(attributes) == 0 and len(urls) == 2 and len(description) == 0:
235
     if len(result) == 0:
225
     if len(result) == 0:
236
         return defaultValue
226
         return defaultValue
237
     else:
227
     else:
238
-        #TODO handle multiple urls
228
+        # TODO handle multiple urls
239
         return result[0]
229
         return result[0]
240
 
230
 
241
 
231