on master help sign in
Merge pull request #178 from Cqoicebordel/thumbnails

Thumbnails

commite3edded60f96aea7ce25465fd0dc49914d4fe9c8 authorAdam Tauber <asciimoo@gmail.com> dateSat 17 Jan 2015, 20:25 (12 years ago) parent975b6b5d910744
9 files changed+43−8
searx/engines/500px.py+7−1 searx/engines/bing_images.py+4 searx/engines/deviantart.py+7−2 searx/engines/flickr-noapi.py+9 searx/engines/flickr.py+10−1 searx/engines/google_images.py+2 searx/templates/courgette/result_templates/images.html+1−1 searx/templates/default/result_templates/images.html+1−1 searx/templates/oscar/result_templates/images.html+2−2
searx/engines/500px.py+7−1
@@ -14,6 +14,7 @@
1414from urllib import urlencode
1515from urlparse import urljoin
1616from lxml import html
17+import re
1718
1819# engine dependent config
1920categories = ['images']
@@ -37,20 +38,25 @@ def response(resp):
3738 results = []
3839
3940 dom = html.fromstring(resp.text)
41+ regex = re.compile('3\.jpg.*$')
4042
4143 # parse results
4244 for result in dom.xpath('//div[@class="photo"]'):
4345 link = result.xpath('.//a')[0]
4446 url = urljoin(base_url, link.attrib.get('href'))
4547 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)
4751 content = result.xpath('.//div[@class="info"]//text()')[0]
52+ img_src = regex.sub('2048.jpg', thumbnail_src)
4853
4954 # append result
5055 results.append({'url': url,
5156 'title': title,
5257 'img_src': img_src,
5358 'content': content,
59+ 'thumbnail_src': thumbnail_src,
5460 'template': 'images.html'})
5561
5662 # return results
searx/engines/bing_images.py+4
@@ -25,6 +25,7 @@ paging = True
2525# search-url
2626base_url = 'https://www.bing.com/'
2727search_string = 'images/search?{query}&count=10&first={offset}'
28+thumb_url = "http://ts1.mm.bing.net/th?id={ihk}"
2829
2930
3031# do search-request
@@ -63,6 +64,8 @@ def response(resp):
6364 yaml_data = load(p.sub(r'\1\2: \3', link.attrib.get('m')))
6465
6566 title = link.attrib.get('t1')
67+ ihk = link.attrib.get('ihk')
68+
6669 #url = 'http://' + link.attrib.get('t3')
6770 url = yaml_data.get('surl')
6871 img_src = yaml_data.get('imgurl')
@@ -72,6 +75,7 @@ def response(resp):
7275 'url': url,
7376 'title': title,
7477 'content': '',
78+ 'thumbnail_src': thumb_url.format(ihk=ihk),
7579 'img_src': img_src})
7680
7781 # TODO stop parsing if 10 images are found
searx/engines/deviantart.py+7−2
@@ -6,13 +6,14 @@
66# @using-api no (TODO, rewrite to api)
77# @results HTML
88# @stable no (HTML can change)
9−# @parse url, title, thumbnail, img_src
9+# @parse url, title, thumbnail_src, img_src
1010#
1111# @todo rewrite to api
1212
1313from urllib import urlencode
1414from urlparse import urljoin
1515from lxml import html
16+import re
1617
1718# engine dependent config
1819categories = ['images']
@@ -43,18 +44,22 @@ def response(resp):
4344
4445 dom = html.fromstring(resp.text)
4546
47+ regex = re.compile('\/200H\/')
48+
4649 # parse results
4750 for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
4851 link = result.xpath('.//a[contains(@class, "thumb")]')[0]
4952 url = urljoin(base_url, link.attrib.get('href'))
5053 title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
5154 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)
5357
5458 # append result
5559 results.append({'url': url,
5660 'title': title,
5761 'img_src': img_src,
62+ 'thumbnail_src': thumbnail_src,
5863 'template': 'images.html'})
5964
6065 # return results
searx/engines/flickr-noapi.py+9
@@ -71,6 +71,14 @@ def response(resp):
7171 if 'id' not in photo['owner']:
7272 continue
7373
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+
7482 url = build_flickr_url(photo['owner']['id'], photo['id'])
7583
7684 title = photo.get('title', '')
@@ -89,6 +97,7 @@ def response(resp):
8997 results.append({'url': url,
9098 'title': title,
9199 'img_src': img_src,
100+ 'thumbnail_src': thumbnail_src,
92101 'content': content,
93102 'template': 'images.html'})
94103
searx/engines/flickr.py+10−1
@@ -23,7 +23,7 @@ api_key = None
2323
2424url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\
2525 '&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' +\
2727 '&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
2828photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
2929
@@ -65,6 +65,14 @@ def response(resp):
6565 else:
6666 continue
6767
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+
6876 url = build_flickr_url(photo['owner'], photo['id'])
6977
7078 title = photo['title']
@@ -80,6 +88,7 @@ def response(resp):
8088 results.append({'url': url,
8189 'title': title,
8290 'img_src': img_src,
91+ 'thumbnail_src': thumbnail_src,
8392 'content': content,
8493 'template': 'images.html'})
8594
searx/engines/google_images.py+2
@@ -47,11 +47,13 @@ def response(resp):
4747 title = result['title']
4848 if not result['url']:
4949 continue
50+ thumbnail_src = result['tbUrl']
5051
5152 # append result
5253 results.append({'url': href,
5354 'title': title,
5455 'content': '',
56+ 'thumbnail_src': thumbnail_src,
5557 'img_src': unquote(result['url']),
5658 'template': 'images.html'})
5759
searx/templates/courgette/result_templates/images.html+1−1
@@ -1,6 +1,6 @@
11<div class="image_result">
22 <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="{% if result.thumbnail_src %}{{ image_proxify(result.thumbnail_src) }}{% else %}{{ image_proxify(result.img_src) }}{% endif %}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}"/></a>
44 <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
55 </p>
66</div>
searx/templates/default/result_templates/images.html+1−1
@@ -1,6 +1,6 @@
11<div class="image_result">
22 <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="{% if result.thumbnail_src %}{{ image_proxify(result.thumbnail_src) }}{% else %}{{ image_proxify(result.img_src) }}{% endif %}" title="{{ result.title|striptags }}" alt="{{ result.title|striptags }}" /></a>
44 <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
55 </p>
66</div>
searx/templates/oscar/result_templates/images.html+2−2
@@ -1,7 +1,7 @@
11{% from 'oscar/macros.html' import draw_favicon %}
22
33<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="{% if result.thumbnail_src %}{{ image_proxify(result.thumbnail_src) }}{% else %}{{ image_proxify(result.img_src) }}{% endif %}" alt="{{ result.title|striptags }}" title="{{ result.title|striptags }}" class="img-thumbnail">
55</a>
66
77<div class="modal fade" id="modal-{{ index }}" tabindex="-1" role="dialog" aria-hidden="true">
@@ -12,7 +12,7 @@
1212 <h4 class="modal-title">{% if result.engine~".png" in favicons %}{{ draw_favicon(result.engine) }} {% endif %}{{ result.title|striptags }}</h4>
1313 </div>
1414 <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="{% if result.thumbnail_src %}{{ image_proxify(result.thumbnail_src) }}{% else %}{{ image_proxify(result.img_src) }}{% endif %}" alt="{{ result.title|striptags }}">
1616 {% if result.content %}<p class="result-content">{{ result.content|safe }}</p>{% endif %}
1717 </div>
1818 <div class="modal-footer">