Browse Source

Use human readable date

For DoB and DoD, wikipedia use a non standard ISO format, not easily readable.
Now the date is displayed in an human readable form, using the language setting as locale if available. If not, it uses the default locale.
Cqoicebordel 10 years ago
parent
commit
7937218be6
1 changed files with 15 additions and 0 deletions
  1. 15
    0
      searx/engines/wikidata.py

+ 15
- 0
searx/engines/wikidata.py View File

@@ -1,6 +1,9 @@
1 1
 import json
2 2
 from requests import get
3 3
 from urllib import urlencode
4
+import locale
5
+import time
6
+import dateutil.parser
4 7
 
5 8
 result_count = 1
6 9
 wikidata_host = 'https://www.wikidata.org'
@@ -35,6 +38,16 @@ def response(resp):
35 38
     language = resp.search_params['language'].split('_')[0]
36 39
     if language == 'all':
37 40
         language = 'en'
41
+    
42
+    try:
43
+        locale.setlocale(locale.LC_ALL, str(resp.search_params['language']))
44
+    except:
45
+        try:
46
+            locale.setlocale(locale.LC_ALL, 'en_US')
47
+        except:
48
+            pass
49
+        pass
50
+    
38 51
     url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
39 52
                                             'languages': language + '|en'}))
40 53
 
@@ -164,10 +177,12 @@ def getDetail(jsonresponse, wikidata_id, language):
164 177
 
165 178
     date_of_birth = get_time(claims, 'P569', None)
166 179
     if date_of_birth is not None:
180
+        date_of_birth = dateutil.parser.parse(date_of_birth[8:]).strftime(locale.nl_langinfo(locale.D_FMT))
167 181
         attributes.append({'label': 'Date of birth', 'value': date_of_birth})
168 182
 
169 183
     date_of_death = get_time(claims, 'P570', None)
170 184
     if date_of_death is not None:
185
+        date_of_death = dateutil.parser.parse(date_of_death[8:]).strftime(locale.nl_langinfo(locale.D_FMT))
171 186
         attributes.append({'label': 'Date of death', 'value': date_of_death})
172 187
 
173 188
     if len(attributes) == 0 and len(urls) == 2 and len(description) == 0: