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
  @website     http://www.faroo.com
4
  @website     http://www.faroo.com
5
  @provide-api yes (http://www.faroo.com/hp/api/api.html), require API-key
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
  @results     JSON
8
  @results     JSON
9
  @stable      yes
9
  @stable      yes
10
  @parse       url, title, content, publishedDate, img_src
10
  @parse       url, title, content, publishedDate, img_src
20
 paging = True
20
 paging = True
21
 language_support = True
21
 language_support = True
22
 number_of_results = 10
22
 number_of_results = 10
23
-api_key = None
24
 
23
 
25
 # search-url
24
 # search-url
26
 url = 'http://www.faroo.com/'
25
 url = 'http://www.faroo.com/'
27
-search_url = url + 'api?{query}'\
26
+search_url = url + 'instant.json?{query}'\
28
                       '&start={offset}'\
27
                       '&start={offset}'\
29
                       '&length={number_of_results}'\
28
                       '&length={number_of_results}'\
30
                       '&l={language}'\
29
                       '&l={language}'\
31
                       '&src={categorie}'\
30
                       '&src={categorie}'\
32
                       '&i=false'\
31
                       '&i=false'\
33
-                      '&f=json'\
34
-                      '&key={api_key}'  # noqa
32
+                      '&c=false'
35
 
33
 
36
 search_category = {'general': 'web',
34
 search_category = {'general': 'web',
37
                    'news': 'news'}
35
                    'news': 'news'}
57
                                       number_of_results=number_of_results,
55
                                       number_of_results=number_of_results,
58
                                       query=urlencode({'q': query}),
56
                                       query=urlencode({'q': query}),
59
                                       language=language,
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
     return params
62
     return params
67
 
63
 
68
 
64
 
69
 # get response from search-request
65
 # get response from search-request
70
 def response(resp):
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
     # HTTP-Code 429: rate limit exceeded
67
     # HTTP-Code 429: rate limit exceeded
76
     if resp.status_code == 429:
68
     if resp.status_code == 429:
77
         raise Exception("rate limit has been exceeded!")
69
         raise Exception("rate limit has been exceeded!")
86
 
78
 
87
     # parse results
79
     # parse results
88
     for result in search_res['results']:
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
         if result['news']:
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
         # append image result if image url is set
89
         # append image result if image url is set
107
-        # TODO, show results with an image like in faroo
108
         if result['iurl']:
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
     # return results
96
     # return results
116
     return results
97
     return results

+ 4
- 5
searx/settings.yml View File

189
     shortcut : et
189
     shortcut : et
190
     disabled : True
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
   - name : 500px
197
   - name : 500px
199
     engine : www500px
198
     engine : www500px

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

40
         response = mock.Mock(text='{"data": []}')
40
         response = mock.Mock(text='{"data": []}')
41
         self.assertEqual(faroo.response(response), [])
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
         response = mock.Mock(text='{"data": []}', status_code=429)
43
         response = mock.Mock(text='{"data": []}', status_code=429)
47
         self.assertRaises(Exception, faroo.response, response)
44
         self.assertRaises(Exception, faroo.response, response)
48
 
45
 
98
         response = mock.Mock(text=json)
95
         response = mock.Mock(text=json)
99
         results = faroo.response(response)
96
         results = faroo.response(response)
100
         self.assertEqual(type(results), list)
97
         self.assertEqual(type(results), list)
101
-        self.assertEqual(len(results), 4)
98
+        self.assertEqual(len(results), 3)
102
         self.assertEqual(results[0]['title'], 'This is the title')
99
         self.assertEqual(results[0]['title'], 'This is the title')
103
         self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
100
         self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
104
         self.assertEqual(results[0]['content'], 'This is the content')
101
         self.assertEqual(results[0]['content'], 'This is the content')
105
         self.assertEqual(results[1]['title'], 'This is the title2')
102
         self.assertEqual(results[1]['title'], 'This is the title2')
106
         self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
103
         self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
107
         self.assertEqual(results[1]['content'], 'This is the content2')
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
         json = """
107
         json = """
111
         {}
108
         {}