.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
nyaa.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Nyaa.si (Anime Bittorrent tracker)
3
4"""
5
6from urllib.parse import urlencode
7
8from lxml import html
9from searx.utils import (
10 eval_xpath_getindex,
11 extract_text,
12 int_or_zero,
13)
14
15# about
16about = {
17 "website": 'https://nyaa.si/',
18 "wikidata_id": None,
19 "official_api_documentation": None,
20 "use_official_api": False,
21 "require_api_key": False,
22 "results": 'HTML',
23}
24
25# engine dependent config
26categories = ['files']
27paging = True
28
29# search-url
30base_url = 'https://nyaa.si/'
31
32# xpath queries
33xpath_results = '//table[contains(@class, "torrent-list")]//tr[not(th)]'
34xpath_category = './/td[1]/a[1]'
35xpath_title = './/td[2]/a[last()]'
36xpath_torrent_links = './/td[3]/a'
37xpath_filesize = './/td[4]/text()'
38xpath_seeds = './/td[6]/text()'
39xpath_leeches = './/td[7]/text()'
40xpath_downloads = './/td[8]/text()'
41
42
43# do search-request
44def request(query, params):
45 args = urlencode(
46 {
47 'q': query,
48 'p': params['pageno'],
49 }
50 )
51 params['url'] = base_url + '?' + args #
52 logger.debug("query_url --> %s", params['url'])
53 return params
54
55
56# get response from search-request
57def response(resp):
58 results = []
59
60 dom = html.fromstring(resp.text)
61
62 for result in dom.xpath(xpath_results):
63 # defaults
64 filesize = 0
65 magnet_link = ""
66 torrent_link = ""
67
68 # category in which our torrent belongs
69
70 category = eval_xpath_getindex(result, xpath_category, 0, '')
71 if category:
72 category = category.attrib.get('title')
73
74 # torrent title
75 page_a = result.xpath(xpath_title)[0]
76 title = extract_text(page_a)
77
78 # link to the page
79 href = base_url + page_a.attrib.get('href')
80
81 for link in result.xpath(xpath_torrent_links):
82 url = link.attrib.get('href')
83 if 'magnet' in url:
84 # link to the magnet
85 magnet_link = url
86 else:
87 # link to the torrent file
88 torrent_link = url
89
90 # seed count
91 seed = int_or_zero(result.xpath(xpath_seeds))
92
93 # leech count
94 leech = int_or_zero(result.xpath(xpath_leeches))
95
96 # torrent downloads count
97 downloads = int_or_zero(result.xpath(xpath_downloads))
98
99 # let's try to calculate the torrent size
100
101 filesize = eval_xpath_getindex(result, xpath_filesize, 0, '')
102
103 # content string contains all information not included into template
104 content = 'Category: "{category}". Downloaded {downloads} times.'
105 content = content.format(category=category, downloads=downloads)
106
107 results.append(
108 {
109 'url': href,
110 'title': title,
111 'content': content,
112 'seed': seed,
113 'leech': leech,
114 'filesize': filesize,
115 'torrentfile': torrent_link,
116 'magnetlink': magnet_link,
117 'template': 'torrent.html',
118 }
119 )
120
121 return results
request(query, params)
Definition nyaa.py:44
response(resp)
Definition nyaa.py:57