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 urlparse import urljoin
from lxml import html
import re
# engine dependent config
categories = ['images']
@ -37,20 +38,25 @@ def response(resp):
results = []
dom = html.fromstring(resp.text)
regex = re.compile('3\.jpg.*$')
# parse results
for result in dom.xpath('//div[@class="photo"]'):
link = result.xpath('.//a')[0]
url = urljoin(base_url, link.attrib.get('href'))
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]
img_src = regex.sub('2048.jpg', thumbnail_src)
# append result
results.append({'url': url,
'title': title,
'img_src': img_src,
'content': content,
'thumbnail_src': thumbnail_src,
'template': 'images.html'})
# return results

View file

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

View file

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

View file

@ -71,6 +71,14 @@ def response(resp):
if 'id' not in photo['owner']:
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'])
title = photo.get('title', '')
@ -89,6 +97,7 @@ def response(resp):
results.append({'url': url,
'title': title,
'img_src': img_src,
'thumbnail_src': thumbnail_src,
'content': content,
'template': 'images.html'})

View file

@ -23,7 +23,7 @@ api_key = None
url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search' +\
'&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}'
photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
@ -65,6 +65,14 @@ def response(resp):
else:
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'])
title = photo['title']
@ -80,6 +88,7 @@ def response(resp):
results.append({'url': url,
'title': title,
'img_src': img_src,
'thumbnail_src': thumbnail_src,
'content': content,
'template': 'images.html'})

View file

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

View file

@ -1,6 +1,6 @@
<div class="image_result">
<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>
</p>
</div>

View file

@ -1,6 +1,6 @@
<div class="image_result">
<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>
</p>
</div>

View file

@ -1,7 +1,7 @@
{% from 'oscar/macros.html' import draw_favicon %}
<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>
<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>
</div>
<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 %}
</div>
<div class="modal-footer">