photon.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 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. return params
  36. # get response from search-request
  37. def response(resp):
  38. results = []
  39. json = loads(resp.text)
  40. # parse results
  41. for r in json.get('features', {}):
  42. properties = r.get('properties')
  43. if not properties:
  44. continue
  45. # get title
  46. title = properties.get('name')
  47. # get osm-type
  48. if properties.get('osm_type') == 'N':
  49. osm_type = 'node'
  50. elif properties.get('osm_type') == 'W':
  51. osm_type = 'way'
  52. elif properties.get('osm_type') == 'R':
  53. osm_type = 'relation'
  54. else:
  55. # continue if invalide osm-type
  56. continue
  57. url = result_base_url.format(osm_type=osm_type,
  58. osm_id=properties.get('osm_id'))
  59. osm = {'type': osm_type,
  60. 'id': properties.get('osm_id')}
  61. geojson = r.get('geometry')
  62. if properties.get('extent'):
  63. boundingbox = [properties.get('extent')[3],
  64. properties.get('extent')[1],
  65. properties.get('extent')[0],
  66. properties.get('extent')[2]]
  67. else:
  68. # TODO: better boundingbox calculation
  69. boundingbox = [geojson['coordinates'][1],
  70. geojson['coordinates'][1],
  71. geojson['coordinates'][0],
  72. geojson['coordinates'][0]]
  73. # address calculation
  74. address = {}
  75. # get name
  76. if properties.get('osm_key') == 'amenity' or\
  77. properties.get('osm_key') == 'shop' or\
  78. properties.get('osm_key') == 'tourism' or\
  79. properties.get('osm_key') == 'leisure':
  80. address = {'name': properties.get('name')}
  81. # add rest of adressdata, if something is already found
  82. if address.get('name'):
  83. address.update({'house_number': properties.get('housenumber'),
  84. 'road': properties.get('street'),
  85. 'locality': properties.get('city',
  86. properties.get('town', # noqa
  87. properties.get('village'))), # noqa
  88. 'postcode': properties.get('postcode'),
  89. 'country': properties.get('country')})
  90. else:
  91. address = None
  92. # append result
  93. results.append({'template': 'map.html',
  94. 'title': title,
  95. 'content': '',
  96. 'longitude': geojson['coordinates'][0],
  97. 'latitude': geojson['coordinates'][1],
  98. 'boundingbox': boundingbox,
  99. 'geojson': geojson,
  100. 'address': address,
  101. 'osm': osm,
  102. 'url': url})
  103. # return results
  104. return results