1
0
mirror of https://github.com/searx/searx synced 2024-12-11 11:40:36 +01:00

[fix] faroo json api and image layout fixed

This commit is contained in:
misnyo 2017-08-30 18:53:00 +02:00 committed by Adam Tauber
parent 489bf0f6fd
commit 0607b167f8
3 changed files with 21 additions and 44 deletions

View File

@ -4,7 +4,7 @@
@website http://www.faroo.com @website http://www.faroo.com
@provide-api yes (http://www.faroo.com/hp/api/api.html), require API-key @provide-api yes (http://www.faroo.com/hp/api/api.html), require API-key
@using-api yes @using-api no
@results JSON @results JSON
@stable yes @stable yes
@parse url, title, content, publishedDate, img_src @parse url, title, content, publishedDate, img_src
@ -20,18 +20,16 @@ categories = ['general', 'news']
paging = True paging = True
language_support = True language_support = True
number_of_results = 10 number_of_results = 10
api_key = None
# search-url # search-url
url = 'http://www.faroo.com/' url = 'http://www.faroo.com/'
search_url = url + 'api?{query}'\ search_url = url + 'instant.json?{query}'\
'&start={offset}'\ '&start={offset}'\
'&length={number_of_results}'\ '&length={number_of_results}'\
'&l={language}'\ '&l={language}'\
'&src={categorie}'\ '&src={categorie}'\
'&i=false'\ '&i=false'\
'&f=json'\ '&c=false'
'&key={api_key}' # noqa
search_category = {'general': 'web', search_category = {'general': 'web',
'news': 'news'} 'news': 'news'}
@ -57,21 +55,15 @@ def request(query, params):
number_of_results=number_of_results, number_of_results=number_of_results,
query=urlencode({'q': query}), query=urlencode({'q': query}),
language=language, language=language,
categorie=categorie, categorie=categorie)
api_key=api_key)
# using searx User-Agent params['headers']['Referer'] = url
params['headers']['User-Agent'] = searx_useragent()
return params return params
# get response from search-request # get response from search-request
def response(resp): def response(resp):
# HTTP-Code 401: api-key is not valide
if resp.status_code == 401:
raise Exception("API key is not valide")
# HTTP-Code 429: rate limit exceeded # HTTP-Code 429: rate limit exceeded
if resp.status_code == 429: if resp.status_code == 429:
raise Exception("rate limit has been exceeded!") raise Exception("rate limit has been exceeded!")
@ -86,31 +78,20 @@ def response(resp):
# parse results # parse results
for result in search_res['results']: for result in search_res['results']:
publishedDate = None
result_json = {'url': result['url'],
'title': result['title'],
'content': result['kwic']}
if result['news']: if result['news']:
# timestamp (milliseconds since 1970) result_json['publishedDate'] = \
publishedDate = datetime.datetime.fromtimestamp(result['date'] / 1000.0) # noqa datetime.datetime.fromtimestamp(result['date'] / 1000.0)
# append news result
results.append({'url': result['url'],
'title': result['title'],
'publishedDate': publishedDate,
'content': result['kwic']})
else:
# append general result
# TODO, publishedDate correct?
results.append({'url': result['url'],
'title': result['title'],
'content': result['kwic']})
# append image result if image url is set # append image result if image url is set
# TODO, show results with an image like in faroo
if result['iurl']: if result['iurl']:
results.append({'template': 'images.html', result_json['template'] ='videos.html'
'url': result['url'], result_json['thumbnail'] = result['iurl']
'title': result['title'],
'content': result['kwic'], results.append(result_json)
'img_src': result['iurl']})
# return results # return results
return results return results

View File

@ -189,11 +189,10 @@ engines:
shortcut : et shortcut : et
disabled : True disabled : True
# api-key required: http://www.faroo.com/hp/api/api.html#key - name : faroo
# - name : faroo engine : faroo
# engine : faroo shortcut : fa
# shortcut : fa disabled : True
# api_key : 'apikey' # required!
- name : 500px - name : 500px
engine : www500px engine : www500px

View File

@ -40,9 +40,6 @@ class TestFarooEngine(SearxTestCase):
response = mock.Mock(text='{"data": []}') response = mock.Mock(text='{"data": []}')
self.assertEqual(faroo.response(response), []) self.assertEqual(faroo.response(response), [])
response = mock.Mock(text='{"data": []}', status_code=401)
self.assertRaises(Exception, faroo.response, response)
response = mock.Mock(text='{"data": []}', status_code=429) response = mock.Mock(text='{"data": []}', status_code=429)
self.assertRaises(Exception, faroo.response, response) self.assertRaises(Exception, faroo.response, response)
@ -98,14 +95,14 @@ class TestFarooEngine(SearxTestCase):
response = mock.Mock(text=json) response = mock.Mock(text=json)
results = faroo.response(response) results = faroo.response(response)
self.assertEqual(type(results), list) self.assertEqual(type(results), list)
self.assertEqual(len(results), 4) self.assertEqual(len(results), 3)
self.assertEqual(results[0]['title'], 'This is the title') self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], 'http://this.is.the.url/') self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
self.assertEqual(results[0]['content'], 'This is the content') self.assertEqual(results[0]['content'], 'This is the content')
self.assertEqual(results[1]['title'], 'This is the title2') self.assertEqual(results[1]['title'], 'This is the title2')
self.assertEqual(results[1]['url'], 'http://this.is.the.url2/') self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
self.assertEqual(results[1]['content'], 'This is the content2') self.assertEqual(results[1]['content'], 'This is the content2')
self.assertEqual(results[3]['img_src'], 'http://upload.wikimedia.org/optimized.jpg') self.assertEqual(results[2]['thumbnail'], 'http://upload.wikimedia.org/optimized.jpg')
json = """ json = """
{} {}