.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
btdigg.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3 BTDigg (Videos, Music, Files)
4"""
5
6from urllib.parse import quote, urljoin
7
8from lxml import html
9from searx.utils import extract_text
10
11# about
12about = {
13 "website": 'https://btdig.com',
14 "wikidata_id": 'Q4836698',
15 "official_api_documentation": {'url': 'https://btdig.com/contacts', 'comment': 'on demand'},
16 "use_official_api": False,
17 "require_api_key": False,
18 "results": 'HTML',
19}
20
21# engine dependent config
22categories = ['files']
23paging = True
24
25# search-url
26url = 'https://btdig.com'
27search_url = url + '/search?q={search_term}&p={pageno}'
28
29
30# do search-request
31def request(query, params):
32 params['url'] = search_url.format(search_term=quote(query), pageno=params['pageno'] - 1)
33
34 return params
35
36
37# get response from search-request
38def response(resp):
39 results = []
40
41 dom = html.fromstring(resp.text)
42
43 search_res = dom.xpath('//div[@class="one_result"]')
44
45 # return empty array if nothing is found
46 if not search_res:
47 return []
48
49 # parse results
50 for result in search_res:
51 link = result.xpath('.//div[@class="torrent_name"]//a')[0]
52 href = urljoin(url, link.attrib.get('href'))
53 title = extract_text(link)
54
55 excerpt = result.xpath('.//div[@class="torrent_excerpt"]')[0]
56 content = html.tostring(excerpt, encoding='unicode', method='text', with_tail=False)
57 content = content.strip().replace('\n', ' | ')
58 content = ' '.join(content.split())
59
60 filesize = result.xpath('.//span[@class="torrent_size"]/text()')[0]
61 files = (result.xpath('.//span[@class="torrent_files"]/text()') or ['1'])[0]
62
63 # convert files to int if possible
64 try:
65 files = int(files)
66 except: # pylint: disable=bare-except
67 files = None
68
69 magnetlink = result.xpath('.//div[@class="torrent_magnet"]//a')[0].attrib['href']
70
71 # append result
72 results.append(
73 {
74 'url': href,
75 'title': title,
76 'content': content,
77 'filesize': filesize,
78 'files': files,
79 'magnetlink': magnetlink,
80 'template': 'torrent.html',
81 }
82 )
83
84 # return results sorted by seeder
85 return results
request(query, params)
Definition btdigg.py:31