Ponysearch/searx/engines/google_images.py

59 lines
1.5 KiB
Python
Raw Normal View History

2014-09-01 15:10:05 +02:00
## Google (Images)
#
# @website https://www.google.com
# @provide-api yes (https://developers.google.com/web-search/docs/), deprecated!
#
# @using-api yes
# @results JSON
# @stable yes (but deprecated)
# @parse url, title, img_src
2013-10-19 22:19:14 +02:00
2013-10-19 23:12:18 +02:00
from urllib import urlencode
from json import loads
2013-10-19 22:19:14 +02:00
2014-09-01 15:10:05 +02:00
# engine dependent config
2013-10-19 22:19:31 +02:00
categories = ['images']
2014-09-01 15:10:05 +02:00
paging = True
2013-10-19 22:19:14 +02:00
2014-09-01 15:10:05 +02:00
# search-url
2013-10-23 23:55:37 +02:00
url = 'https://ajax.googleapis.com/'
2014-01-30 01:21:33 +01:00
search_url = url + 'ajax/services/search/images?v=1.0&start={offset}&rsz=large&safe=off&filter=off&{query}' # noqa
2014-01-20 02:31:20 +01:00
2013-10-19 22:19:14 +02:00
2014-09-01 15:10:05 +02:00
# do search-request
2013-10-19 22:19:14 +02:00
def request(query, params):
2014-01-30 01:21:33 +01:00
offset = (params['pageno'] - 1) * 8
2014-09-01 15:10:05 +02:00
2014-01-30 01:21:33 +01:00
params['url'] = search_url.format(query=urlencode({'q': query}),
offset=offset)
2014-09-01 15:10:05 +02:00
2013-10-19 22:19:14 +02:00
return params
2014-01-20 02:31:20 +01:00
2014-09-01 15:10:05 +02:00
# get response from search-request
2013-10-19 22:19:14 +02:00
def response(resp):
results = []
2014-09-01 15:10:05 +02:00
2013-10-19 23:12:18 +02:00
search_res = loads(resp.text)
2014-09-01 15:10:05 +02:00
# return empty array if there are no results
if not search_res.get('responseData', {}).get('results'):
2013-10-20 19:45:13 +02:00
return []
2014-09-01 15:10:05 +02:00
# parse results
2013-10-19 23:12:18 +02:00
for result in search_res['responseData']['results']:
2013-10-23 23:55:37 +02:00
href = result['originalContextUrl']
2013-10-19 23:12:18 +02:00
title = result['title']
2013-10-22 23:35:17 +02:00
if not result['url']:
continue
2014-09-01 15:10:05 +02:00
# append result
2014-01-20 02:31:20 +01:00
results.append({'url': href,
'title': title,
'content': '',
'img_src': result['url'],
'template': 'images.html'})
2014-09-01 15:10:05 +02:00
# return results
2013-10-19 22:19:14 +02:00
return results