| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2015-09-07 22:39:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from searx import logger | 
					
						
							| 
									
										
										
										
											2015-01-21 11:33:16 +01:00
										 |  |  | from searx.poolrequests import get | 
					
						
							| 
									
										
										
										
											2015-01-11 13:26:42 +01:00
										 |  |  | from searx.utils import format_date_by_locale | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 22:39:33 +02:00
										 |  |  | from datetime import datetime | 
					
						
							|  |  |  | from dateutil.parser import parse as dateutil_parse | 
					
						
							|  |  |  | from urllib import urlencode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | logger = logger.getChild('wikidata') | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  | result_count = 1 | 
					
						
							|  |  |  | wikidata_host = 'https://www.wikidata.org' | 
					
						
							|  |  |  | wikidata_api = wikidata_host + '/w/api.php' | 
					
						
							|  |  |  | url_search = wikidata_api \ | 
					
						
							|  |  |  |     + '?action=query&list=search&format=json'\ | 
					
						
							|  |  |  |     + '&srnamespace=0&srprop=sectiontitle&{query}' | 
					
						
							|  |  |  | url_detail = wikidata_api\ | 
					
						
							|  |  |  |     + '?action=wbgetentities&format=json'\ | 
					
						
							|  |  |  |     + '&props=labels%7Cinfo%7Csitelinks'\ | 
					
						
							|  |  |  |     + '%7Csitelinks%2Furls%7Cdescriptions%7Cclaims'\ | 
					
						
							|  |  |  |     + '&{query}' | 
					
						
							|  |  |  | url_map = 'https://www.openstreetmap.org/'\ | 
					
						
							|  |  |  |     + '?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     params['url'] = url_search.format( | 
					
						
							|  |  |  |         query=urlencode({'srsearch': query, | 
					
						
							|  |  |  |                         'srlimit': result_count})) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  |     search_res = json.loads(resp.text) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     wikidata_ids = set() | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     for r in search_res.get('query', {}).get('search', {}): | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  |         wikidata_ids.add(r.get('title', '')) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  |     language = resp.search_params['language'].split('_')[0] | 
					
						
							|  |  |  |     if language == 'all': | 
					
						
							|  |  |  |         language = 'en' | 
					
						
							| 
									
										
										
										
											2015-01-02 12:33:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     url = url_detail.format(query=urlencode({'ids': '|'.join(wikidata_ids), | 
					
						
							|  |  |  |                                             'languages': language + '|en'})) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     htmlresponse = get(url) | 
					
						
							|  |  |  |     jsonresponse = json.loads(htmlresponse.content) | 
					
						
							|  |  |  |     for wikidata_id in wikidata_ids: | 
					
						
							| 
									
										
										
										
											2015-01-11 13:26:42 +01:00
										 |  |  |         results = results + getDetail(jsonresponse, wikidata_id, language, resp.search_params['language']) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  |     return results | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-11 13:26:42 +01:00
										 |  |  | def getDetail(jsonresponse, wikidata_id, language, locale): | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |     results = [] | 
					
						
							|  |  |  |     urls = [] | 
					
						
							|  |  |  |     attributes = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  |     result = jsonresponse.get('entities', {}).get(wikidata_id, {}) | 
					
						
							| 
									
										
										
										
											2014-10-04 22:53:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     title = result.get('labels', {}).get(language, {}).get('value', None) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if title is None: | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |         title = result.get('labels', {}).get('en', {}).get('value', None) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if title is None: | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |         return results | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     description = result\ | 
					
						
							|  |  |  |         .get('descriptions', {})\ | 
					
						
							|  |  |  |         .get(language, {})\ | 
					
						
							|  |  |  |         .get('value', None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if description is None: | 
					
						
							|  |  |  |         description = result\ | 
					
						
							|  |  |  |             .get('descriptions', {})\ | 
					
						
							|  |  |  |             .get('en', {})\ | 
					
						
							|  |  |  |             .get('value', '') | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     claims = result.get('claims', {}) | 
					
						
							|  |  |  |     official_website = get_string(claims, 'P856', None) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if official_website is not None: | 
					
						
							|  |  |  |         urls.append({'title': 'Official site', 'url': official_website}) | 
					
						
							|  |  |  |         results.append({'title': title, 'url': official_website}) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |     wikipedia_link_count = 0 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     if language != 'en': | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |         wikipedia_link_count += add_url(urls, | 
					
						
							|  |  |  |                                         'Wikipedia (' + language + ')', | 
					
						
							|  |  |  |                                         get_wikilink(result, language + | 
					
						
							|  |  |  |                                                      'wiki')) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     wikipedia_en_link = get_wikilink(result, 'enwiki') | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     wikipedia_link_count += add_url(urls, | 
					
						
							|  |  |  |                                     'Wikipedia (en)', | 
					
						
							|  |  |  |                                     wikipedia_en_link) | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |     if wikipedia_link_count == 0: | 
					
						
							|  |  |  |         misc_language = get_wiki_firstlanguage(result, 'wiki') | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |         if misc_language is not None: | 
					
						
							|  |  |  |             add_url(urls, | 
					
						
							|  |  |  |                     'Wikipedia (' + misc_language + ')', | 
					
						
							|  |  |  |                     get_wikilink(result, misc_language + 'wiki')) | 
					
						
							| 
									
										
										
										
											2014-10-12 14:33:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     if language != 'en': | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |         add_url(urls, | 
					
						
							|  |  |  |                 'Wiki voyage (' + language + ')', | 
					
						
							|  |  |  |                 get_wikilink(result, language + 'wikivoyage')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     add_url(urls, | 
					
						
							|  |  |  |             'Wiki voyage (en)', | 
					
						
							|  |  |  |             get_wikilink(result, 'enwikivoyage')) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if language != 'en': | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |         add_url(urls, | 
					
						
							|  |  |  |                 'Wikiquote (' + language + ')', | 
					
						
							|  |  |  |                 get_wikilink(result, language + 'wikiquote')) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     add_url(urls, | 
					
						
							|  |  |  |             'Wikiquote (en)', | 
					
						
							|  |  |  |             get_wikilink(result, 'enwikiquote')) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     add_url(urls, | 
					
						
							|  |  |  |             'Commons wiki', | 
					
						
							|  |  |  |             get_wikilink(result, 'commonswiki')) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     add_url(urls, | 
					
						
							|  |  |  |             'Location', | 
					
						
							|  |  |  |             get_geolink(claims, 'P625', None)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     add_url(urls, | 
					
						
							|  |  |  |             'Wikidata', | 
					
						
							|  |  |  |             'https://www.wikidata.org/wiki/' | 
					
						
							|  |  |  |             + wikidata_id + '?uselang=' + language) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-02 23:36:18 +02:00
										 |  |  |     musicbrainz_work_id = get_string(claims, 'P435') | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if musicbrainz_work_id is not None: | 
					
						
							|  |  |  |         add_url(urls, | 
					
						
							|  |  |  |                 'MusicBrainz', | 
					
						
							|  |  |  |                 'http://musicbrainz.org/work/' | 
					
						
							|  |  |  |                 + musicbrainz_work_id) | 
					
						
							| 
									
										
										
										
											2014-10-02 23:36:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     musicbrainz_artist_id = get_string(claims, 'P434') | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if musicbrainz_artist_id is not None: | 
					
						
							|  |  |  |         add_url(urls, | 
					
						
							|  |  |  |                 'MusicBrainz', | 
					
						
							|  |  |  |                 'http://musicbrainz.org/artist/' | 
					
						
							|  |  |  |                 + musicbrainz_artist_id) | 
					
						
							| 
									
										
										
										
											2014-10-02 23:36:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     musicbrainz_release_group_id = get_string(claims, 'P436') | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if musicbrainz_release_group_id is not None: | 
					
						
							|  |  |  |         add_url(urls, | 
					
						
							|  |  |  |                 'MusicBrainz', | 
					
						
							|  |  |  |                 'http://musicbrainz.org/release-group/' | 
					
						
							|  |  |  |                 + musicbrainz_release_group_id) | 
					
						
							| 
									
										
										
										
											2014-10-04 22:53:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-02 23:36:18 +02:00
										 |  |  |     musicbrainz_label_id = get_string(claims, 'P966') | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if musicbrainz_label_id is not None: | 
					
						
							|  |  |  |         add_url(urls, | 
					
						
							|  |  |  |                 'MusicBrainz', | 
					
						
							|  |  |  |                 'http://musicbrainz.org/label/' | 
					
						
							|  |  |  |                 + musicbrainz_label_id) | 
					
						
							| 
									
										
										
										
											2014-10-02 23:36:18 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # musicbrainz_area_id = get_string(claims, 'P982') | 
					
						
							|  |  |  |     # P1407 MusicBrainz series ID | 
					
						
							|  |  |  |     # P1004 MusicBrainz place ID | 
					
						
							|  |  |  |     # P1330 MusicBrainz instrument ID | 
					
						
							|  |  |  |     # P1407 MusicBrainz series ID | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     postal_code = get_string(claims, 'P281', None) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if postal_code is not None: | 
					
						
							|  |  |  |         attributes.append({'label': 'Postal code(s)', 'value': postal_code}) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 22:39:33 +02:00
										 |  |  |     date_of_birth = get_time(claims, 'P569', locale, None) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if date_of_birth is not None: | 
					
						
							|  |  |  |         attributes.append({'label': 'Date of birth', 'value': date_of_birth}) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 22:39:33 +02:00
										 |  |  |     date_of_death = get_time(claims, 'P570', locale, None) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if date_of_death is not None: | 
					
						
							|  |  |  |         attributes.append({'label': 'Date of death', 'value': date_of_death}) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if len(attributes) == 0 and len(urls) == 2 and len(description) == 0: | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |         results.append({ | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |                        'url': urls[0]['url'], | 
					
						
							|  |  |  |                        'title': title, | 
					
						
							|  |  |  |                        'content': description | 
					
						
							|  |  |  |                        }) | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |     else: | 
					
						
							|  |  |  |         results.append({ | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |                        'infobox': title, | 
					
						
							|  |  |  |                        'id': wikipedia_en_link, | 
					
						
							|  |  |  |                        'content': description, | 
					
						
							|  |  |  |                        'attributes': attributes, | 
					
						
							|  |  |  |                        'urls': urls | 
					
						
							|  |  |  |                        }) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return results | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | def add_url(urls, title, url): | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if url is not None: | 
					
						
							|  |  |  |         urls.append({'title': title, 'url': url}) | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |         return 1 | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return 0 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | def get_mainsnak(claims, propertyName): | 
					
						
							|  |  |  |     propValue = claims.get(propertyName, {}) | 
					
						
							|  |  |  |     if len(propValue) == 0: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     propValue = propValue[0].get('mainsnak', None) | 
					
						
							|  |  |  |     return propValue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | def get_string(claims, propertyName, defaultValue=None): | 
					
						
							|  |  |  |     propValue = claims.get(propertyName, {}) | 
					
						
							|  |  |  |     if len(propValue) == 0: | 
					
						
							|  |  |  |         return defaultValue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result = [] | 
					
						
							|  |  |  |     for e in propValue: | 
					
						
							|  |  |  |         mainsnak = e.get('mainsnak', {}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         datavalue = mainsnak.get('datavalue', {}) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |         if datavalue is not None: | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |             result.append(datavalue.get('value', '')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if len(result) == 0: | 
					
						
							|  |  |  |         return defaultValue | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2015-01-11 13:26:42 +01:00
										 |  |  |         # TODO handle multiple urls | 
					
						
							| 
									
										
										
										
											2014-10-12 14:33:03 +02:00
										 |  |  |         return result[0] | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-07 22:39:33 +02:00
										 |  |  | def get_time(claims, propertyName, locale, defaultValue=None): | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     propValue = claims.get(propertyName, {}) | 
					
						
							|  |  |  |     if len(propValue) == 0: | 
					
						
							|  |  |  |         return defaultValue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result = [] | 
					
						
							|  |  |  |     for e in propValue: | 
					
						
							|  |  |  |         mainsnak = e.get('mainsnak', {}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         datavalue = mainsnak.get('datavalue', {}) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |         if datavalue is not None: | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |             value = datavalue.get('value', '') | 
					
						
							|  |  |  |             result.append(value.get('time', '')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if len(result) == 0: | 
					
						
							| 
									
										
										
										
											2015-09-07 22:39:33 +02:00
										 |  |  |         date_string = defaultValue | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2015-09-07 22:39:33 +02:00
										 |  |  |         date_string = ', '.join(result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         parsed_date = datetime.strptime(date_string, "+%Y-%m-%dT%H:%M:%SZ") | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         if date_string.startswith('-'): | 
					
						
							|  |  |  |             return date_string.split('T')[0] | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             parsed_date = dateutil_parse(date_string, fuzzy=False, default=False) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             logger.debug('could not parse date %s', date_string) | 
					
						
							|  |  |  |             return date_string.split('T')[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return format_date_by_locale(parsed_date, locale) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | def get_geolink(claims, propertyName, defaultValue=''): | 
					
						
							|  |  |  |     mainsnak = get_mainsnak(claims, propertyName) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if mainsnak is None: | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |         return defaultValue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     datatype = mainsnak.get('datatype', '') | 
					
						
							|  |  |  |     datavalue = mainsnak.get('datavalue', {}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if datatype != 'globe-coordinate': | 
					
						
							|  |  |  |         return defaultValue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     value = datavalue.get('value', {}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     precision = value.get('precision', 0.0002) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-04 22:53:54 +02:00
										 |  |  |     # there is no zoom information, deduce from precision (error prone) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     # samples : | 
					
						
							|  |  |  |     # 13 --> 5 | 
					
						
							|  |  |  |     # 1 --> 6 | 
					
						
							|  |  |  |     # 0.016666666666667 --> 9 | 
					
						
							|  |  |  |     # 0.00027777777777778 --> 19 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     # wolframalpha : | 
					
						
							|  |  |  |     # quadratic fit { {13, 5}, {1, 6}, {0.0166666, 9}, {0.0002777777,19}} | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |     # 14.1186-8.8322 x+0.625447 x^2 | 
					
						
							|  |  |  |     if precision < 0.0003: | 
					
						
							|  |  |  |         zoom = 19 | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         zoom = int(15 - precision*8.8322 + precision*precision*0.625447) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     url = url_map\ | 
					
						
							|  |  |  |         .replace('{latitude}', str(value.get('latitude', 0)))\ | 
					
						
							|  |  |  |         .replace('{longitude}', str(value.get('longitude', 0)))\ | 
					
						
							|  |  |  |         .replace('{zoom}', str(zoom)) | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return url | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:53:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  | def get_wikilink(result, wikiid): | 
					
						
							|  |  |  |     url = result.get('sitelinks', {}).get(wikiid, {}).get('url', None) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |     if url is None: | 
					
						
							| 
									
										
										
										
											2014-09-28 16:51:41 +02:00
										 |  |  |         return url | 
					
						
							|  |  |  |     elif url.startswith('http://'): | 
					
						
							|  |  |  |         url = url.replace('http://', 'https://') | 
					
						
							|  |  |  |     elif url.startswith('//'): | 
					
						
							|  |  |  |         url = 'https:' + url | 
					
						
							|  |  |  |     return url | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  | def get_wiki_firstlanguage(result, wikipatternid): | 
					
						
							|  |  |  |     for k in result.get('sitelinks', {}).keys(): | 
					
						
							| 
									
										
										
										
											2014-12-07 16:36:20 +01:00
										 |  |  |         if k.endswith(wikipatternid) and len(k) == (2+len(wikipatternid)): | 
					
						
							| 
									
										
										
										
											2014-10-11 15:49:50 +02:00
										 |  |  |             return k[0:2] | 
					
						
							|  |  |  |     return None |