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