瀏覽代碼

[enh] add photon engine

Thomas Pointhuber 10 年之前
父節點
當前提交
5ae38bafdf
共有 3 個檔案被更改,包括 131 行新增3 行删除
  1. 6
    3
      searx/engines/openstreetmap.py
  2. 121
    0
      searx/engines/photon.py
  3. 4
    0
      searx/settings.yml

+ 6
- 3
searx/engines/openstreetmap.py 查看文件

@@ -9,20 +9,23 @@
9 9
 # @parse       url, title
10 10
 
11 11
 from json import loads
12
+from searx.utils import searx_useragent
12 13
 
13 14
 # engine dependent config
14 15
 categories = ['map']
15 16
 paging = False
16 17
 
17 18
 # search-url
18
-url = 'https://nominatim.openstreetmap.org/search/{query}?format=json&polygon_geojson=1&addressdetails=1'
19
-
19
+base_url = 'https://nominatim.openstreetmap.org/search/{query}?format=json&polygon_geojson=1&addressdetails=1'
20 20
 result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
21 21
 
22 22
 
23 23
 # do search-request
24 24
 def request(query, params):
25
-    params['url'] = url.format(query=query)
25
+    params['url'] = base_url.format(query=query)
26
+
27
+    # using searx User-Agent
28
+    params['headers']['User-Agent'] = searx_useragent()
26 29
 
27 30
     return params
28 31
 

+ 121
- 0
searx/engines/photon.py 查看文件

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

+ 4
- 0
searx/settings.yml 查看文件

@@ -95,6 +95,10 @@ engines:
95 95
     engine : openstreetmap
96 96
     shortcut : osm
97 97
 
98
+  - name : photon
99
+    engine : photon
100
+    shortcut : ph
101
+
98 102
 #  - name : piratebay
99 103
 #    engine : piratebay
100 104
 #    shortcut : tpb