logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

photon.py (4029B)


  1. """
  2. Photon (Map)
  3. @website https://photon.komoot.de
  4. @provide-api yes (https://photon.komoot.de/)
  5. @using-api yes
  6. @results JSON
  7. @stable yes
  8. @parse url, title
  9. """
  10. from json import loads
  11. from searx.utils import searx_useragent
  12. from searx.url_utils import urlencode
  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. supported_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. language = params['language'].split('-')[0]
  30. if language in supported_languages:
  31. params['url'] = params['url'] + "&lang=" + language
  32. # using searx User-Agent
  33. params['headers']['User-Agent'] = searx_useragent()
  34. return params
  35. # get response from search-request
  36. def response(resp):
  37. results = []
  38. json = loads(resp.text)
  39. # parse results
  40. for r in json.get('features', {}):
  41. properties = r.get('properties')
  42. if not properties:
  43. continue
  44. # get title
  45. title = properties.get('name')
  46. # get osm-type
  47. if properties.get('osm_type') == 'N':
  48. osm_type = 'node'
  49. elif properties.get('osm_type') == 'W':
  50. osm_type = 'way'
  51. elif properties.get('osm_type') == 'R':
  52. osm_type = 'relation'
  53. else:
  54. # continue if invalide osm-type
  55. continue
  56. url = result_base_url.format(osm_type=osm_type,
  57. osm_id=properties.get('osm_id'))
  58. osm = {'type': osm_type,
  59. 'id': properties.get('osm_id')}
  60. geojson = r.get('geometry')
  61. if properties.get('extent'):
  62. boundingbox = [properties.get('extent')[3],
  63. properties.get('extent')[1],
  64. properties.get('extent')[0],
  65. properties.get('extent')[2]]
  66. else:
  67. # TODO: better boundingbox calculation
  68. boundingbox = [geojson['coordinates'][1],
  69. geojson['coordinates'][1],
  70. geojson['coordinates'][0],
  71. geojson['coordinates'][0]]
  72. # address calculation
  73. address = {}
  74. # get name
  75. if properties.get('osm_key') == 'amenity' or\
  76. properties.get('osm_key') == 'shop' or\
  77. properties.get('osm_key') == 'tourism' or\
  78. properties.get('osm_key') == 'leisure':
  79. address = {'name': properties.get('name')}
  80. # add rest of adressdata, if something is already found
  81. if address.get('name'):
  82. address.update({'house_number': properties.get('housenumber'),
  83. 'road': properties.get('street'),
  84. 'locality': properties.get('city',
  85. properties.get('town', # noqa
  86. properties.get('village'))), # noqa
  87. 'postcode': properties.get('postcode'),
  88. 'country': properties.get('country')})
  89. else:
  90. address = None
  91. # append result
  92. results.append({'template': 'map.html',
  93. 'title': title,
  94. 'content': '',
  95. 'longitude': geojson['coordinates'][0],
  96. 'latitude': geojson['coordinates'][1],
  97. 'boundingbox': boundingbox,
  98. 'geojson': geojson,
  99. 'address': address,
  100. 'osm': osm,
  101. 'url': url})
  102. # return results
  103. return results