photon.py 4.0 KB

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