1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/yacy.py

95 lines
2.8 KiB
Python
Raw Normal View History

2014-12-01 12:26:38 +01:00
## Yacy (Web, Images, Videos, Music, Files)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @website http://yacy.net
2014-12-13 13:56:02 +01:00
# @provide-api yes
# (http://www.yacy-websuche.de/wiki/index.php/Dev:APIyacysearch)
#
2014-12-01 12:26:38 +01:00
# @using-api yes
# @results JSON
# @stable yes
# @parse (general) url, title, content, publishedDate
# @parse (images) url, title, img_src
#
# @todo parse video, audio and file results
2014-05-10 11:56:19 +02:00
from json import loads
from urllib import urlencode
2014-12-01 12:26:38 +01:00
from dateutil import parser
# engine dependent config
2014-12-13 13:56:02 +01:00
categories = ['general', 'images'] # TODO , 'music', 'videos', 'files'
2014-12-01 12:26:38 +01:00
paging = True
language_support = True
number_of_results = 5
# search-url
base_url = 'http://localhost:8090'
2015-01-13 16:40:30 +01:00
search_url = '/yacysearch.json?{query}'\
'&startRecord={offset}'\
'&maximumRecords={limit}'\
'&contentdom={search_type}'\
'&resource=global' # noqa
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# yacy specific type-definitions
search_types = {'general': 'text',
'images': 'image',
2014-12-13 13:56:02 +01:00
'files': 'app',
2014-12-01 12:26:38 +01:00
'music': 'audio',
'videos': 'video'}
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
2014-12-01 12:26:38 +01:00
offset = (params['pageno'] - 1) * number_of_results
search_type = search_types.get(params['category'], '0')
2015-01-13 16:40:30 +01:00
params['url'] = base_url +\
search_url.format(query=urlencode({'query': query}),
offset=offset,
limit=number_of_results,
search_type=search_type)
2014-12-01 12:26:38 +01:00
# add language tag if specified
if params['language'] != 'all':
params['url'] += '&lr=lang_' + params['language'].split('_')[0]
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
2014-12-01 12:26:38 +01:00
results = []
2014-05-10 11:56:19 +02:00
raw_search_results = loads(resp.text)
2014-12-01 12:26:38 +01:00
# return empty array if there are no results
2014-05-10 11:56:19 +02:00
if not raw_search_results:
return []
search_results = raw_search_results.get('channels', {})[0].get('items', [])
2015-02-09 13:30:16 +01:00
for result in search_results:
# parse image results
if result.get('image'):
# append result
results.append({'url': result['url'],
'title': result['title'],
'content': '',
'img_src': result['image'],
'template': 'images.html'})
2014-12-01 12:26:38 +01:00
# parse general results
2015-02-09 13:30:16 +01:00
else:
2014-12-01 12:26:38 +01:00
publishedDate = parser.parse(result['pubDate'])
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# append result
results.append({'url': result['link'],
2015-01-13 16:40:30 +01:00
'title': result['title'],
'content': result['description'],
'publishedDate': publishedDate})
2014-05-10 11:56:19 +02:00
2015-02-09 13:30:16 +01:00
#TODO parse video, audio and file results
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results