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