Files
searxng/searx/engines/flickr-noapi.py
T

96 lines
2.4 KiB
Python
Raw Normal View History

2014-12-16 20:40:03 +01:00
#!/usr/bin/env python
# Flickr (Images)
#
2014-12-16 20:40:03 +01:00
# @website https://www.flickr.com
# @provide-api yes (https://secure.flickr.com/services/api/flickr.photos.search.html)
#
2014-12-16 20:40:03 +01:00
# @using-api no
# @results HTML
# @stable no
# @parse url, title, thumbnail, img_src
from urllib import urlencode
from json import loads
import re
categories = ['images']
url = 'https://secure.flickr.com/'
search_url = url+'search/?{query}&page={page}'
photo_url = 'https://www.flickr.com/photos/{userid}/{photoid}'
regex = re.compile(r"\"search-photos-models\",\"photos\":(.*}),\"totalItems\":", re.DOTALL)
image_sizes = ('o', 'k', 'h', 'b', 'c', 'z', 'n', 'm', 't', 'q', 's')
2014-12-16 20:40:03 +01:00
paging = True
2014-12-16 20:40:03 +01:00
def build_flickr_url(user_id, photo_id):
return photo_url.format(userid=user_id, photoid=photo_id)
2014-12-16 20:40:03 +01:00
def request(query, params):
params['url'] = search_url.format(query=urlencode({'text': query}),
page=params['pageno'])
return params
def response(resp):
results = []
2014-12-16 20:40:03 +01:00
matches = regex.search(resp.text)
if matches is None:
2014-12-16 20:40:03 +01:00
return results
match = matches.group(1)
search_results = loads(match)
if '_data' not in search_results:
2014-12-16 20:40:03 +01:00
return []
2014-12-16 20:40:03 +01:00
photos = search_results['_data']
2014-12-16 20:40:03 +01:00
for photo in photos:
2014-12-29 21:31:04 +01:00
# In paged configuration, the first pages' photos
# are represented by a None object
if photo is None:
2014-12-16 20:40:03 +01:00
continue
img_src = None
2014-12-16 20:40:03 +01:00
# From the biggest to the lowest format
for image_size in image_sizes:
if image_size in photo['sizes']:
img_src = photo['sizes'][image_size]['displayUrl']
break
if not img_src:
continue
if 'id' not in photo['owner']:
2014-12-16 20:40:03 +01:00
continue
2014-12-16 20:40:03 +01:00
url = build_flickr_url(photo['owner']['id'], photo['id'])
title = photo['title']
2014-12-29 21:31:04 +01:00
content = '<span class="photo-author">' +\
photo['owner']['username'] +\
'</span><br />'
2014-12-16 20:40:03 +01:00
if 'description' in photo:
2014-12-29 21:31:04 +01:00
content = content +\
2015-01-02 12:33:40 +01:00
'<span class="description">' +\
photo['description'] +\
'</span>'
2014-12-16 20:40:03 +01:00
# append result
results.append({'url': url,
'title': title,
'img_src': img_src,
'content': content,
'template': 'images.html'})
2014-12-16 20:40:03 +01:00
return results