photon.py 3.9 KB

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