on master help sign in
[fix] bing images changes
commitebbf33343a19089ea7b121ae3001dc528a4d7bf3 authorAdam Tauber <asciimoo@gmail.com> dateMon 1 Feb 2016, 21:40 (11 years ago) parent37035b7
2 files changed+15−21
searx/engines/bing_images.py+8−8 tests/unit/engines/test_bing_images.py+7−13
searx/engines/bing_images.py+8−8
@@ -17,7 +17,7 @@
1717
1818from urllib import urlencode
1919from lxml import html
20−from yaml import load
20+from json import loads
2121import re
2222
2323# engine dependent config
@@ -36,6 +36,9 @@ safesearch_types = {2: 'STRICT',
3636 0: 'OFF'}
3737
3838
39+_quote_keys_regex = re.compile('({|,)([a-z][a-z0-9]*):(")', re.I | re.U)
40+
41+
3942# do search-request
4043def request(query, params):
4144 offset = (params['pageno'] - 1) * 10 + 1
@@ -65,22 +68,19 @@ def response(resp):
6568
6669 dom = html.fromstring(resp.text)
6770
68− # init regex for yaml-parsing
69− p = re.compile('({|,)([a-z]+):(")')
70−
7171 # parse results
7272 for result in dom.xpath('//div[@class="dg_u"]'):
7373 link = result.xpath('./a')[0]
7474
75− # parse yaml-data (it is required to add a space, to make it parsable)
76− yaml_data = load(p.sub(r'\1\2: \3', link.attrib.get('m')))
75+ # parse json-data (it is required to add a space, to make it parsable)
76+ json_data = loads(_quote_keys_regex.sub(r'\1"\2": \3', link.attrib.get('m')))
7777
7878 title = link.attrib.get('t1')
7979 ihk = link.attrib.get('ihk')
8080
8181 # url = 'http://' + link.attrib.get('t3')
82− url = yaml_data.get('surl')
83− img_src = yaml_data.get('imgurl')
82+ url = json_data.get('surl')
83+ img_src = json_data.get('imgurl')
8484
8585 # append result
8686 results.append({'template': 'images.html',
tests/unit/engines/test_bing_images.py+7−13
@@ -39,16 +39,10 @@ class TestBingImagesEngine(SearxTestCase):
3939
4040 html = """
4141 <div class="dg_u" style="width:178px;height:144px;left:17px;top:0px">
42− <a href="#" ihk="HN.608003696942779811"
43− m="{ns:&quot;images&quot;,k:&quot;5045&quot;,
44−mid:&quot;659EB92C317974F34517A1CCAEBEF76A578E08DEE&quot;,
45−surl:&quot;http://www.page.url/&quot;,imgurl:&quot;http://test.url/Test%20Query.jpg&quot;,
46−oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%20Query.jpg&quot;}"
47− mid="59EB92C317974F34517A1CCAEBEF76A578E08DEE" onclick="return false;"
48− t1="Test Query" t2="650 x 517 · 31 kB · jpeg" t3="www.short.url" h="ID=images,5045.1">
49− <img src="https://tse4.mm.bing.net/th?id=HN.608003696942779811&amp;o=4&amp;pid=1.7"
50− style="height:144px;" width="178" height="144"/>
42+ <a href="/images/search?q=south&amp;view=detailv2&amp;&amp;id=7E92863981CCFB89FBDD55205C742DFDA3290CF6&amp;selectedIndex=9&amp;ccid=vzvIfv5u&amp;simid=608055786735667000&amp;thid=OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0" ihk="OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0" t1="South Carolina" t2="747 x 589 &#183; 29 kB &#183; gif" t3="www.digital-topo-maps.com/county-map/south-carolina.shtml" hh="236" hw="300" m='{ns:"images",k:"5117",mid:"7E92863981CCFB89FBDD55205C742DFDA3290CF6",md5:"bf3bc87efe6e0e476be8cc34bf6cd80e",surl:"http://www.digital-topo-maps.com/county-map/south-carolina.shtml",imgurl:"http://www.digital-topo-maps.com/county-map/south-carolina-county-map.gif",tid:"OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0",ow:"480",docid:"608055786735667000",oh:"378",tft:"45"}' mid="7E92863981CCFB89FBDD55205C742DFDA3290CF6" h="ID=images,5117.1">
43+ <img class="img_hid" src2="https://tse4.mm.bing.net/th?id=OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0&amp;w=210&amp;h=154&amp;c=7&amp;rs=1&amp;qlt=90&amp;o=4&amp;pid=1.1" style="width:210px;height:154px;" width="210" height="154">
5144 </a>
45+
5246 </div>
5347 """
5448 html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
@@ -56,11 +50,11 @@ oh:&quot;238&quot;,tft:&quot;0&quot;,oi:&quot;http://www.image.url/Images/Test%2
5650 results = bing_images.response(response)
5751 self.assertEqual(type(results), list)
5852 self.assertEqual(len(results), 1)
59− self.assertEqual(results[0]['title'], 'Test Query')
60− self.assertEqual(results[0]['url'], 'http://www.page.url/')
53+ self.assertEqual(results[0]['title'], 'South Carolina')
54+ self.assertEqual(results[0]['url'], 'http://www.digital-topo-maps.com/county-map/south-carolina.shtml')
6155 self.assertEqual(results[0]['content'], '')
62− self.assertEqual(results[0]['thumbnail_src'], 'https://www.bing.com/th?id=HN.608003696942779811')
63− self.assertEqual(results[0]['img_src'], 'http://test.url/Test%20Query.jpg')
56+ self.assertEqual(results[0]['thumbnail_src'], 'https://www.bing.com/th?id=OIP.Mbf3bc87efe6e0e476be8cc34bf6cd80eH0')
57+ self.assertEqual(results[0]['img_src'], 'http://www.digital-topo-maps.com/county-map/south-carolina-county-map.gif')
6458
6559 html = """
6660 <a href="#" ihk="HN.608003696942779811"