From 2c6531b2330a5c8ca986379dbad8812a6c6d3e49 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Tue, 9 Jun 2020 17:01:59 +0200 Subject: [PATCH] [enh] add routing directions to osm search - closes #254 --- searx/engines/openstreetmap.py | 14 ++++++++++++++ searx/plugins/self_info.py | 6 ++---- searx/results.py | 4 ++-- searx/templates/legacy/results.html | 8 ++++++-- searx/templates/oscar/results.html | 8 ++++++-- searx/templates/simple/results.html | 10 ++++++++-- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/searx/engines/openstreetmap.py b/searx/engines/openstreetmap.py index cec10a3c7..011523433 100644 --- a/searx/engines/openstreetmap.py +++ b/searx/engines/openstreetmap.py @@ -10,7 +10,9 @@ @parse url, title """ +import re from json import loads +from flask_babel import gettext # engine dependent config categories = ['map'] @@ -21,10 +23,15 @@ base_url = 'https://nominatim.openstreetmap.org/' search_string = 'search/{query}?format=json&polygon_geojson=1&addressdetails=1' result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}' +route_url = 'https://graphhopper.com/maps/?point={}&point={}&locale=en-US&vehicle=car&weighting=fastest&turn_costs=true&use_miles=false&layer=Omniscale' +route_re = re.compile('(?:from )?(.+) to (.+)') + # do search-request def request(query, params): + params['url'] = base_url + search_string.format(query=query.decode('utf-8')) + params['route'] = route_re.match(query.decode('utf-8')) return params @@ -34,6 +41,13 @@ def response(resp): results = [] json = loads(resp.text) + + if resp.search_params['route']: + results.append({ + 'answer': gettext('Get directions'), + 'url': route_url.format(*resp.search_params['route'].groups()), + }) + # parse results for r in json: if 'display_name' not in r: diff --git a/searx/plugins/self_info.py b/searx/plugins/self_info.py index 8d6c661ad..67e5e0e53 100644 --- a/searx/plugins/self_info.py +++ b/searx/plugins/self_info.py @@ -37,10 +37,8 @@ def post_search(request, search): ip = x_forwarded_for[0] else: ip = request.remote_addr - search.result_container.answers.clear() - search.result_container.answers.add(ip) + search.result_container.answers['ip'] = {'answer': ip} elif p.match(search.search_query.query): ua = request.user_agent - search.result_container.answers.clear() - search.result_container.answers.add(ua) + search.result_container.answers['user-agent'] = {'answer': ua} return True diff --git a/searx/results.py b/searx/results.py index 62a01a5bd..17db33aa4 100644 --- a/searx/results.py +++ b/searx/results.py @@ -131,7 +131,7 @@ class ResultContainer(object): self._merged_results = [] self.infoboxes = [] self.suggestions = set() - self.answers = set() + self.answers = {} self.corrections = set() self._number_of_results = [] self._ordered = False @@ -146,7 +146,7 @@ class ResultContainer(object): self.suggestions.add(result['suggestion']) results.remove(result) elif 'answer' in result: - self.answers.add(result['answer']) + self.answers[result['answer']] = result results.remove(result) elif 'correction' in result: self.corrections.add(result['correction']) diff --git a/searx/templates/legacy/results.html b/searx/templates/legacy/results.html index 2e28bc91f..fd95657a4 100644 --- a/searx/templates/legacy/results.html +++ b/searx/templates/legacy/results.html @@ -33,8 +33,12 @@ {% if answers %}
{{ _('Answers') }} - {% for answer in answers %} - {{ answer }} + {% for answer in answers.values() %} + {% if answer.url %} + {{ answer.answer }} + {% else %} + {{ answer.answer }} + {% endif %} {% endfor %}
{% endif %} diff --git a/searx/templates/oscar/results.html b/searx/templates/oscar/results.html index fc4efadfa..7a444d19f 100644 --- a/searx/templates/oscar/results.html +++ b/searx/templates/oscar/results.html @@ -94,9 +94,13 @@ {%- endif %} {% if answers -%} - {%- for answer in answers %} + {%- for answer in answers.values() %}
- {{ answer }} + {% if answer.url %} + {{ answer.answer }} + {% else %} + {{ answer.answer }} + {% endif %}
{%- endfor %} {%- endif %} diff --git a/searx/templates/simple/results.html b/searx/templates/simple/results.html index 8885abc30..2e393193f 100644 --- a/searx/templates/simple/results.html +++ b/searx/templates/simple/results.html @@ -15,8 +15,14 @@
{% if answers -%}

{{ _('Answers') }} :

- {%- for answer in answers -%} -
{{- answer -}}
+ {%- for answer in answers.values() -%} +
+ {% if answer.url %} + {{ answer.answer }} + {% else %} + {{ answer.answer }} + {% endif %} +
{%- endfor -%}
{%- endif %}