[fix] bing_videos (#1579)

use JSON where possible, compose 'content' using all available data, use correct 'url' (direct to source instead of redirect through bing)
This commit is contained in:
Frank de Lange 2019-07-27 17:49:30 +02:00 committed by Alexandre Flament
parent 4b56a4bfaa
commit 204a2cbbf0
2 changed files with 40 additions and 62 deletions

View File

@ -13,7 +13,6 @@
from json import loads from json import loads
from lxml import html from lxml import html
from searx.engines.bing_images import _fetch_supported_languages, supported_languages_url from searx.engines.bing_images import _fetch_supported_languages, supported_languages_url
from searx.engines.xpath import extract_text
from searx.url_utils import urlencode from searx.url_utils import urlencode
from searx.utils import match_language from searx.utils import match_language
@ -22,11 +21,16 @@ categories = ['videos']
paging = True paging = True
safesearch = True safesearch = True
time_range_support = True time_range_support = True
number_of_results = 10 number_of_results = 28
language_support = True language_support = True
search_url = 'https://www.bing.com/videos/asyncv2?{query}&async=content&'\ base_url = 'https://www.bing.com/'
'first={offset}&count={number_of_results}&CW=1366&CH=25&FORM=R5VR5' search_string = 'videos/search'\
'?{query}'\
'&count={count}'\
'&first={first}'\
'&scope=video'\
'&FORM=QBLH'
time_range_string = '&qft=+filterui:videoage-lt{interval}' time_range_string = '&qft=+filterui:videoage-lt{interval}'
time_range_dict = {'day': '1440', time_range_dict = {'day': '1440',
'week': '10080', 'week': '10080',
@ -41,7 +45,12 @@ safesearch_types = {2: 'STRICT',
# do search-request # do search-request
def request(query, params): def request(query, params):
offset = (params['pageno'] - 1) * 10 + 1 offset = ((params['pageno'] - 1) * number_of_results) + 1
search_path = search_string.format(
query=urlencode({'q': query}),
count=number_of_results,
first=offset)
# safesearch cookie # safesearch cookie
params['cookies']['SRCHHPGUSR'] = \ params['cookies']['SRCHHPGUSR'] = \
@ -52,9 +61,7 @@ def request(query, params):
params['cookies']['_EDGE_S'] = 'mkt=' + language + '&F=1' params['cookies']['_EDGE_S'] = 'mkt=' + language + '&F=1'
# query and paging # query and paging
params['url'] = search_url.format(query=urlencode({'q': query}), params['url'] = base_url + search_path
offset=offset,
number_of_results=number_of_results)
# time range # time range
if params['time_range'] in time_range_dict: if params['time_range'] in time_range_dict:
@ -70,19 +77,18 @@ def response(resp):
dom = html.fromstring(resp.text) dom = html.fromstring(resp.text)
for result in dom.xpath('//div[@class="dg_u"]'): for result in dom.xpath('//div[@class="dg_u"]'):
url = result.xpath('./div[@class="mc_vtvc"]/a/@href')[0] try:
url = 'https://bing.com' + url metadata = loads(result.xpath('.//div[@class="vrhdata"]/@vrhm')[0])
title = extract_text(result.xpath('./div/a/div/div[@class="mc_vtvc_title"]/@title')) info = ' - '.join(result.xpath('.//div[@class="mc_vtvc_meta_block"]//span/text()')).strip()
content = extract_text(result.xpath('./div/a/div/div/div/div/text()')) content = '{0} - {1}'.format(metadata['du'], info)
thumbnail = result.xpath('./div/a/div/div/img/@src')[0] thumbnail = '{0}th?id={1}'.format(base_url, metadata['thid'])
results.append({'url': metadata['murl'],
results.append({'url': url,
'title': title,
'content': content,
'thumbnail': thumbnail, 'thumbnail': thumbnail,
'title': metadata.get('vt', ''),
'content': content,
'template': 'videos.html'}) 'template': 'videos.html'})
if len(results) >= number_of_results: except:
break continue
return results return results

View File

@ -29,7 +29,7 @@ class TestBingVideosEngine(SearxTestCase):
dicto['time_range'] = 'day' dicto['time_range'] = 'day'
dicto['safesearch'] = 2 dicto['safesearch'] = 2
params = bing_videos.request(query, dicto) params = bing_videos.request(query, dicto)
self.assertTrue('first=11' in params['url']) self.assertTrue('first=29' in params['url'])
self.assertTrue('1440' in params['url']) self.assertTrue('1440' in params['url'])
self.assertIn('SRCHHPGUSR', params['cookies']) self.assertIn('SRCHHPGUSR', params['cookies'])
self.assertTrue('STRICT' in params['cookies']['SRCHHPGUSR']) self.assertTrue('STRICT' in params['cookies']['SRCHHPGUSR'])
@ -48,44 +48,16 @@ class TestBingVideosEngine(SearxTestCase):
html = """ html = """
<div class="dg_u"> <div class="dg_u">
<div id="mc_vtvc_1" class="mc_vtvc"> <div>
<a class="mc_vtvc_link" href="/video"> <a>
<div class="mc_vtvc_th"> <div>
<div class="cico"> <div>
<img src="thumb_1.jpg" />
</div>
<div class="mc_vtvc_ban_lo">
<div class="vtbc">
<div class="mc_bc_w b_smText">
<div class="mc_bc pivot bpi_2">
<span title="">
<span class="mv_vtvc_play cipg "></span>
</span>
</div>
<div class="mc_bc items">10:06</div>
</div>
</div>
</div>
</div>
<div class="mc_vtvc_meta">
<div class="mc_vtvc_title" title="Title 1"></div>
<div class="mc_vtvc_meta_block_area">
<div class="mc_vtvc_meta_block"> <div class="mc_vtvc_meta_block">
<div class="mc_vtvc_meta_row"> <div><span>100 views</span><span>1 year ago</span></div><div><span>ExampleTube</span><span>Channel 1<span></div> #noqa
<span>65,696,000+ views</span>
<span>1 year ago</span>
</div>
<div class="mc_vtvc_meta_row mc_vtvc_meta_row_channel">Content 1</div>
<div class="mc_vtvc_meta_row"><span>
<div class="cico mc_vtvc_src_ico">
<div></div>
</div>
<span>YouTube</span>
</span></div>
</div> </div>
</div> </div>
<div class="vrhdata" vrhm='{"du":"01:11","murl":"https://www.example.com/watch?v=DEADBEEF","thid":"OVP.BINGTHUMB1","vt":"Title 1"}'></div> # noqa
</div> </div>
<div class="vrhdata"></div>
</a> </a>
</div> </div>
</div> </div>
@ -95,6 +67,6 @@ class TestBingVideosEngine(SearxTestCase):
self.assertEqual(type(results), list) self.assertEqual(type(results), list)
self.assertEqual(len(results), 1) self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Title 1') self.assertEqual(results[0]['title'], 'Title 1')
self.assertEqual(results[0]['url'], 'https://bing.com/video') self.assertEqual(results[0]['url'], 'https://www.example.com/watch?v=DEADBEEF')
self.assertEqual(results[0]['content'], 'Content 1') self.assertEqual(results[0]['content'], '01:11 - 100 views - 1 year ago - ExampleTube - Channel 1')
self.assertEqual(results[0]['thumbnail'], 'thumb_1.jpg') self.assertEqual(results[0]['thumbnail'], 'https://www.bing.com/th?id=OVP.BINGTHUMB1')