2015-05-02 13:45:17 +00:00
|
|
|
"""
|
|
|
|
Bing (Images)
|
|
|
|
|
|
|
|
@website https://www.bing.com/images
|
|
|
|
@provide-api yes (http://datamarket.azure.com/dataset/bing/search),
|
|
|
|
max. 5000 query/month
|
|
|
|
|
|
|
|
@using-api no (because of query limit)
|
|
|
|
@results HTML (using search portal)
|
|
|
|
@stable no (HTML can change)
|
|
|
|
@parse url, title, img_src
|
|
|
|
|
|
|
|
@todo currently there are up to 35 images receive per page,
|
|
|
|
because bing does not parse count=10.
|
|
|
|
limited response to 10 images
|
|
|
|
"""
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
from lxml import html
|
2016-02-01 20:40:28 +00:00
|
|
|
from json import loads
|
2014-09-01 12:39:18 +00:00
|
|
|
import re
|
2016-12-15 06:34:43 +00:00
|
|
|
from searx.engines.bing import _fetch_supported_languages, supported_languages_url
|
2016-11-30 17:43:03 +00:00
|
|
|
from searx.url_utils import urlencode
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
# engine dependent config
|
|
|
|
categories = ['images']
|
|
|
|
paging = True
|
2015-02-08 20:53:37 +00:00
|
|
|
safesearch = True
|
2016-10-30 19:58:34 +00:00
|
|
|
time_range_support = True
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
# search-url
|
|
|
|
base_url = 'https://www.bing.com/'
|
|
|
|
search_string = 'images/search?{query}&count=10&first={offset}'
|
2016-10-30 19:58:34 +00:00
|
|
|
time_range_string = '&qft=+filterui:age-lt{interval}'
|
2015-06-06 17:23:07 +00:00
|
|
|
thumb_url = "https://www.bing.com/th?id={ihk}"
|
2016-10-30 19:58:34 +00:00
|
|
|
time_range_dict = {'day': '1440',
|
|
|
|
'week': '10080',
|
2016-12-11 15:39:12 +00:00
|
|
|
'month': '43200',
|
|
|
|
'year': '525600'}
|
2014-09-01 12:39:18 +00:00
|
|
|
|
2015-02-08 20:53:37 +00:00
|
|
|
# safesearch definitions
|
|
|
|
safesearch_types = {2: 'STRICT',
|
|
|
|
1: 'DEMOTE',
|
|
|
|
0: 'OFF'}
|
2014-09-02 15:13:44 +00:00
|
|
|
|
2015-02-08 21:01:24 +00:00
|
|
|
|
2016-02-01 20:40:28 +00:00
|
|
|
_quote_keys_regex = re.compile('({|,)([a-z][a-z0-9]*):(")', re.I | re.U)
|
|
|
|
|
|
|
|
|
2014-09-01 12:39:18 +00:00
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
|
|
|
offset = (params['pageno'] - 1) * 10 + 1
|
|
|
|
|
|
|
|
# required for cookie
|
2015-01-29 19:15:52 +00:00
|
|
|
if params['language'] == 'all':
|
|
|
|
language = 'en-US'
|
|
|
|
else:
|
2016-08-06 04:34:56 +00:00
|
|
|
language = params['language']
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
search_path = search_string.format(
|
|
|
|
query=urlencode({'q': query}),
|
|
|
|
offset=offset)
|
|
|
|
|
|
|
|
params['cookies']['SRCHHPGUSR'] = \
|
2015-02-08 20:53:37 +00:00
|
|
|
'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0] +\
|
|
|
|
'&ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE')
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
params['url'] = base_url + search_path
|
2016-10-30 19:58:34 +00:00
|
|
|
if params['time_range'] in time_range_dict:
|
|
|
|
params['url'] += time_range_string.format(interval=time_range_dict[params['time_range']])
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
|
|
|
|
2015-08-28 12:51:32 +00:00
|
|
|
dom = html.fromstring(resp.text)
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
# parse results
|
2016-03-26 01:07:52 +00:00
|
|
|
for result in dom.xpath('//div[@class="dg_u"]/div'):
|
2014-09-01 12:39:18 +00:00
|
|
|
link = result.xpath('./a')[0]
|
|
|
|
|
2016-02-01 20:40:28 +00:00
|
|
|
# parse json-data (it is required to add a space, to make it parsable)
|
|
|
|
json_data = loads(_quote_keys_regex.sub(r'\1"\2": \3', link.attrib.get('m')))
|
2014-12-07 15:37:56 +00:00
|
|
|
|
2014-09-01 12:39:18 +00:00
|
|
|
title = link.attrib.get('t1')
|
2015-01-17 18:21:09 +00:00
|
|
|
ihk = link.attrib.get('ihk')
|
2015-01-17 18:24:35 +00:00
|
|
|
|
2015-05-02 13:45:17 +00:00
|
|
|
# url = 'http://' + link.attrib.get('t3')
|
2016-02-01 20:40:28 +00:00
|
|
|
url = json_data.get('surl')
|
|
|
|
img_src = json_data.get('imgurl')
|
2014-09-01 12:39:18 +00:00
|
|
|
|
|
|
|
# append result
|
|
|
|
results.append({'template': 'images.html',
|
|
|
|
'url': url,
|
|
|
|
'title': title,
|
2014-12-07 15:37:56 +00:00
|
|
|
'content': '',
|
2015-01-17 18:21:09 +00:00
|
|
|
'thumbnail_src': thumb_url.format(ihk=ihk),
|
2014-09-01 12:39:18 +00:00
|
|
|
'img_src': img_src})
|
|
|
|
|
|
|
|
# TODO stop parsing if 10 images are found
|
|
|
|
if len(results) >= 10:
|
|
|
|
break
|
|
|
|
|
|
|
|
# return results
|
|
|
|
return results
|