|  | @@ -2,7 +2,7 @@ import json
 | 
	
		
			
			| 2 | 2 |  from requests import get
 | 
	
		
			
			| 3 | 3 |  from urllib import urlencode
 | 
	
		
			
			| 4 | 4 |  
 | 
	
		
			
			| 5 |  | -resultCount=2
 | 
	
		
			
			|  | 5 | +resultCount=1
 | 
	
		
			
			| 6 | 6 |  urlSearch = 'https://www.wikidata.org/w/api.php?action=query&list=search&format=json&srnamespace=0&srprop=sectiontitle&{query}'
 | 
	
		
			
			| 7 | 7 |  urlDetail = 'https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&props=labels%7Cinfo%7Csitelinks%7Csitelinks%2Furls%7Cdescriptions%7Cclaims&{query}'
 | 
	
		
			
			| 8 | 8 |  urlMap = 'https://www.openstreetmap.org/?lat={latitude}&lon={longitude}&zoom={zoom}&layers=M'
 | 
	
	
		
			
			|  | @@ -33,17 +33,20 @@ def response(resp):
 | 
	
		
			
			| 33 | 33 |      return results
 | 
	
		
			
			| 34 | 34 |  
 | 
	
		
			
			| 35 | 35 |  def getDetail(jsonresponse, wikidata_id, language):
 | 
	
		
			
			|  | 36 | +    results = []
 | 
	
		
			
			|  | 37 | +    urls = []
 | 
	
		
			
			|  | 38 | +    attributes = []
 | 
	
		
			
			|  | 39 | +
 | 
	
		
			
			| 36 | 40 |      result = jsonresponse.get('entities', {}).get(wikidata_id, {})
 | 
	
		
			
			| 37 | 41 |  
 | 
	
		
			
			| 38 | 42 |      title = result.get('labels', {}).get(language, {}).get('value', None)
 | 
	
		
			
			| 39 | 43 |      if title == None:
 | 
	
		
			
			| 40 |  | -        title = result.get('labels', {}).get('en', {}).get('value', wikidata_id)
 | 
	
		
			
			| 41 |  | -    results = []
 | 
	
		
			
			| 42 |  | -    urls = []
 | 
	
		
			
			| 43 |  | -    attributes = []
 | 
	
		
			
			|  | 44 | +        title = result.get('labels', {}).get('en', {}).get('value', None)
 | 
	
		
			
			|  | 45 | +    if title == None:
 | 
	
		
			
			|  | 46 | +        return results
 | 
	
		
			
			| 44 | 47 |  
 | 
	
		
			
			| 45 |  | -    description = result.get('descriptions', {}).get(language, {}).get('value', '')
 | 
	
		
			
			| 46 |  | -    if description == '':
 | 
	
		
			
			|  | 48 | +    description = result.get('descriptions', {}).get(language, {}).get('value', None)
 | 
	
		
			
			|  | 49 | +    if description == None:
 | 
	
		
			
			| 47 | 50 |          description = result.get('descriptions', {}).get('en', {}).get('value', '')
 | 
	
		
			
			| 48 | 51 |  
 | 
	
		
			
			| 49 | 52 |      claims = result.get('claims', {})
 | 
	
	
		
			
			|  | @@ -52,11 +55,16 @@ def getDetail(jsonresponse, wikidata_id, language):
 | 
	
		
			
			| 52 | 55 |          urls.append({ 'title' : 'Official site', 'url': official_website })
 | 
	
		
			
			| 53 | 56 |          results.append({ 'title': title, 'url' : official_website })
 | 
	
		
			
			| 54 | 57 |  
 | 
	
		
			
			|  | 58 | +    wikipedia_link_count = 0
 | 
	
		
			
			| 55 | 59 |      if language != 'en':
 | 
	
		
			
			| 56 |  | -        add_url(urls, 'Wikipedia (' + language + ')', get_wikilink(result, language + 'wiki'))
 | 
	
		
			
			|  | 60 | +        wikipedia_link_count += add_url(urls, 'Wikipedia (' + language + ')', get_wikilink(result, language + 'wiki'))
 | 
	
		
			
			| 57 | 61 |      wikipedia_en_link = get_wikilink(result, 'enwiki')
 | 
	
		
			
			| 58 |  | -    add_url(urls, 'Wikipedia (en)', wikipedia_en_link)
 | 
	
		
			
			| 59 |  | -
 | 
	
		
			
			|  | 62 | +    wikipedia_link_count += add_url(urls, 'Wikipedia (en)', wikipedia_en_link)
 | 
	
		
			
			|  | 63 | +    if wikipedia_link_count == 0:
 | 
	
		
			
			|  | 64 | +        misc_language = get_wiki_firstlanguage(result, 'wiki')
 | 
	
		
			
			|  | 65 | +        if misc_language != None:
 | 
	
		
			
			|  | 66 | +            add_url(urls, 'Wikipedia (' + misc_language + ')', get_wikilink(result, misc_language + 'wiki'))
 | 
	
		
			
			|  | 67 | +        
 | 
	
		
			
			| 60 | 68 |      if language != 'en':
 | 
	
		
			
			| 61 | 69 |          add_url(urls, 'Wiki voyage (' + language + ')', get_wikilink(result, language + 'wikivoyage'))
 | 
	
		
			
			| 62 | 70 |      add_url(urls, 'Wiki voyage (en)', get_wikilink(result, 'enwikivoyage'))
 | 
	
	
		
			
			|  | @@ -105,14 +113,20 @@ def getDetail(jsonresponse, wikidata_id, language):
 | 
	
		
			
			| 105 | 113 |      if date_of_death != None:
 | 
	
		
			
			| 106 | 114 |          attributes.append({'label' : 'Date of death', 'value' : date_of_death})
 | 
	
		
			
			| 107 | 115 |  
 | 
	
		
			
			| 108 |  | -
 | 
	
		
			
			| 109 |  | -    results.append({
 | 
	
		
			
			| 110 |  | -            'infobox' : title,
 | 
	
		
			
			| 111 |  | -            'id' : wikipedia_en_link,
 | 
	
		
			
			| 112 |  | -            'content' : description,
 | 
	
		
			
			| 113 |  | -            'attributes' : attributes,
 | 
	
		
			
			| 114 |  | -            'urls' : urls
 | 
	
		
			
			| 115 |  | -            })
 | 
	
		
			
			|  | 116 | +    if len(attributes)==0 and len(urls)==2 and len(description)==0:
 | 
	
		
			
			|  | 117 | +        results.append({
 | 
	
		
			
			|  | 118 | +                'url': urls[0]['url'],
 | 
	
		
			
			|  | 119 | +                'title': title,
 | 
	
		
			
			|  | 120 | +                'content': description
 | 
	
		
			
			|  | 121 | +                })
 | 
	
		
			
			|  | 122 | +    else:
 | 
	
		
			
			|  | 123 | +        results.append({
 | 
	
		
			
			|  | 124 | +                'infobox' : title,
 | 
	
		
			
			|  | 125 | +                'id' : wikipedia_en_link,
 | 
	
		
			
			|  | 126 | +                'content' : description,
 | 
	
		
			
			|  | 127 | +                'attributes' : attributes,
 | 
	
		
			
			|  | 128 | +                'urls' : urls
 | 
	
		
			
			|  | 129 | +                })
 | 
	
		
			
			| 116 | 130 |  
 | 
	
		
			
			| 117 | 131 |      return results
 | 
	
		
			
			| 118 | 132 |  
 | 
	
	
		
			
			|  | @@ -120,7 +134,9 @@ def getDetail(jsonresponse, wikidata_id, language):
 | 
	
		
			
			| 120 | 134 |  def add_url(urls, title, url):
 | 
	
		
			
			| 121 | 135 |      if url != None:
 | 
	
		
			
			| 122 | 136 |          urls.append({'title' : title, 'url' : url})
 | 
	
		
			
			| 123 |  | -
 | 
	
		
			
			|  | 137 | +        return 1
 | 
	
		
			
			|  | 138 | +    else:
 | 
	
		
			
			|  | 139 | +        return 0
 | 
	
		
			
			| 124 | 140 |  
 | 
	
		
			
			| 125 | 141 |  def get_mainsnak(claims, propertyName):
 | 
	
		
			
			| 126 | 142 |      propValue = claims.get(propertyName, {})
 | 
	
	
		
			
			|  | @@ -213,3 +229,9 @@ def get_wikilink(result, wikiid):
 | 
	
		
			
			| 213 | 229 |      elif url.startswith('//'):
 | 
	
		
			
			| 214 | 230 |          url = 'https:' + url
 | 
	
		
			
			| 215 | 231 |      return url
 | 
	
		
			
			|  | 232 | +
 | 
	
		
			
			|  | 233 | +def get_wiki_firstlanguage(result, wikipatternid):
 | 
	
		
			
			|  | 234 | +    for k in result.get('sitelinks', {}).keys():
 | 
	
		
			
			|  | 235 | +        if k.endswith(wikipatternid) and len(k)==(2+len(wikipatternid)):
 | 
	
		
			
			|  | 236 | +            return k[0:2]
 | 
	
		
			
			|  | 237 | +    return None
 |