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
 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 time
6
+import dateutil.parser
4
 
7
 
5
 result_count = 1
8
 result_count = 1
6
 wikidata_host = 'https://www.wikidata.org'
9
 wikidata_host = 'https://www.wikidata.org'
35
     language = resp.search_params['language'].split('_')[0]
38
     language = resp.search_params['language'].split('_')[0]
36
     if language == 'all':
39
     if language == 'all':
37
         language = 'en'
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
     url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
51
     url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids),
39
                                             'languages': language + '|en'}))
52
                                             'languages': language + '|en'}))
40
 
53
 
164
 
177
 
165
     date_of_birth = get_time(claims, 'P569', None)
178
     date_of_birth = get_time(claims, 'P569', None)
166
     if date_of_birth is not None:
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
         attributes.append({'label': 'Date of birth', 'value': date_of_birth})
181
         attributes.append({'label': 'Date of birth', 'value': date_of_birth})
168
 
182
 
169
     date_of_death = get_time(claims, 'P570', None)
183
     date_of_death = get_time(claims, 'P570', None)
170
     if date_of_death is not None:
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
         attributes.append({'label': 'Date of death', 'value': date_of_death})
186
         attributes.append({'label': 'Date of death', 'value': date_of_death})
172
 
187
 
173
     if len(attributes) == 0 and len(urls) == 2 and len(description) == 0:
188
     if len(attributes) == 0 and len(urls) == 2 and len(description) == 0: