on master help sign in
[enh] add routing directions to osm search - closes #254
2c6531bAdam Tauber
searx/engines/openstreetmap.py+14
@@ -10,7 +10,9 @@
10 @parse url, title
10 @parse url, title
11"""
11"""
12
12
13+import re
13from json import loads
14from json import loads
15+from flask_babel import gettext
14
16
15# engine dependent config
17# engine dependent config
16categories = ['map']
18categories = ['map']
@@ -21,10 +23,15 @@ base_url = 'https://nominatim.openstreetmap.org/'
21search_string = 'search/{query}?format=json&polygon_geojson=1&addressdetails=1'
23search_string = 'search/{query}?format=json&polygon_geojson=1&addressdetails=1'
22result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
24result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
23
25
26+route_url = 'https://graphhopper.com/maps/?point={}&point={}&locale=en-US&vehicle=car&weighting=fastest&turn_costs=true&use_miles=false&layer=Omniscale'
27+route_re = re.compile('(?:from )?(.+) to (.+)')
28+
24
29
25# do search-request
30# do search-request
26def request(query, params):
31def request(query, params):
32+
27 params['url'] = base_url + search_string.format(query=query.decode('utf-8'))
33 params['url'] = base_url + search_string.format(query=query.decode('utf-8'))
34+ params['route'] = route_re.match(query.decode('utf-8'))
28
35
29 return params
36 return params
30
37
@@ -34,6 +41,13 @@ def response(resp):
34 results = []
41 results = []
35 json = loads(resp.text)
42 json = loads(resp.text)
36
43
44+
45+ if resp.search_params['route']:
46+ results.append({
47+ 'answer': gettext('Get directions'),
48+ 'url': route_url.format(*resp.search_params['route'].groups()),
49+ })
50+
37 # parse results
51 # parse results
38 for r in json:
52 for r in json:
39 if 'display_name' not in r:
53 if 'display_name' not in r:
searx/plugins/self_info.py+2−4
@@ -37,10 +37,8 @@ def post_search(request, search):
37 ip = x_forwarded_for[0]
37 ip = x_forwarded_for[0]
38 else:
38 else:
39 ip = request.remote_addr
39 ip = request.remote_addr
40− search.result_container.answers.clear()
40+ search.result_container.answers['ip'] = {'answer': ip}
41− search.result_container.answers.add(ip)
42 elif p.match(search.search_query.query):
41 elif p.match(search.search_query.query):
43 ua = request.user_agent
42 ua = request.user_agent
44− search.result_container.answers.clear()
43+ search.result_container.answers['user-agent'] = {'answer': ua}
45− search.result_container.answers.add(ua)
46 return True
44 return True
searx/results.py+2−2
@@ -131,7 +131,7 @@ class ResultContainer(object):
131 self._merged_results = []
131 self._merged_results = []
132 self.infoboxes = []
132 self.infoboxes = []
133 self.suggestions = set()
133 self.suggestions = set()
134− self.answers = set()
134+ self.answers = {}
135 self.corrections = set()
135 self.corrections = set()
136 self._number_of_results = []
136 self._number_of_results = []
137 self._ordered = False
137 self._ordered = False
@@ -146,7 +146,7 @@ class ResultContainer(object):
146 self.suggestions.add(result['suggestion'])
146 self.suggestions.add(result['suggestion'])
147 results.remove(result)
147 results.remove(result)
148 elif 'answer' in result:
148 elif 'answer' in result:
149− self.answers.add(result['answer'])
149+ self.answers[result['answer']] = result
150 results.remove(result)
150 results.remove(result)
151 elif 'correction' in result:
151 elif 'correction' in result:
152 self.corrections.add(result['correction'])
152 self.corrections.add(result['correction'])
searx/templates/legacy/results.html+6−2
@@ -33,8 +33,12 @@
33
33
34 {% if answers %}
34 {% if answers %}
35 <div id="answers"><span>{{ _('Answers') }}</span>
35 <div id="answers"><span>{{ _('Answers') }}</span>
36− {% for answer in answers %}
36+ {% for answer in answers.values() %}
37+ {% if answer.url %}
38+ <a href="{{ answer.url }}">{{ answer.answer }}</a>
39+ {% else %}
37− <span>{{ answer }}</span>
40+ <span>{{ answer.answer }}</span>
41+ {% endif %}
38 {% endfor %}
42 {% endfor %}
39 </div>
43 </div>
40 {% endif %}
44 {% endif %}
searx/templates/oscar/results.html+6−2
@@ -94,9 +94,13 @@
94 {%- endif %}
94 {%- endif %}
95
95
96 {% if answers -%}
96 {% if answers -%}
97− {%- for answer in answers %}
97+ {%- for answer in answers.values() %}
98 <div class="result well">
98 <div class="result well">
99+ {% if answer.url %}
100+ <a href="{{ answer.url }}">{{ answer.answer }}</a>
101+ {% else %}
99− <span>{{ answer }}</span>
102+ <span>{{ answer.answer }}</span>
103+ {% endif %}
100 </div>
104 </div>
101 {%- endfor %}
105 {%- endfor %}
102 {%- endif %}
106 {%- endif %}
searx/templates/simple/results.html+8−2
@@ -15,8 +15,14 @@
15<div id="results" class="{{ only_template }}">
15<div id="results" class="{{ only_template }}">
16 {% if answers -%}
16 {% if answers -%}
17 <div id="answers"><h4 class="title">{{ _('Answers') }} : </h4>
17 <div id="answers"><h4 class="title">{{ _('Answers') }} : </h4>
18− {%- for answer in answers -%}
18+ {%- for answer in answers.values() -%}
19− <div class="answer">{{- answer -}}</div>
19+ <div class="answer">
20+ {% if answer.url %}
21+ <a href="{{ answer.url }}">{{ answer.answer }}</a>
22+ {% else %}
23+ <span>{{ answer.answer }}</span>
24+ {% endif %}
25+ </div>
20 {%- endfor -%}
26 {%- endfor -%}
21 </div>
27 </div>
22 {%- endif %}
28 {%- endif %}