瀏覽代碼

Add thumbnails in images results

- Modify engines to create/fetch an URL for the thumbnails
- Modify themes to show thumbnails instead of full images.

In Courgette, the result is not very beautiful. Should we change it ?
Cqoicebordel 10 年之前
父節點
當前提交
cb4a3fe598

+ 7
- 1
searx/engines/500px.py 查看文件

@@ -14,6 +14,7 @@
14 14
 from urllib import urlencode
15 15
 from urlparse import urljoin
16 16
 from lxml import html
17
+import re
17 18
 
18 19
 # engine dependent config
19 20
 categories = ['images']
@@ -37,20 +38,25 @@ def response(resp):
37 38
     results = []
38 39
 
39 40
     dom = html.fromstring(resp.text)
41
+    regex = re.compile('3\.jpg.*$')
40 42
 
41 43
     # parse results
42 44
     for result in dom.xpath('//div[@class="photo"]'):
43 45
         link = result.xpath('.//a')[0]
44 46
         url = urljoin(base_url, link.attrib.get('href'))
45 47
         title = result.xpath('.//div[@class="title"]//text()')[0]
46
-        img_src = link.xpath('.//img')[0].attrib['src']
48
+        thumbnail_src = link.xpath('.//img')[0].attrib['src']
49
+        # To have a bigger thumbnail, uncomment the next line
50
+        #thumbnail_src = regex.sub('4.jpg', thumbnail_src)
47 51
         content = result.xpath('.//div[@class="info"]//text()')[0]
52
+        img_src = regex.sub('2048.jpg', thumbnail_src)
48 53
 
49 54
         # append result
50 55
         results.append({'url': url,
51 56
                         'title': title,
52 57
                         'img_src': img_src,
53 58
                         'content': content,
59
+                        'thumbnail_src': thumbnail_src,
54 60
                         'template': 'images.html'})
55 61
 
56 62
     # return results

+ 4
- 0
searx/engines/bing_images.py 查看文件

@@ -25,6 +25,7 @@ paging = True
25 25
 # search-url
26 26
 base_url = 'https://www.bing.com/'
27 27
 search_string = 'images/search?{query}&count=10&first={offset}'
28
+thumb_url = "http://ts1.mm.bing.net/th?id={ihk}"
28 29
 
29 30
 
30 31
 # do search-request
@@ -63,6 +64,8 @@ def response(resp):
63 64
         yaml_data = load(p.sub(r'\1\2: \3', link.attrib.get('m')))
64 65
 
65 66
         title = link.attrib.get('t1')
67
+        ihk = link.attrib.get('ihk')
68
+        
66 69
         #url = 'http://' + link.attrib.get('t3')
67 70
         url = yaml_data.get('surl')
68 71
         img_src = yaml_data.get('imgurl')
@@ -72,6 +75,7 @@ def response(resp):
72 75
                         'url': url,
73 76
                         'title': title,
74 77
                         'content': '',
78
+                        'thumbnail_src': thumb_url.format(ihk=ihk),
75 79
                         'img_src': img_src})
76 80
 
77 81
         # TODO stop parsing if 10 images are found

+ 7
- 2
searx/engines/deviantart.py 查看文件

@@ -6,13 +6,14 @@
6 6
 # @using-api   no (TODO, rewrite to api)
7 7
 # @results     HTML
8 8
 # @stable      no (HTML can change)
9
-# @parse       url, title, thumbnail, img_src
9
+# @parse       url, title, thumbnail_src, img_src
10 10
 #
11 11
 # @todo        rewrite to api
12 12
 
13 13
 from urllib import urlencode
14 14
 from urlparse import urljoin
15 15
 from lxml import html
16
+import re
16 17
 
17 18
 # engine dependent config
18 19
 categories = ['images']
@@ -42,6 +43,8 @@ def response(resp):
42 43
         return []
43 44
 
44 45
     dom = html.fromstring(resp.text)
46
+    
47
+    regex = re.compile('\/200H\/')
45 48
 
46 49
     # parse results
47 50
     for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
@@ -49,12 +52,14 @@ def response(resp):
49 52
         url = urljoin(base_url, link.attrib.get('href'))
50 53
         title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]')  # noqa
51 54
         title = ''.join(title_links[0].xpath('.//text()'))
52
-        img_src = link.xpath('.//img')[0].attrib['src']
55
+        thumbnail_src = link.xpath('.//img')[0].attrib['src']
56
+        img_src = regex.sub('/', thumbnail_src)
53 57
 
54 58
         # append result
55 59
         results.append({'url': url,
56 60
                         'title': title,
57 61
                         'img_src': img_src,
62
+                        'thumbnail_src': thumbnail_src,
58 63
                         'template': 'images.html'})
59 64
 
60 65
     # return results

+ 9
- 0
searx/engines/flickr-noapi.py 查看文件

@@ -71,6 +71,14 @@ def response(resp):
71 71
         if 'id' not in photo['owner']:
72 72
             continue
73 73
 
