소스 검색

[fix] bing images changes

Adam Tauber 9 년 전
부모
커밋
ebbf33343a
2개의 변경된 파일15개의 추가작업 그리고 21개의 파일을 삭제
  1. 8
    8
      searx/engines/bing_images.py
  2. 7
    13
      tests/unit/engines/test_bing_images.py

+ 8
- 8
searx/engines/bing_images.py 파일 보기

@@ -17,7 +17,7 @@
17 17
 
18 18
 from urllib import urlencode
19 19
 from lxml import html
20
-from yaml import load
20
+from json import loads
21 21
 import re
22 22
 
23 23
 # engine dependent config
@@ -36,6 +36,9 @@ safesearch_types = {2: 'STRICT',
36 36
                     0: 'OFF'}
37 37
 
38 38
 
39
+_quote_keys_regex = re.compile('({|,)([a-z][a-z0-9]*):(")', re.I | re.U)
40
+
41
+
39 42
 # do search-request
40 43
 def request(query, params):
41 44
     offset = (params['pageno'] - 1) * 10 + 1
@@ -65,22 +68,19 @@ def response(resp):
65 68
 
66 69
     dom = html.fromstring(resp.text)
67 70
 
68
-    # init regex for yaml-parsing
69
-    p = re.compile('({|,)([a-z]+):(")')
70
-
71 71
     # parse results
72 72
     for result in dom.xpath('//div[@class="dg_u"]'):
73 73
         link = result.xpath('./a')[0]
74 74
 
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')))
77 77
 
78 78
         title = link.attrib.get('t1')
79 79
         ihk = link.attrib.get('ihk')
80 80
 
81 81
         # 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')
84 84
 
85 85
         # append result
86 86
         results.append({'template': 'images.html',

+ 7
- 13
tests/unit/engines/test_bing_images.py 파일 보기

@@ -39,16 +39,10 @@ class TestBingImagesEngine(SearxTestCase):
39 39
 
40 40
         html = """
41 41
         <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">
51 44
             </a>
45
+
52 46
         </div>
53 47
         """
54 48
         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
56 50
         results = bing_images.response(response)
57 51
         self.assertEqual(type(results), list)
58 52
         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')
61 55
         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')
64 58
 
65 59
         html = """
66 60
         <a href="#" ihk="HN.608003696942779811"