on master help sign in
Merge pull request #178 from Cqoicebordel/thumbnails
e3eddedAdam Tauber
searx/engines/500px.py+7−1
@@ -14,6 +14,7 @@
14from urllib import urlencode
14from urllib import urlencode
15from urlparse import urljoin
15from urlparse import urljoin
16from lxml import html
16from lxml import html
17+import re
17
18
18# engine dependent config
19# engine dependent config
19categories = ['images']
20categories = ['images']
@@ -37,20 +38,25 @@ def response(resp):
37 results = []
38 results = []
38
39
39 dom = html.fromstring(resp.text)
40 dom = html.fromstring(resp.text)
41+ regex = re.compile('3\.jpg.*$')
40
42
41 # parse results
43 # parse results
42 for result in dom.xpath('//div[@class="photo"]'):
44 for result in dom.xpath('//div[@class="photo"]'):
43 link = result.xpath('.//a')[0]
45 link = result.xpath('.//a')[0]
44 url = urljoin(base_url, link.attrib.get('href'))
46 url = urljoin(base_url, link.attrib.get('href'))
45 title = result.xpath('.//div[@class="title"]//text()')[0]
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 content = result.xpath('.//div[@class="info"]//text()')[0]
51 content = result.xpath('.//div[@class="info"]//text()')[0]
52+ img_src = regex.sub('2048.jpg', thumbnail_src)
48
53
49 # append result
54 # append result
50 results.append({'url': url,
55 results.append({'url': url,
51 'title': title,
56 'title': title,
52 'img_src': img_src,
57 'img_src': img_src,
53 'content': content,
58 'content': content,
59+ 'thumbnail_src': thumbnail_src,
54 'template': 'images.html'})
60 'template': 'images.html'})
55
61
56 # return results
62 # return results
searx/engines/bing_images.py+4
@@ -25,6 +25,7 @@ paging = True
25# search-url
25# search-url
26base_url = 'https://www.bing.com/'
26base_url = 'https://www.bing.com/'
27search_string = 'images/search?{query}&count=10&first={offset}'
27search_string = 'images/search?{query}&count=10&first={offset}'
28+thumb_url = "http://ts1.mm.bing.net/th?id={ihk}"
28
29
29
30
30# do search-request
31# do search-request
@@ -63,6 +64,8 @@ def response(resp):
63 yaml_data = load(p.sub(r'\1\2: \3', link.attrib.get('m')))
64 yaml_data = load(p.sub(r'\1\2: \3', link.attrib.get('m')))
64
65
65 title = link.attrib.get('t1')
66 title = link.attrib.get('t1')
67+ ihk = link.attrib.get('ihk')
68+
66 #url = 'http://' + link.attrib.get('t3')
69 #url = 'http://' + link.attrib.get('t3')
67 url = yaml_data.get('surl')
70 url = yaml_data.get('surl')
68 img_src = yaml_data.get('imgurl')
71 img_src = yaml_data.get('imgurl')
@@ -72,6 +75,7 @@ def response(resp):
72 'url': url,
75 'url': url,
73 'title': title,
76 'title': title,
74 'content': '',
77 'content': '',
78+ 'thumbnail_src': thumb_url.format(ihk=ihk),
75 'img_src': img_src})
79 'img_src': img_src})
76
80
77 # TODO stop parsing if 10 images are found
81 # TODO stop parsing if 10 images are found
searx/engines/deviantart.py+7−2
@@ -6,13 +6,14 @@
6# @using-api no (TODO, rewrite to api)
6# @using-api no (TODO, rewrite to api)
7# @results HTML
7# @results HTML
8# @stable no (HTML can change)
8# @stable no (HTML can change)
9−# @parse url, title, thumbnail, img_src
9+# @parse url, title, thumbnail_src, img_src
10#
10#
11# @todo rewrite to api
11# @todo rewrite to api
12
12
13from urllib import urlencode
13from urllib import urlencode
14from urlparse import urljoin
14from urlparse import urljoin
15from lxml import html
15from lxml import html
16+import re
16
17
17# engine dependent config
18# engine dependent config
18categories = ['images']
19categories = ['images']
@@ -43,18 +44,22 @@ def response(resp):
43
44
44 dom = html.fromstring(resp.text)
45 dom = html.fromstring(resp.text)
45
46
47+ regex = re.compile('\/200H\/')
48+
46 # parse results
49 # parse results
47 for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
50 for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
48 link = result.xpath('.//a[contains(@class, "thumb")]')[0]
51 link = result.xpath('.//a[contains(@class, "thumb")]')[0]
49 url = urljoin(base_url, link.attrib.get('href'))
52 url = urljoin(base_url, link.attrib.get('href'))
50 title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
53 title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
51 title = ''.join(title_links[0].xpath('.//text()'))
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 # append result
58 # append result
55 results.append({'url': url,
59 results.append({'url': url,
56 'title': title,
60 'title': title,
57 'img_src': img_src,
61 'img_src': img_src,
62+ 'thumbnail_src': thumbnail_src,
58 'template': 'images.html'})
63 'template': 'images.html'})
59
64
60 # return results
65 # return results
searx/engines/flickr-noapi.py+9
@@ -71,6 +71,14 @@ def response(resp):
71 if 'id' not in photo['owner']:
71 if 'id' not in photo['owner']:
72 continue
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 url = build_flickr_url(photo['owner']['id'], photo['id'])
82 url = build_flickr_url(photo['owner']['id'], photo['id'])
75
83
76 title = photo.get('title', '')
84 title = photo.get('title', '')
@@ -89,6 +97,7 @@ def response(resp):
89 results.append({'url': url,
97 results.append({'url': url,
90 'title': title,
98 'title': title,
91 'img_src': img_src,
99 'img_src': img_src,
100+ 'thumbnail_src': thumbnail_src,
92 'content': content,
101 'content': content,
93 'template': 'images.html'})
102 'template': 'images.html'})
94
103
searx/engines/flickr.py+10−1
@@ -23,7 +23,7 @@ api_key = None
23
23
24url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\
24url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\
25 '&api_key={api_key}&{text}&sort=relevance' +\
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 '&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
27 '&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
28photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
28photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
29
29
@@ -65,6 +65,14 @@ def response(resp):
65 else:
65 else:
66 continue
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 url = build_flickr_url(photo['owner'], photo['id'])
76 url = build_flickr_url(photo['owner'], photo['id'])
69
77
70 title = photo['title']
78 title = photo['title']
@@ -80,6 +88,7 @@ def response(resp):
80 results.append({'url': url,
88 results.append({'url': url,
81 'title': title,
89 'title': title,
82 'img_src': img_src,
90 'img_src': img_src,
91+ 'thumbnail_src': thumbnail_src,
83 'content': content,
92 'content': content,
84 'template': 'images.html'})
93 'template': 'images.html'})
85
94
searx/engines/google_images.py+2
@@ -47,11 +47,13 @@ def response(resp):
47 title = result['title']
47 title = result['title']
48 if not result['url']:
48 if not result['url']:
49 continue
49 continue
50+ thumbnail_src = result['tbUrl']
50
51
51 # append result
52 # append result
52 results.append({'url': href,
53 results.append({'url': href,
53 'title': title,
54 'title': title,
54 'content': '',
55 'content': '',
56+ 'thumbnail_src': thumbnail_src,
55 'img_src': unquote(result['url']),
57 'img_src': unquote(result['url']),
56 'template': 'images.html'})
58 'template': 'images.html'})
57
59
searx/templates/courgette/result_templates/images.html+1−1
@@ -1,6 +1,6 @@
1<div class="image_result">
1<div class="image_result">
2 <p>
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="{% 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>
4 <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
4 <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
5 </p>
5 </p>
6</div>
6</div>
searx/templates/default/result_templates/images.html+1−1
@@ -1,6 +1,6 @@
1<div class="image_result">
1<div class="image_result">
2 <p>
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="{% 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>
4 <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
4 <span class="url"><a href="{{ result.url }}" class="small_font">{{ _('original context') }}</a></span>
5 </p>
5 </p>
6</div>
6</div>
searx/templates/oscar/result_templates/images.html+2−2
@@ -1,7 +1,7 @@
1{% from 'oscar/macros.html' import draw_favicon %}
1{% from 'oscar/macros.html' import draw_favicon %}
2
2
3<a href="{{ result.img_src }}" data-toggle="modal" data-target="#modal-{{ index }}">
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="{% 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">
5</a>
5</a>
6
6
7<div class="modal fade" id="modal-{{ index }}" tabindex="-1" role="dialog" aria-hidden="true">
7<div class="modal fade" id="modal-{{ index }}" tabindex="-1" role="dialog" aria-hidden="true">
@@ -12,7 +12,7 @@
12 <h4 class="modal-title">{% if result.engine~".png" in favicons %}{{ draw_favicon(result.engine) }} {% endif %}{{ result.title|striptags }}</h4>
12 <h4 class="modal-title">{% if result.engine~".png" in favicons %}{{ draw_favicon(result.engine) }} {% endif %}{{ result.title|striptags }}</h4>
13 </div>
13 </div>
14 <div class="modal-body">
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="{% if result.thumbnail_src %}{{ image_proxify(result.thumbnail_src) }}{% else %}{{ image_proxify(result.img_src) }}{% endif %}" alt="{{ result.title|striptags }}">
16 {% if result.content %}<p class="result-content">{{ result.content|safe }}</p>{% endif %}
16 {% if result.content %}<p class="result-content">{{ result.content|safe }}</p>{% endif %}
17 </div>
17 </div>
18 <div class="modal-footer">
18 <div class="modal-footer">