74
+# For a bigger thumbnail, keep only the url_z, not the url_n
75
+        if 'n' in photo['sizes']:
76
+            thumbnail_src = photo['sizes']['n']['displayUrl']
77
+        elif 'z'  in photo['sizes']:
78
+            thumbnail_src = photo['sizes']['z']['displayUrl']
79
+        else:
80
+            thumbnail_src = img_src
81
+
74 82
         url = build_flickr_url(photo['owner']['id'], photo['id'])
75 83
 
76 84
         title = photo.get('title', '')
@@ -89,6 +97,7 @@ def response(resp):
89 97
         results.append({'url': url,
90 98
                         'title': title,
91 99
                         'img_src': img_src,
100
+                        'thumbnail_src': thumbnail_src,
92 101
                         'content': content,
93 102
                         'template': 'images.html'})
94 103
 

+ 10
- 1
searx/engines/flickr.py 查看文件

@@ -23,7 +23,7 @@ api_key = None
23 23
 
24 24
 url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\
25 25
       '&api_key={api_key}&{text}&sort=relevance' +\
26
-      '&extras=description%2C+owner_name%2C+url_o%2C+url_z' +\
26
+      '&extras=description%2C+owner_name%2C+url_o%2C+url_n%2C+url_z' +\
27 27
       '&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
28 28
 photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
29 29
 
@@ -65,6 +65,14 @@ def response(resp):
65 65
         else:
66 66
             continue
67 67
 
68
+# For a bigger thumbnail, keep only the url_z, not the url_n
69
+        if 'url_n' in photo:
70
+            thumbnail_src = photo['url_n']
71
+        elif 'url_z' in photo:
72
+            thumbnail_src = photo['url_z']
73
+        else:
74
+            thumbnail_src = img_src
75
+
68 76
         url = build_flickr_url(photo['owner'], photo['id'])
69 77
 
70 78
         title = photo['title']
@@ -80,6 +88,7 @@ def response(resp):
80 88
         results.append({'url': url,
81 89
                         'title': title,
82 90
                         'img_src': img_src,
91
+                        'thumbnail_src': thumbnail_src,
83 92
                         'content': content,
84 93
                         'template': 'images.html'})
85 94
 

+ 3
- 0
searx/engines/google_images.py 查看文件

@@ -43,15 +43,18 @@ def response(resp):
43 43
 
44 44
     # parse results
45 45
     for result in search_res['responseData']['results']:
46
+        print result
46 47
         href = result['originalContextUrl']
47 48
         title = result['title']
48 49
         if not result['url']:
49 50
             continue
51
+        thumbnail_src = result['tbUrl']
50 52
 
51 53
         # append result
52 54
         results.append({'url': href,
53 55
                         'title': title,
54 56
                         'content': '',
57
+                        'thumbnail_src': thumbnail_src,
55 58
                         'img_src': unquote(result['url']),
56 59
                         'template': 'images.html'})
57 60
 

+ 1
- 1
searx/templates/courgette/result_templates/images.html 查看文件

@@ -1,6 +1,6 @@
1 1
 <div class="image_result">
2 2
     <p>
3
-        <a href="{{ result.img_src }}"><img src="{{ image_proxify(result.img_src) }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}"/></a>
3
+        <a href="{{ result.img_src }}"><img src="{{ image_proxify(result.thumbnail_src) }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}"/></a>
4 4
         <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
5 5
     </p>
6 6
 </div>

+ 1
- 1
searx/templates/default/result_templates/images.html 查看文件

@@ -1,6 +1,6 @@
1 1
 <div class="image_result">
2 2
     <p>
3
-        <a href="{{ result.img_src }}"><img src="{{ image_proxify(result.img_src) }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}" /></a>
3
+        <a href="{{ result.img_src }}"><img src="{{ image_proxify(result.thumbnail_src) }}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}" /></a>
4 4
         <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
5 5
     </p>
6 6
 </div>

+ 2
- 2
searx/templates/oscar/result_templates/images.html 查看文件

@@ -1,7 +1,7 @@
1 1
 {% from 'oscar/macros.html' import draw_favicon %}
2 2
 
3 3
 <a href="{{ result.img_src }}" data-toggle="modal" data-target="#modal-{{ index }}">
4
-    <img src="{{ image_proxify(result.img_src) }}" alt="{{ result.title|striptags }}" title="{{ result.title|striptags }}" class="img-thumbnail">
4
+    <img src="{{ image_proxify(result.thumbnail_src) }}" alt="{{ result.title|striptags }}" title="{{ result.title|striptags }}" class="img-thumbnail">
5 5
 </a>
6 6
 
7 7
 <div class="modal fade" id="modal-{{ index }}" tabindex="-1" role="dialog" aria-hidden="true">
@@ -12,7 +12,7 @@
12 12
                 <h4 class="modal-title">{% if result.engine~".png" in favicons %}{{ draw_favicon(result.engine) }} {% endif %}{{ result.title|striptags }}</h4>
13 13
             </div>
14 14
             <div class="modal-body">
15
-                <img class="img-responsive center-block" src="{{ result.img_src }}" alt="{{ result.title }}">
15
+                <img class="img-responsive center-block" src="{{ result.thumbnail_src }}" alt="{{ result.title|striptags }}">
16 16
                 {% if result.content %}<p class="result-content">{{ result.content|safe }}</p>{% endif %}
17 17
             </div>
18 18
             <div class="modal-footer">