Ponysearch/searx/engines/btdigg.py

86 lines
2.3 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
BTDigg (Videos, Music, Files)
"""
2015-01-21 18:02:29 +01:00
from urllib.parse import quote, urljoin
from lxml import html
from searx.utils import extract_text
2015-01-21 18:02:29 +01:00
# about
about = {
"website": 'https://btdig.com',
"wikidata_id": 'Q4836698',
"official_api_documentation": {'url': 'https://btdig.com/contacts', 'comment': 'on demand'},
"use_official_api": False,
"require_api_key": False,
"results": 'HTML',
}
2015-01-21 18:02:29 +01:00
# engine dependent config
categories = ['files']
2015-01-21 18:02:29 +01:00
paging = True
# search-url
url = 'https://btdig.com'
2015-01-25 10:21:44 +01:00
search_url = url + '/search?q={search_term}&p={pageno}'
2015-01-21 18:02:29 +01:00
# do search-request
def request(query, params):
params['url'] = search_url.format(search_term=quote(query), pageno=params['pageno'] - 1)
2015-01-21 18:02:29 +01:00
return params
# get response from search-request
def response(resp):
results = []
2016-11-30 18:43:03 +01:00
dom = html.fromstring(resp.text)
2015-01-21 18:02:29 +01:00
search_res = dom.xpath('//div[@class="one_result"]')
2015-01-21 18:02:29 +01:00
# return empty array if nothing is found
if not search_res:
return []
# parse results
for result in search_res:
link = result.xpath('.//div[@class="torrent_name"]//a')[0]
2015-01-30 19:52:44 +01:00
href = urljoin(url, link.attrib.get('href'))
title = extract_text(link)
2015-01-21 18:02:29 +01:00
excerpt = result.xpath('.//div[@class="torrent_excerpt"]')[0]
content = html.tostring(excerpt, encoding='unicode', method='text', with_tail=False)
content = content.strip().replace('\n', ' | ')
content = ' '.join(content.split())
2015-01-21 18:02:29 +01:00
filesize = result.xpath('.//span[@class="torrent_size"]/text()')[0]
files = (result.xpath('.//span[@class="torrent_files"]/text()') or ['1'])[0]
2015-01-21 18:02:29 +01:00
# convert files to int if possible
try:
2015-01-21 18:02:29 +01:00
files = int(files)
except: # pylint: disable=bare-except
2015-01-21 18:02:29 +01:00
files = None
magnetlink = result.xpath('.//div[@class="torrent_magnet"]//a')[0].attrib['href']
2015-01-21 18:02:29 +01:00
# append result
results.append(
{
'url': href,
'title': title,
'content': content,
'filesize': filesize,
'files': files,
'magnetlink': magnetlink,
'template': 'torrent.html',
}
)
2015-01-21 18:02:29 +01:00
# return results sorted by seeder
return results