on master help sign in
[enh] add routing directions to osm search - closes #254
commit2c6531b2330a5c8ca986379dbad8812a6c6d3e49 authorAdam Tauber <asciimoo@gmail.com> dateTue 9 Jun 2020, 17:01 (6 years ago) parent785f093
6 files changed+38−12
searx/engines/openstreetmap.py+14 searx/plugins/self_info.py+2−4 searx/results.py+2−2 searx/templates/legacy/results.html+6−2 searx/templates/oscar/results.html+6−2 searx/templates/simple/results.html+8−2
searx/engines/openstreetmap.py+14
@@ -10,7 +10,9 @@
1010 @parse url, title
1111"""
1212
13+import re
1314from json import loads
15+from flask_babel import gettext
1416
1517# engine dependent config
1618categories = ['map']
@@ -21,10 +23,15 @@ base_url = 'https://nominatim.openstreetmap.org/'
2123search_string = 'search/{query}?format=json&polygon_geojson=1&addressdetails=1'
2224result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
2325
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+
2429
2530# do search-request
2631def request(query, params):
32+
2733 params['url'] = base_url + search_string.format(query=query.decode('utf-8'))
34+ params['route'] = route_re.match(query.decode('utf-8'))
2835
2936 return params
3037
@@ -34,6 +41,13 @@ def response(resp):
3441 results = []
3542 json = loads(resp.text)
3643
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+
3751 # parse results
3852 for r in json:
3953 if 'display_name' not in r:
searx/plugins/self_info.py+2−4
@@ -37,10 +37,8 @@ def post_search(request, search):
3737 ip = x_forwarded_for[0]
3838 else:
3939 ip = request.remote_addr
40− search.result_container.answers.clear()
41− search.result_container.answers.add(ip)
40+ search.result_container.answers['ip'] = {'answer': ip}
4241 elif p.match(search.search_query.query):
4342 ua = request.user_agent
44− search.result_container.answers.clear()
45− search.result_container.answers.add(ua)
43+ search.result_container.answers['user-agent'] = {'answer': ua}
4644 return True
searx/results.py+2−2
@@ -131,7 +131,7 @@ class ResultContainer(object):
131131 self._merged_results = []
132132 self.infoboxes = []
133133 self.suggestions = set()
134− self.answers = set()
134+ self.answers = {}
135135 self.corrections = set()
136136 self._number_of_results = []
137137 self._ordered = False
@@ -146,7 +146,7 @@ class ResultContainer(object):
146146 self.suggestions.add(result['suggestion'])
147147 results.remove(result)
148148 elif 'answer' in result:
149− self.answers.add(result['answer'])
149+ self.answers[result['answer']] = result
150150 results.remove(result)
151151 elif 'correction' in result:
152152 self.corrections.add(result['correction'])
searx/templates/legacy/results.html+6−2
@@ -33,8 +33,12 @@
3333
3434 {% if answers %}
3535 <div id="answers"><span>{{ _('Answers') }}</span>
36− {% for answer in answers %}
37− <span>{{ answer }}</span>
36+ {% for answer in answers.values() %}
37+ {% if answer.url %}
38+ <a href="{{ answer.url }}">{{ answer.answer }}</a>
39+ {% else %}
40+ <span>{{ answer.answer }}</span>
41+ {% endif %}
3842 {% endfor %}
3943 </div>
4044 {% endif %}
searx/templates/oscar/results.html+6−2
@@ -94,9 +94,13 @@
9494 {%- endif %}
9595
9696 {% if answers -%}
97− {%- for answer in answers %}
97+ {%- for answer in answers.values() %}
9898 <div class="result well">
99− <span>{{ answer }}</span>
99+ {% if answer.url %}
100+ <a href="{{ answer.url }}">{{ answer.answer }}</a>
101+ {% else %}
102+ <span>{{ answer.answer }}</span>
103+ {% endif %}
100104 </div>
101105 {%- endfor %}
102106 {%- endif %}
searx/templates/simple/results.html+8−2
@@ -15,8 +15,14 @@
1515<div id="results" class="{{ only_template }}">
1616 {% if answers -%}
1717 <div id="answers"><h4 class="title">{{ _('Answers') }} : </h4>
18− {%- for answer in answers -%}
19− <div class="answer">{{- answer -}}</div>
18+ {%- for answer in answers.values() -%}
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>
2026 {%- endfor -%}
2127 </div>
2228 {%- endif %}