浏览代码

[enh] template_oscar: show addressdata if possible

Thomas Pointhuber 10 年前
父节点
当前提交
c38917bb2a

+ 26
- 1
searx/engines/openstreetmap.py 查看文件

@@ -15,7 +15,7 @@ categories = ['map']
15 15
 paging = False
16 16
 
17 17
 # search-url
18
-url = 'https://nominatim.openstreetmap.org/search/{query}?format=json&polygon_geojson=1'
18
+url = 'https://nominatim.openstreetmap.org/search/{query}?format=json&polygon_geojson=1&addressdetails=1'
19 19
 
20 20
 result_base_url = 'https://openstreetmap.org/{osm_type}/{osm_id}'
21 21
 
@@ -47,6 +47,30 @@ def response(resp):
47 47
             geojson = {u'type':u'Point', 
48 48
                        u'coordinates':[r['lon'],r['lat']]}
49 49
 
50
+        address_raw = r.get('address')
51
+        address = {}
52
+
53
+        # get name
54
+        if r['class'] == 'amenity' or\
55
+           r['class'] == 'shop' or\
56
+           r['class'] == 'tourism' or\
57
+           r['class'] == 'leisure':
58
+            if address_raw.get('address29'):
59
+                address = {'name':address_raw.get('address29')}
60
+            else:
61
+                address = {'name':address_raw.get(r['type'])}
62
+
63
+        # add rest of adressdata, if something is already found
64
+        if address.get('name'):
65
+            address.update({'house_number':address_raw.get('house_number'),
66
+                       'road':address_raw.get('road'),
67
+                       'locality':address_raw.get('town', address_raw.get('village')),
68
+                       'postcode':address_raw.get('postcode'),
69
+                       'country':address_raw.get('country'),
70
+                       'country_code':address_raw.get('country_code')})
71
+        else:
72
+            address = None
73
+
50 74
         # append result
51 75
         results.append({'template': 'map.html',
52 76
                         'title': title,
@@ -55,6 +79,7 @@ def response(resp):
55 79
                         'latitude': r['lat'],
56 80
                         'boundingbox': r['boundingbox'],
57 81
                         'geojson': geojson,
82
+                        'address': address,
58 83
                         'url': url})
59 84
 
60 85
     # return results

+ 1
- 1
searx/static/oscar/css/oscar.min.css 查看文件

@@ -5,7 +5,7 @@ input[type=checkbox]:checked~.label_hide_if_checked{display:none}
5 5
 input[type=checkbox]:not(:checked)~.label_hide_if_not_checked{display:none}
6 6
 .result_header{margin-bottom:5px;margin-top:20px}.result_header .favicon{margin-bottom:-3px}
7 7
 .result_header a{vertical-align:bottom}.result_header a .highlight{font-weight:bold}
8
-.result-content .highlight{font-weight:bold}
8
+.result-content{margin-top:5px}.result-content .highlight{font-weight:bold}
9 9
 .result-default{clear:both}
10 10
 .result-images{float:left !important}
11 11
 .img-thumbnail{margin:5px;max-height:128px;min-height:128px}

+ 2
- 0
searx/static/oscar/less/oscar/results.less 查看文件

@@ -17,6 +17,8 @@
17 17
 }
18 18
 
19 19
 .result-content {
20
+    margin-top: 5px;
21
+
20 22
     .highlight {
21 23
         font-weight:bold;
22 24
     }

+ 23
- 1
searx/templates/oscar/result_templates/map.html 查看文件

@@ -7,7 +7,29 @@
7 7
 {% if (result.latitude and result.longitude) or result.boundingbox %}
8 8
     <small> &bull; <a class="text-info btn-collapse collapsed searx_init_map cursor-pointer" data-toggle="collapse" data-target="#result-map-{{ index }}" data-leaflet-target="osm-map-{{ index }}" data-map-lon="{{ result.longitude }}" data-map-lat="{{ result.latitude }}" {% if result.boundingbox %}data-map-boundingbox='{{ result.boundingbox|tojson|safe }}'{% endif %} {% if result.geojson %}data-map-geojson='{{ result.geojson|tojson|safe }}'{% endif %} data-btn-text-collapsed="{{ _('show map') }}" data-btn-text-not-collapsed="{{ _('hide map') }}">{{ icon('globe') }} {{ _('show map') }}</a></small>
9 9
 {% endif %}
10
-   
10
+
11
+{% if result.address %}
12
+<p class="result-content result-adress" itemscope itemtype="http://schema.org/PostalAddress">
13
+    {% if result.address.name %}
14
+        <strong itemprop="name">{{ result.address.name }}</strong><br/>
15
+    {% endif %}
16
+    {% if result.address.road %}
17
+        <span itemprop="streetAddress">
18
+            {% if result.address.house_number %}{{ result.address.house_number }}, {% endif %}
19
+            {{ result.address.road }}
20
+        </span><br/>
21
+    {% endif %}
22
+    {% if result.address.locality %}
23
+        <span itemprop="addressLocality">{{ result.address.locality }}</span>,
24
+        {% if result.address.postcode %} <span itemprop="postalCode">{{ result.address.postcode }}</span>{% endif %}
25
+        <br/>
26
+    {% endif %}
27
+    {% if result.address.country %}
28
+        <span itemprop="addressCountry">{{ result.address.country }}</span>
29
+    {% endif %}
30
+</p><div class="clearfix"></div>
31
+{% endif %}
32
+
11 33
 {% if result.content %}<p class="result-content">{{ result.content|safe }}</p>{% endif %}
12 34
 
13 35
 {% if (result.latitude and result.longitude) or result.boundingbox %}