Merge pull request #178 from Cqoicebordel/thumbnails

Thumbnails
This commit is contained in:
Adam Tauber 2015-01-17 20:25:13 +01:00
commit e3edded60f
9 changed files with 43 additions and 8 deletions

View File

@ -14,6 +14,7 @@
from urllib import urlencode from urllib import urlencode
from urlparse import urljoin from urlparse import urljoin
from lxml import html from lxml import html
import re
# engine dependent config # engine dependent config
categories = ['images'] categories = ['images']
@ -37,20 +38,25 @@ def response(resp):
results = [] results = []
dom = html.fromstring(resp.text) dom = html.fromstring(resp.text)
regex = re.compile('3\.jpg.*$')
# parse results # parse results
for result in dom.xpath('//div[@class="photo"]'): for result in dom.xpath('//div[@class="photo"]'):
link = result.xpath('.//a')[0] link = result.xpath('.//a')[0]
url = urljoin(base_url, link.attrib.get('href')) url = urljoin(base_url, link.attrib.get('href'))
title = result.xpath('.//div[@class="title"]//text()')[0] title = result.xpath('.//div[@class="title"]//text()')[0]
img_src = link.xpath('.//img')[0].attrib['src'] thumbnail_src = link.xpath('.//img')[0].attrib['src']
# To have a bigger thumbnail, uncomment the next line
#thumbnail_src = regex.sub('4.jpg', thumbnail_src)
content = result.xpath('.//div[@class="info"]//text()')[0] content = result.xpath('.//div[@class="info"]//text()')[0]
img_src = regex.sub('2048.jpg', thumbnail_src)
# append result # append result
results.append({'url': url, results.append({'url': url,
'title': title, 'title': title,
'img_src': img_src, 'img_src': img_src,
'content': content, 'content': content,
'thumbnail_src': thumbnail_src,
'template': 'images.html'}) 'template': 'images.html'})
# return results # return results

View File

@ -25,6 +25,7 @@ paging = True
# search-url # search-url
base_url = 'https://www.bing.com/' base_url = 'https://www.bing.com/'
search_string = 'images/search?{query}&count=10&first={offset}' search_string = 'images/search?{query}&count=10&first={offset}'
thumb_url = "http://ts1.mm.bing.net/th?id={ihk}"
# do search-request # do search-request
@ -63,6 +64,8 @@ def response(resp):
yaml_data = load(p.sub(r'\1\2: \3', link.attrib.get('m'))) yaml_data = load(p.sub(r'\1\2: \3', link.attrib.get('m')))
title = link.attrib.get('t1') title = link.attrib.get('t1')
ihk = link.attrib.get('ihk')
#url = 'http://' + link.attrib.get('t3') #url = 'http://' + link.attrib.get('t3')
url = yaml_data.get('surl') url = yaml_data.get('surl')
img_src = yaml_data.get('imgurl') img_src = yaml_data.get('imgurl')
@ -72,6 +75,7 @@ def response(resp):
'url': url, 'url': url,
'title': title, 'title': title,
'content': '', 'content': '',
'thumbnail_src': thumb_url.format(ihk=ihk),
'img_src': img_src}) 'img_src': img_src})
# TODO stop parsing if 10 images are found # TODO stop parsing if 10 images are found

View File

@ -6,13 +6,14 @@
# @using-api no (TODO, rewrite to api) # @using-api no (TODO, rewrite to api)
# @results HTML # @results HTML
# @stable no (HTML can change) # @stable no (HTML can change)
# @parse url, title, thumbnail, img_src # @parse url, title, thumbnail_src, img_src
# #
# @todo rewrite to api # @todo rewrite to api
from urllib import urlencode from urllib import urlencode
from urlparse import urljoin from urlparse import urljoin
from lxml import html from lxml import html
import re
# engine dependent config # engine dependent config
categories = ['images'] categories = ['images']
@ -43,18 +44,22 @@ def response(resp):
dom = html.fromstring(resp.text) dom = html.fromstring(resp.text)
regex = re.compile('\/200H\/')
# parse results # parse results
for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'): for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
link = result.xpath('.//a[contains(@class, "thumb")]')[0] link = result.xpath('.//a[contains(@class, "thumb")]')[0]
url = urljoin(base_url, link.attrib.get('href')) url = urljoin(base_url, link.attrib.get('href'))
title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
title = ''.join(title_links[0].xpath('.//text()')) title = ''.join(title_links[0].xpath('.//text()'))
img_src = link.xpath('.//img')[0].attrib['src'] thumbnail_src = link.xpath('.//img')[0].attrib['src']
img_src = regex.sub('/', thumbnail_src)
# append result # append result
results.append({'url': url, results.append({'url': url,
'title': title, 'title': title,
'img_src': img_src, 'img_src': img_src,
'thumbnail_src': thumbnail_src,
'template': 'images.html'}) 'template': 'images.html'})
# return results # return results

View File

@ -71,6 +71,14 @@ def response(resp):
if 'id' not in photo['owner']: if 'id' not in photo['owner']:
continue continue
# For a bigger thumbnail, keep only the url_z, not the url_n
if 'n' in photo['sizes']:
thumbnail_src = photo['sizes']['n']['displayUrl']
elif 'z' in photo['sizes']:
thumbnail_src = photo['sizes']['z']['displayUrl']
else:
thumbnail_src = img_src
url = build_flickr_url(photo['owner']['id'], photo['id']) url = build_flickr_url(photo['owner']['id'], photo['id'])
title = photo.get('title', '') title = photo.get('title', '')
@ -89,6 +97,7 @@ def response(resp):
results.append({'url': url, results.append({'url': url,
'title': title, 'title': title,
'img_src': img_src, 'img_src': img_src,
'thumbnail_src': thumbnail_src,
'content': content, 'content': content,
'template': 'images.html'}) 'template': 'images.html'})

View File

@ -23,7 +23,7 @@ api_key = None
url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\ url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\
'&api_key={api_key}&{text}&sort=relevance' +\ '&api_key={api_key}&{text}&sort=relevance' +\
'&extras=description%2C+owner_name%2C+url_o%2C+url_z' +\ '&extras=description%2C+owner_name%2C+url_o%2C+url_n%2C+url_z' +\
'&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}' '&per_page={nb_per_page}&format=json&nojsoncallback=1&page={page}'
photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}' photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
@ -65,6 +65,14 @@ def response(resp):
else: else:
continue continue
# For a bigger thumbnail, keep only the url_z, not the url_n
if 'url_n' in photo:
thumbnail_src = photo['url_n']
elif 'url_z' in photo:
thumbnail_src = photo['url_z']
else:
thumbnail_src = img_src
url = build_flickr_url(photo['owner'], photo['id']) url = build_flickr_url(photo['owner'], photo['id'])
title = photo['title'] title = photo['title']
@ -80,6 +88,7 @@ def response(resp):
results.append({'url': url, results.append({'url': url,
'title': title, 'title': title,
'img_src': img_src, 'img_src': img_src,
'thumbnail_src': thumbnail_src,
'content': content, 'content': content,
'template': 'images.html'}) 'template': 'images.html'})

View File

@ -47,11 +47,13 @@ def response(resp):
title = result['title'] title = result['title']
if not result['url']: if not result['url']:
continue continue
thumbnail_src = result['tbUrl']
# append result # append result
results.append({'url': href, results.append({'url': href,
'title': title, 'title': title,
'content': '', 'content': '',
'thumbnail_src': thumbnail_src,
'img_src': unquote(result['url']), 'img_src': unquote(result['url']),
'template': 'images.html'}) 'template': 'images.html'})

View File

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

View File

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

View File

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