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