1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-18 02:10:12 +01:00
searxng/searx/engines/solidtorrents.py

81 lines
2.5 KiB
Python
Raw Normal View History

2021-03-07 18:14:30 +01:00
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""SolidTorrents
2021-03-07 18:14:30 +01:00
"""
from datetime import datetime
2021-03-07 18:14:30 +01:00
from urllib.parse import urlencode
import random
from lxml import html
from searx.utils import (
extract_text,
eval_xpath,
eval_xpath_getindex,
eval_xpath_list,
get_torrent_size,
)
2021-03-07 18:14:30 +01:00
about = {
2023-09-14 09:33:46 +02:00
"website": 'https://www.solidtorrents.to/',
2021-03-07 18:14:30 +01:00
"wikidata_id": None,
"official_api_documentation": None,
"use_official_api": False,
2021-03-07 18:14:30 +01:00
"require_api_key": False,
"results": 'HTML',
2021-03-07 18:14:30 +01:00
}
categories = ['files']
paging = True
# base_url can be overwritten by a list of URLs in the settings.yml
2023-09-14 09:33:46 +02:00
base_url = 'https://solidtorrents.to'
2021-03-07 18:14:30 +01:00
def request(query, params):
if isinstance(base_url, list):
params['base_url'] = random.choice(base_url)
else:
params['base_url'] = base_url
search_url = params['base_url'] + '/search?{query}'
2023-09-14 09:33:05 +02:00
query = urlencode({'q': query, 'page': params['pageno']})
2021-03-07 18:14:30 +01:00
params['url'] = search_url.format(query=query)
return params
def response(resp):
results = []
dom = html.fromstring(resp.text)
2023-09-14 09:32:17 +02:00
for result in eval_xpath(dom, '//li[contains(@class, "search-result")]'):
torrentfile = eval_xpath_getindex(result, './/a[contains(@class, "dl-torrent")]/@href', 0, None)
magnet = eval_xpath_getindex(result, './/a[contains(@class, "dl-magnet")]/@href', 0, None)
if torrentfile is None or magnet is None:
continue # ignore anime results that which aren't actually torrents
title = eval_xpath_getindex(result, './/h5[contains(@class, "title")]', 0, None)
url = eval_xpath_getindex(result, './/h5[contains(@class, "title")]/a/@href', 0, None)
categ = eval_xpath(result, './/a[contains(@class, "category")]')
stats = eval_xpath_list(result, './/div[contains(@class, "stats")]/div', min_len=5)
params = {
2023-09-14 09:32:17 +02:00
'seed': extract_text(stats[3]),
'leech': extract_text(stats[2]),
'title': extract_text(title),
'url': resp.search_params['base_url'] + url,
2023-09-14 09:32:17 +02:00
'filesize': get_torrent_size(*extract_text(stats[1]).split()),
'magnetlink': magnet,
'torrentfile': torrentfile,
2023-09-14 09:32:17 +02:00
'metadata': extract_text(categ),
'template': "torrent.html",
}
try:
2023-09-14 09:32:17 +02:00
params['publishedDate'] = datetime.strptime(extract_text(stats[4]), '%b %d, %Y')
except ValueError:
pass
results.append(params)
2021-03-07 18:14:30 +01:00
return results