瀏覽代碼

[enh] add blekko_images engine

Thomas Pointhuber 10 年之前
父節點
當前提交
dd4686a388
共有 4 個檔案被更改,包括 127 行新增0 行删除
  1. 56
    0
      searx/engines/blekko_images.py
  2. 5
    0
      searx/settings.yml
  3. 65
    0
      searx/tests/engines/test_blekko_images.py
  4. 1
    0
      searx/tests/test_engines.py

+ 56
- 0
searx/engines/blekko_images.py 查看文件

@@ -0,0 +1,56 @@
1
+## Blekko (Images)
2
+#
3
+# @website     https://blekko.com
4
+# @provide-api yes (inofficial)
5
+#
6
+# @using-api   yes
7
+# @results     JSON
8
+# @stable      yes
9
+# @parse       url, title, img_src
10
+
11
+from json import loads
12
+from urllib import urlencode
13
+
14
+# engine dependent config
15
+categories = ['images']
16
+paging = True
17
+
18
+# search-url
19
+base_url = 'https://blekko.com'
20
+search_url = '/api/images?{query}&c={c}'
21
+
22
+
23
+# do search-request
24
+def request(query, params):
25
+    c = (params['pageno'] - 1) * 48
26
+
27
+    params['url'] = base_url +\
28
+        search_url.format(query=urlencode({'q': query}),
29
+                          c=c)
30
+
31
+    if params['pageno'] != 1:
32
+        params['url'] += '&page={pageno}'.format(pageno=(params['pageno']-1))
33
+
34
+    return params
35
+
36
+
37
+# get response from search-request
38
+def response(resp):
39
+    results = []
40
+
41
+    search_results = loads(resp.text)
42
+
43
+    # return empty array if there are no results
44
+    if not search_results:
45
+        return []
46
+
47
+    for result in search_results:
48
+        # append result
49
+        results.append({'url': result['page_url'],
50
+                        'title': result['title'],
51
+                        'content': '',
52
+                        'img_src': result['url'],
53
+                        'template': 'images.html'})
54
+
55
+    # return results
56
+    return results

+ 5
- 0
searx/settings.yml 查看文件

@@ -33,6 +33,11 @@ engines:
33 33
     locale : en-US
34 34
     shortcut : bin
35 35
 
36
+  - name : blekko images
37
+    engine : blekko_images
38
+    locale : en-US
39
+    shortcut : bli
40
+
36 41
   - name : btdigg
37 42
     engine : btdigg
38 43
     shortcut : bt

+ 65
- 0
searx/tests/engines/test_blekko_images.py 查看文件

@@ -0,0 +1,65 @@
1
+from collections import defaultdict
2
+import mock
3
+from searx.engines import blekko_images
4
+from searx.testing import SearxTestCase
5
+
6
+
7
+class TestBlekkoImagesEngine(SearxTestCase):
8
+
9
+    def test_request(self):
10
+        query = 'test_query'
11
+        dicto = defaultdict(dict)
12
+        dicto['pageno'] = 0
13
+        params = blekko_images.request(query, dicto)
14
+        self.assertTrue('url' in params)
15
+        self.assertTrue(query in params['url'])
16
+        self.assertTrue('blekko.com' in params['url'])
17
+
18
+    def test_response(self):
19
+        self.assertRaises(AttributeError, blekko_images.response, None)
20
+        self.assertRaises(AttributeError, blekko_images.response, [])
21
+        self.assertRaises(AttributeError, blekko_images.response, '')
22
+        self.assertRaises(AttributeError, blekko_images.response, '[]')
23
+
24
+        response = mock.Mock(text='[]')
25
+        self.assertEqual(blekko_images.response(response), [])
26
+
27
+        json = """
28
+        [
29
+            {
30
+                "c": 1,
31
+                "page_url": "http://result_url.html",
32
+                "title": "Photo title",
33
+                "tn_url": "http://ts1.mm.bing.net/th?id=HN.608050619474382748&pid=15.1",
34
+                "url": "http://result_image.jpg"
35
+            },
36
+            {
37
+                "c": 2,
38
+                "page_url": "http://companyorange.simpsite.nl/OSM",
39
+                "title": "OSM",
40
+                "tn_url": "http://ts2.mm.bing.net/th?id=HN.608048068264919461&pid=15.1",
41
+                "url": "http://simpsite.nl/userdata2/58985/Home/OSM.bmp"
42
+            },
43
+            {
44
+                "c": 3,
45
+                "page_url": "http://invincible.webklik.nl/page/osm",
46
+                "title": "OSM",
47
+                "tn_url": "http://ts1.mm.bing.net/th?id=HN.608024514657649476&pid=15.1",
48
+                "url": "http://www.webklik.nl/user_files/2009_09/65324/osm.gif"
49
+            },
50
+            {
51
+                "c": 4,
52
+                "page_url": "http://www.offshorenorway.no/event/companyDetail/id/12492",
53
+                "title": "Go to OSM Offshore AS homepage",
54
+                "tn_url": "http://ts2.mm.bing.net/th?id=HN.608054265899847285&pid=15.1",
55
+                "url": "http://www.offshorenorway.no/firmalogo/OSM-logo.png"
56
+            }
57
+        ]
58
+        """
59
+        response = mock.Mock(text=json)
60
+        results = blekko_images.response(response)
61
+        self.assertEqual(type(results), list)
62
+        self.assertEqual(len(results), 4)
63
+        self.assertEqual(results[0]['title'], 'Photo title')
64
+        self.assertEqual(results[0]['url'], 'http://result_url.html')
65
+        self.assertEqual(results[0]['img_src'], 'http://result_image.jpg')

+ 1
- 0
searx/tests/test_engines.py 查看文件

@@ -1,6 +1,7 @@
1 1
 from searx.tests.engines.test_bing import *  # noqa
2 2
 from searx.tests.engines.test_bing_images import *  # noqa
3 3
 from searx.tests.engines.test_bing_news import *  # noqa
4
+from searx.tests.engines.test_blekko_images import *  # noqa
4 5
 from searx.tests.engines.test_btdigg import *  # noqa
5 6
 from searx.tests.engines.test_dailymotion import *  # noqa
6 7
 from searx.tests.engines.test_deezer import *  # noqa