Browse Source

[fix] faroo json api and image layout fixed

misnyo 7 years ago
parent
commit
0607b167f8
3 changed files with 21 additions and 44 deletions
  1. 15
    34
      searx/engines/faroo.py
  2. 4
    5
      searx/settings.yml
  3. 2
    5
      tests/unit/engines/test_faroo.py

+ 15
- 34
searx/engines/faroo.py View File

@@ -4,7 +4,7 @@
4 4
  @website     http://www.faroo.com
5 5
  @provide-api yes (http://www.faroo.com/hp/api/api.html), require API-key
6 6
 
7
- @using-api   yes
7
+ @using-api   no
8 8
  @results     JSON
9 9
  @stable      yes
10 10
  @parse       url, title, content, publishedDate, img_src
@@ -20,18 +20,16 @@ categories = ['general', 'news']
20 20
 paging = True
21 21
 language_support = True
22 22
 number_of_results = 10
23
-api_key = None
24 23
 
25 24
 # search-url
26 25
 url = 'http://www.faroo.com/'
27
-search_url = url + 'api?{query}'\
26
+search_url = url + 'instant.json?{query}'\
28 27
                       '&start={offset}'\
29 28
                       '&length={number_of_results}'\
30 29
                       '&l={language}'\
31 30
                       '&src={categorie}'\
32 31
                       '&i=false'\
33
-                      '&f=json'\
34
-                      '&key={api_key}'  # noqa
32
+                      '&c=false'
35 33
 
36 34
 search_category = {'general': 'web',
37 35
                    'news': 'news'}
@@ -57,21 +55,15 @@ def request(query, params):
57 55
                                       number_of_results=number_of_results,
58 56
                                       query=urlencode({'q': query}),
59 57
                                       language=language,
60
-                                      categorie=categorie,
61
-                                      api_key=api_key)
58
+                                      categorie=categorie)
62 59
 
63
-    # using searx User-Agent
64
-    params['headers']['User-Agent'] = searx_useragent()
60
+    params['headers']['Referer'] = url
65 61
 
66 62
     return params
67 63
 
68 64
 
69 65
 # get response from search-request
70 66
 def response(resp):
71
-    # HTTP-Code 401: api-key is not valide
72
-    if resp.status_code == 401:
73
-        raise Exception("API key is not valide")
74
-
75 67
     # HTTP-Code 429: rate limit exceeded
76 68
     if resp.status_code == 429:
77 69
         raise Exception("rate limit has been exceeded!")
@@ -86,31 +78,20 @@ def response(resp):
86 78
 
87 79
     # parse results
88 80
     for result in search_res['results']:
81
+        publishedDate = None
82
+        result_json = {'url': result['url'],
83
+                        'title': result['title'],
84
+                        'content': result['kwic']}
89 85
         if result['news']:
90
-            # timestamp (milliseconds since 1970)
91
-            publishedDate = datetime.datetime.fromtimestamp(result['date'] / 1000.0)  # noqa
92
-
93
-            # append news result
94
-            results.append({'url': result['url'],
95
-                            'title': result['title'],
96
-                            'publishedDate': publishedDate,
97
-                            'content': result['kwic']})
98
-
99
-        else:
100
-            # append general result
101
-            # TODO, publishedDate correct?
102
-            results.append({'url': result['url'],
103
-                            'title': result['title'],
104
-                            'content': result['kwic']})
86
+            result_json['publishedDate'] = \
87
+                    datetime.datetime.fromtimestamp(result['date'] / 1000.0)
105 88
 
106 89
         # append image result if image url is set
107
-        # TODO, show results with an image like in faroo
108 90
         if result['iurl']:
109
-            results.append({'template': 'images.html',
110
-                            'url': result['url'],
111
-                            'title': result['title'],
112
-                            'content': result['kwic'],
113
-                            'img_src': result['iurl']})
91
+            result_json['template'] ='videos.html'
92
+            result_json['thumbnail'] = result['iurl']
93
+
94
+        results.append(result_json)
114 95
 
115 96
     # return results
116 97
     return results

+ 4
- 5
searx/settings.yml View File

@@ -189,11 +189,10 @@ engines:
189 189
     shortcut : et
190 190
     disabled : True
191 191
 
192
-# api-key required: http://www.faroo.com/hp/api/api.html#key
193
-#  - name : faroo
194
-#    engine : faroo
195
-#    shortcut : fa
196
-#    api_key : 'apikey' # required!
192
+  - name : faroo
193
+    engine : faroo
194
+    shortcut : fa
195
+    disabled : True
197 196
 
198 197
   - name : 500px
199 198
     engine : www500px

+ 2
- 5
tests/unit/engines/test_faroo.py View File

@@ -40,9 +40,6 @@ class TestFarooEngine(SearxTestCase):
40 40
         response = mock.Mock(text='{"data": []}')
41 41
         self.assertEqual(faroo.response(response), [])
42 42
 
43
-        response = mock.Mock(text='{"data": []}', status_code=401)
44
-        self.assertRaises(Exception, faroo.response, response)
45
-
46 43
         response = mock.Mock(text='{"data": []}', status_code=429)
47 44
         self.assertRaises(Exception, faroo.response, response)
48 45
 
@@ -98,14 +95,14 @@ class TestFarooEngine(SearxTestCase):
98 95
         response = mock.Mock(text=json)
99 96
         results = faroo.response(response)
100 97
         self.assertEqual(type(results), list)
101
-        self.assertEqual(len(results), 4)
98
+        self.assertEqual(len(results), 3)
102 99
         self.assertEqual(results[0]['title'], 'This is the title')
103 100
         self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
104 101
         self.assertEqual(results[0]['content'], 'This is the content')
105 102
         self.assertEqual(results[1]['title'], 'This is the title2')
106 103
         self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
107 104
         self.assertEqual(results[1]['content'], 'This is the content2')
108
-        self.assertEqual(results[3]['img_src'], 'http://upload.wikimedia.org/optimized.jpg')
105
+        self.assertEqual(results[2]['thumbnail'], 'http://upload.wikimedia.org/optimized.jpg')
109 106
 
110 107
         json = """
111 108
         {}