Selaa lähdekoodia

[fix] dates before 1900

Adam Tauber 10 vuotta sitten
vanhempi
commit
d7ea44ab8d
1 muutettua tiedostoa jossa 6 lisäystä ja 16 poistoa
  1. 6
    16
      searx/engines/wikidata.py

+ 6
- 16
searx/engines/wikidata.py Näytä tiedosto

@@ -1,8 +1,7 @@
1 1
 import json
2 2
 from requests import get
3 3
 from urllib import urlencode
4
-import locale
5
-import dateutil.parser
4
+from searx.utils import format_date_by_locale
6 5
 
7 6
 result_count = 1
8 7
 wikidata_host = 'https://www.wikidata.org'
@@ -38,27 +37,18 @@ def response(resp):
38 37
     if language == 'all':
39 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 40
     url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
51 41
                                             'languages': language + '|en'}))
52 42
 
53 43
     htmlresponse = get(url)
54 44
     jsonresponse = json.loads(htmlresponse.content)
55 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 48
     return results
59 49
 
60 50
 
61
-def getDetail(jsonresponse, wikidata_id, language):
51
+def getDetail(jsonresponse, wikidata_id, language, locale):
62 52
     results = []
63 53
     urls = []
64 54
     attributes = []
@@ -176,12 +166,12 @@ def getDetail(jsonresponse, wikidata_id, language):
176 166
 
177 167
     date_of_birth = get_time(claims, 'P569', None)
178 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 170
         attributes.append({'label': 'Date of birth', 'value': date_of_birth})
181 171
 
182 172
     date_of_death = get_time(claims, 'P570', None)
183 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 175
         attributes.append({'label': 'Date of death', 'value': date_of_death})
186 176
 
187 177
     if len(attributes) == 0 and len(urls) == 2 and len(description) == 0:
@@ -235,7 +225,7 @@ def get_string(claims, propertyName, defaultValue=None):
235 225
     if len(result) == 0:
236 226
         return defaultValue
237 227
     else:
238
-        #TODO handle multiple urls
228
+        # TODO handle multiple urls
239 229
         return result[0]
240 230
 
241 231