Ponysearch/searx/engines/faroo.py

117 lines
3.5 KiB
Python
Raw Permalink Normal View History

2014-10-17 12:56:14 +02:00
## Faroo (Web, News)
#
2014-10-17 12:34:51 +02:00
# @website http://www.faroo.com
# @provide-api yes (http://www.faroo.com/hp/api/api.html), require API-key
#
2014-10-17 12:34:51 +02:00
# @using-api yes
# @results JSON
# @stable yes
# @parse url, title, content, publishedDate, img_src
from urllib import urlencode
from json import loads
import datetime
from searx.utils import searx_useragent
# engine dependent config
categories = ['general', 'news']
paging = True
language_support = True
number_of_results = 10
api_key = None
# search-url
url = 'http://www.faroo.com/'
2014-12-16 17:26:16 +01:00
search_url = url + 'api?{query}'\
'&start={offset}'\
'&length={number_of_results}'\
'&l={language}'\
'&src={categorie}'\
'&i=false'\
'&f=json'\
'&key={api_key}' # noqa
2014-10-17 12:34:51 +02:00
search_category = {'general': 'web',
2014-12-16 17:26:16 +01:00
'news': 'news'}
2014-10-17 12:34:51 +02:00
2014-10-17 12:34:51 +02:00
# do search-request
def request(query, params):
2014-10-17 12:54:22 +02:00
offset = (params['pageno']-1) * number_of_results + 1
2014-10-17 12:34:51 +02:00
categorie = search_category.get(params['category'], 'web')
if params['language'] == 'all':
language = 'en'
else:
language = params['language'].split('_')[0]
# skip, if language is not supported
if language != 'en' and\
language != 'de' and\
language != 'zh':
return params
params['url'] = search_url.format(offset=offset,
number_of_results=number_of_results,
query=urlencode({'q': query}),
language=language,
categorie=categorie,
api_key=api_key)
2014-10-17 12:34:51 +02:00
# using searx User-Agent
params['headers']['User-Agent'] = searx_useragent()
return params
# get response from search-request
def response(resp):
# HTTP-Code 401: api-key is not valide
if resp.status_code == 401:
raise Exception("API key is not valide")
return []
# HTTP-Code 429: rate limit exceeded
if resp.status_code == 429:
raise Exception("rate limit has been exceeded!")
return []
results = []
search_res = loads(resp.text)
# return empty array if there are no results
if not search_res.get('results', {}):
return []
# parse results
for result in search_res['results']:
2014-10-17 12:54:22 +02:00
if result['news']:
2014-12-16 17:26:16 +01:00
# timestamp (milliseconds since 1970)
publishedDate = datetime.datetime.fromtimestamp(result['date']/1000.0) # noqa
2014-10-17 12:34:51 +02:00
# 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']})
2014-10-17 12:54:22 +02:00
# append image result if image url is set
# TODO, show results with an image like in faroo
if result['iurl']:
results.append({'template': 'images.html',
'url': result['url'],
'title': result['title'],
'content': result['kwic'],
2014-10-17 12:54:22 +02:00
'img_src': result['iurl']})
2014-10-17 12:34:51 +02:00
# return results
return results