photon.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Photon (Map)
  4. """
  5. from json import loads
  6. from urllib.parse import urlencode
  7. from searx.utils import searx_useragent
  8. # about
  9. about = {
  10. "website": 'https://photon.komoot.de',
  11. "wikidata_id": None,
  12. "official_api_documentation": 'https://photon.komoot.de/',
  13. "use_official_api": True,
  14. "require_api_key": False,
  15. "results": 'JSON',
  16. }
  17. # engine dependent config
  18. categories = ['map']
  19. paging = False
  20. number_of_results = 10
  21. # search-url
  22. base_url = 'https://photon.komoot.io/'
  23. search_string = 'api/?{query}&limit={limit}'
  24. result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
  25. # list of supported languages
  26. supported_languages = ['de', 'en', 'fr', 'it']
  27. # do search-request
  28. def request(query, params):
  29. params['url'] = base_url +\
  30. search_string.format(query=urlencode({'q': query}),
  31. limit=number_of_results)
  32. if params['language'] != 'all':
  33. language = params['language'].split('_')[0]
  34. if language in supported_languages:
  35. params['url'] = params['url'] + "&lang=" + language
  36. # using searx User-Agent
  37. params['headers']['User-Agent'] = searx_useragent()
  38. return params
  39. # get response from search-request
  40. def response(resp):
  41. results = []
  42. json = loads(resp.text)
  43. # parse results
  44. for r in json.get('features', {}):
  45. properties = r.get('properties')
  46. if not properties:
  47. continue
  48. # get title
  49. title = properties.get('name')
  50. # get osm-type
  51. if properties.get('osm_type') == 'N':
  52. osm_type = 'node'
  53. elif properties.get('osm_type') == 'W':
  54. osm_type = 'way'
  55. elif properties.get('osm_type') == 'R':
  56. osm_type = 'relation'
  57. else:
  58. # continue if invalide osm-type
  59. continue
  60. url = result_base_url.format(osm_type=osm_type,
  61. osm_id=properties.get('osm_id'))
  62. osm = {'type': osm_type,
  63. 'id': properties.get('osm_id')}
  64. geojson = r.get('geometry')
  65. if properties.get('extent'):
  66. boundingbox = [properties.get('extent')[3],
  67. properties.get('extent')[1],
  68. properties.get('extent')[0],
  69. properties.get('extent')[2]]
  70. else:
  71. # TODO: better boundingbox calculation
  72. boundingbox = [geojson['coordinates'][1],
  73. geojson['coordinates'][1],
  74. geojson['coordinates'][0],
  75. geojson['coordinates'][0]]
  76. # address calculation
  77. address = {}
  78. # get name
  79. if properties.get('osm_key') == 'amenity' or\
  80. properties.get('osm_key') == 'shop' or\
  81. properties.get('osm_key') == 'tourism' or\
  82. properties.get('osm_key') == 'leisure':
  83. address = {'name': properties.get('name')}
  84. # add rest of adressdata, if something is already found
  85. if address.get('name'):
  86. address.update({'house_number': properties.get('housenumber'),
  87. 'road': properties.get('street'),
  88. 'locality': properties.get('city',
  89. properties.get('town', # noqa
  90. properties.get('village'))), # noqa
  91. 'postcode': properties.get('postcode'),
  92. 'country': properties.get('country')})
  93. else:
  94. address = None
  95. # append result
  96. results.append({'template': 'map.html',
  97. 'title': title,
  98. 'content': '',
  99. 'longitude': geojson['coordinates'][0],
  100. 'latitude': geojson['coordinates'][1],
  101. 'boundingbox': boundingbox,
  102. 'geojson': geojson,
  103. 'address': address,
  104. 'osm': osm,
  105. 'url': url})
  106. # return results
  107. return results