[fix] nyaa engine - paging support & filesize (GiB)

BTW: pylint engine

Closes: https://github.com/searxng/searxng/issues/3290
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2024-03-05 17:26:23 +01:00 committed by Markus Heiser
parent 3179993847
commit a5b81e2555
2 changed files with 29 additions and 16 deletions

View file

@ -1,11 +1,18 @@
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
""" # lint: pylint
Nyaa.si (Anime Bittorrent tracker) """Nyaa.si (Anime Bittorrent tracker)
""" """
from lxml import html
from urllib.parse import urlencode from urllib.parse import urlencode
from searx.utils import extract_text, get_torrent_size, int_or_zero
from lxml import html
from searx.utils import (
eval_xpath_getindex,
extract_text,
get_torrent_size,
int_or_zero,
)
# about # about
about = { about = {
@ -23,7 +30,6 @@ paging = True
# search-url # search-url
base_url = 'https://nyaa.si/' base_url = 'https://nyaa.si/'
search_url = base_url + '?page=search&{query}&offset={offset}'
# xpath queries # xpath queries
xpath_results = '//table[contains(@class, "torrent-list")]//tr[not(th)]' xpath_results = '//table[contains(@class, "torrent-list")]//tr[not(th)]'
@ -38,8 +44,14 @@ xpath_downloads = './/td[8]/text()'
# do search-request # do search-request
def request(query, params): def request(query, params):
query = urlencode({'term': query}) args = urlencode(
params['url'] = search_url.format(query=query, offset=params['pageno']) {
'q': query,
'p': params['pageno'],
}
)
params['url'] = base_url + '?' + args #
logger.debug("query_url --> %s", params['url'])
return params return params
@ -56,10 +68,10 @@ def response(resp):
torrent_link = "" torrent_link = ""
# category in which our torrent belongs # category in which our torrent belongs
try:
category = result.xpath(xpath_category)[0].attrib.get('title') category = eval_xpath_getindex(result, xpath_category, 0, '')
except: if category:
pass category = category.attrib.get('title')
# torrent title # torrent title
page_a = result.xpath(xpath_title)[0] page_a = result.xpath(xpath_title)[0]
@ -87,12 +99,12 @@ def response(resp):
downloads = int_or_zero(result.xpath(xpath_downloads)) downloads = int_or_zero(result.xpath(xpath_downloads))
# let's try to calculate the torrent size # let's try to calculate the torrent size
try:
filesize = None
filesize_info = eval_xpath_getindex(result, xpath_filesize, 0, '')
if filesize_info:
filesize_info = result.xpath(xpath_filesize)[0] filesize_info = result.xpath(xpath_filesize)[0]
filesize, filesize_multiplier = filesize_info.split() filesize = get_torrent_size(*filesize_info.split())
filesize = get_torrent_size(filesize, filesize_multiplier)
except:
pass
# content string contains all information not included into template # content string contains all information not included into template
content = 'Category: "{category}". Downloaded {downloads} times.' content = 'Category: "{category}". Downloaded {downloads} times.'

View file

@ -48,6 +48,7 @@ _STORAGE_UNIT_VALUE: Dict[str, int] = {
'GB': 1024 * 1024 * 1024, 'GB': 1024 * 1024 * 1024,
'MB': 1024 * 1024, 'MB': 1024 * 1024,
'TiB': 1000 * 1000 * 1000 * 1000, 'TiB': 1000 * 1000 * 1000 * 1000,
'GiB': 1000 * 1000 * 1000,
'MiB': 1000 * 1000, 'MiB': 1000 * 1000,
'KiB': 1000, 'KiB': 1000,
} }