1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/google_images.py

74 lines
1.9 KiB
Python
Raw Normal View History

2015-09-08 23:05:37 +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
"""
2014-05-10 11:56:19 +02:00
2015-01-13 16:40:30 +01:00
from urllib import urlencode, unquote
2014-05-10 11:56:19 +02:00
from json import loads
2014-12-01 12:26:38 +01:00
# engine dependent config
2014-05-10 11:56:19 +02:00
categories = ['images']
2014-12-01 12:26:38 +01:00
paging = True
2015-02-17 12:45:54 +01:00
safesearch = True
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# search-url
2014-05-10 11:56:19 +02:00
url = 'https://ajax.googleapis.com/'
2015-02-17 12:45:54 +01:00
search_url = url + 'ajax/services/search/images?v=1.0&start={offset}&rsz=large&safe={safesearch}&filter=off&{query}'
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
offset = (params['pageno'] - 1) * 8
2014-12-01 12:26:38 +01:00
2015-02-17 12:45:54 +01:00
if params['safesearch'] == 0:
safesearch = 'off'
else:
safesearch = 'on'
2014-05-10 11:56:19 +02:00
params['url'] = search_url.format(query=urlencode({'q': query}),
2015-02-17 12:45:54 +01:00
offset=offset,
safesearch=safesearch)
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
results = []
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
search_res = loads(resp.text)
2014-12-01 12:26:38 +01:00
# return empty array if there are no results
if not search_res.get('responseData', {}).get('results'):
2014-05-10 11:56:19 +02:00
return []
2014-12-01 12:26:38 +01:00
# parse results
2014-05-10 11:56:19 +02:00
for result in search_res['responseData']['results']:
href = result['originalContextUrl']
title = result['title']
2015-02-09 13:30:16 +01:00
if 'url' not in result:
2014-05-10 11:56:19 +02:00
continue
2015-02-09 13:30:16 +01:00
thumbnail_src = result['tbUrl']
2014-12-01 12:26:38 +01:00
2015-09-08 23:05:37 +02:00
# http to https
thumbnail_src = thumbnail_src.replace("http://", "https://")
2014-12-01 12:26:38 +01:00
# append result
2014-05-10 11:56:19 +02:00
results.append({'url': href,
'title': title,
2015-02-09 13:30:16 +01:00
'content': result['content'],
'thumbnail_src': thumbnail_src,
2014-12-13 13:56:02 +01:00
'img_src': unquote(result['url']),
2014-05-10 11:56:19 +02:00
'template': 'images.html'})
2014-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results