.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.torznab Namespace Reference

Functions

 init (engine_settings=None)
dict[str, t.Any] request (str query, dict[str, t.Any] params)
list[dict[str, t.Any]] response ("SXNG_Response" resp)
dict[str, t.Any] build_result (etree.Element item)
str|None _map_result_url (str|None guid, str|None comments)
str|None _map_leechers (str|None leechers, str|None seeders, str|None peers)
datetime|None _map_published_date (str|None pubDate)
str|None _map_torrent_file (str|None link, str|None enclosure_url)
str|None _map_magnet_link (str|None magneturl, str|None guid, str|None enclosure_url, str|None link)
str|None get_attribute (etree.Element item, str property_name)
str|None get_torznab_attribute (etree.Element item, str attribute_name)

Variables

dict about
list categories = ['files']
bool paging = False
bool time_range_support = False
str base_url = ''
str api_key = ''
list torznab_categories = []
bool show_torrent_files = False
bool show_magnet_links = True

Detailed Description

Torznab_ is an API specification that provides a standardized way to query
torrent site for content. It is used by a number of torrent applications,
including Prowlarr_ and Jackett_.

Using this engine together with Prowlarr_ or Jackett_ allows you to search
a huge number of torrent sites which are not directly supported.

Configuration
=============

The engine has the following settings:

``base_url``:
  Torznab endpoint URL.

``api_key``:
  The API key to use for authentication.

``torznab_categories``:
  The categories to use for searching. This is a list of category IDs.  See
  Prowlarr-categories_ or Jackett-categories_ for more information.

``show_torrent_files``:
  Whether to show the torrent file in the search results.  Be careful as using
  this with Prowlarr_ or Jackett_ leaks the API key.  This should be used only
  if you are querying a Torznab endpoint without authentication or if the
  instance is private.  Be aware that private trackers may ban you if you share
  the torrent file.  Defaults to ``false``.

``show_magnet_links``:
  Whether to show the magnet link in the search results.  Be aware that private
  trackers may ban you if you share the magnet link.  Defaults to ``true``.

.. _Torznab:
   https://torznab.github.io/spec-1.3-draft/index.html
.. _Prowlarr:
   https://github.com/Prowlarr/Prowlarr
.. _Jackett:
   https://github.com/Jackett/Jackett
.. _Prowlarr-categories:
   https://wiki.servarr.com/en/prowlarr/cardigann-yml-definition#categories
.. _Jackett-categories:
   https://github.com/Jackett/Jackett/wiki/Jackett-Categories

Implementations
===============

Function Documentation

◆ _map_leechers()

str | None searx.engines.torznab._map_leechers ( str | None leechers,
str | None seeders,
str | None peers )
protected

Definition at line 179 of file torznab.py.

179def _map_leechers(leechers: str | None, seeders: str | None, peers: str | None) -> str | None:
180 if leechers:
181 return leechers
182 if seeders and peers:
183 return str(int(peers) - int(seeders))
184 return None
185
186

Referenced by build_result().

Here is the caller graph for this function:

◆ _map_magnet_link()

str | None searx.engines.torznab._map_magnet_link ( str | None magneturl,
str | None guid,
str | None enclosure_url,
str | None link )
protected

Definition at line 204 of file torznab.py.

209) -> str | None:
210 if magneturl and magneturl.startswith('magnet'):
211 return magneturl
212 if guid and guid.startswith('magnet'):
213 return guid
214 if enclosure_url and enclosure_url.startswith('magnet'):
215 return enclosure_url
216 if link and link.startswith('magnet'):
217 return link
218 return None
219
220

Referenced by build_result().

Here is the caller graph for this function:

◆ _map_published_date()

datetime | None searx.engines.torznab._map_published_date ( str | None pubDate)
protected

Definition at line 187 of file torznab.py.

187def _map_published_date(pubDate: str | None) -> datetime | None:
188 if pubDate is not None:
189 try:
190 return datetime.strptime(pubDate, '%a, %d %b %Y %H:%M:%S %z')
191 except (ValueError, TypeError) as e:
192 logger.debug("ignore exception (publishedDate): %s", e)
193 return None
194
195

Referenced by build_result().

Here is the caller graph for this function:

◆ _map_result_url()

str | None searx.engines.torznab._map_result_url ( str | None guid,
str | None comments )
protected

Definition at line 171 of file torznab.py.

171def _map_result_url(guid: str | None, comments: str | None) -> str | None:
172 if guid and guid.startswith('http'):
173 return guid
174 if comments and comments.startswith('http'):
175 return comments
176 return None
177
178

Referenced by build_result().

Here is the caller graph for this function:

◆ _map_torrent_file()

str | None searx.engines.torznab._map_torrent_file ( str | None link,
str | None enclosure_url )
protected

Definition at line 196 of file torznab.py.

196def _map_torrent_file(link: str | None, enclosure_url: str | None) -> str | None:
197 if link and link.startswith('http'):
198 return link
199 if enclosure_url and enclosure_url.startswith('http'):
200 return enclosure_url
201 return None
202
203

Referenced by build_result().

Here is the caller graph for this function:

◆ build_result()

dict[str, t.Any] searx.engines.torznab.build_result ( etree.Element item)
Build a result from a XML item.

Definition at line 127 of file torznab.py.

127def build_result(item: etree.Element) -> dict[str, t.Any]:
128 """Build a result from a XML item."""
129
130 # extract attributes from XML
131 # see https://torznab.github.io/spec-1.3-draft/torznab/Specification-v1.3.html#predefined-attributes
132 enclosure: etree.Element | None = item.find('enclosure')
133 enclosure_url: str | None = None
134 if enclosure is not None:
135 enclosure_url = enclosure.get('url')
136
137 filesize = get_attribute(item, 'size')
138 if not filesize and enclosure:
139 filesize = enclosure.get('length')
140
141 guid = get_attribute(item, 'guid')
142 comments = get_attribute(item, 'comments')
143 pubDate = get_attribute(item, 'pubDate')
144 seeders = get_torznab_attribute(item, 'seeders')
145 leechers = get_torznab_attribute(item, 'leechers')
146 peers = get_torznab_attribute(item, 'peers')
147
148 # map attributes to SearXNG result
149 result: dict[str, t.Any] = {
150 'template': 'torrent.html',
151 'title': get_attribute(item, 'title'),
152 'filesize': humanize_bytes(int(filesize)) if filesize else None,
153 'files': get_attribute(item, 'files'),
154 'seed': seeders,
155 'leech': _map_leechers(leechers, seeders, peers),
156 'url': _map_result_url(guid, comments),
157 'publishedDate': _map_published_date(pubDate),
158 'torrentfile': None,
159 'magnetlink': None,
160 }
161
162 link = get_attribute(item, 'link')
163 if show_torrent_files:
164 result['torrentfile'] = _map_torrent_file(link, enclosure_url)
165 if show_magnet_links:
166 magneturl = get_torznab_attribute(item, 'magneturl')
167 result['magnetlink'] = _map_magnet_link(magneturl, guid, enclosure_url, link)
168 return result
169
170

References _map_leechers(), _map_magnet_link(), _map_published_date(), _map_result_url(), _map_torrent_file(), get_attribute(), and get_torznab_attribute().

Referenced by response().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_attribute()

str | None searx.engines.torznab.get_attribute ( etree.Element item,
str property_name )
Get attribute from item.

Definition at line 221 of file torznab.py.

221def get_attribute(item: etree.Element, property_name: str) -> str | None:
222 """Get attribute from item."""
223 property_element: etree.Element | None = item.find(property_name)
224 if property_element is not None:
225 return property_element.text
226 return None
227
228

Referenced by build_result().

Here is the caller graph for this function:

◆ get_torznab_attribute()

str | None searx.engines.torznab.get_torznab_attribute ( etree.Element item,
str attribute_name )
Get torznab special attribute from item.

Definition at line 229 of file torznab.py.

229def get_torznab_attribute(item: etree.Element, attribute_name: str) -> str | None:
230 """Get torznab special attribute from item."""
231 element: etree.Element | None = item.find(
232 './/torznab:attr[@name="{attribute_name}"]'.format(attribute_name=attribute_name),
233 {'torznab': 'http://torznab.com/schemas/2015/feed'},
234 )
235 if element is not None:
236 return element.get("value")
237 return None

Referenced by build_result().

Here is the caller graph for this function:

◆ init()

searx.engines.torznab.init ( engine_settings = None)
Initialize the engine.

Definition at line 86 of file torznab.py.

86def init(engine_settings=None): # pylint: disable=unused-argument
87 """Initialize the engine."""
88 if len(base_url) < 1:
89 raise ValueError('missing torznab base_url')
90
91

◆ request()

dict[str, t.Any] searx.engines.torznab.request ( str query,
dict[str, t.Any] params )
Build the request params.

Definition at line 92 of file torznab.py.

92def request(query: str, params: dict[str, t.Any]) -> dict[str, t.Any]:
93 """Build the request params."""
94 search_url: str = base_url + '?t=search&q={search_query}'
95
96 if len(api_key) > 0:
97 search_url += '&apikey={api_key}'
98 if len(torznab_categories) > 0:
99 search_url += '&cat={torznab_categories}'
100
101 params['url'] = search_url.format(
102 search_query=quote(query), api_key=api_key, torznab_categories=",".join([str(x) for x in torznab_categories])
103 )
104
105 return params
106
107

◆ response()

list[dict[str, t.Any]] searx.engines.torznab.response ( "SXNG_Response" resp)
Parse the XML response and return a list of results.

Definition at line 108 of file torznab.py.

108def response(resp: "SXNG_Response") -> list[dict[str, t.Any]]:
109 """Parse the XML response and return a list of results."""
110 results = []
111 search_results = etree.XML(resp.content)
112
113 # handle errors: https://newznab.readthedocs.io/en/latest/misc/api/#newznab-error-codes
114 if search_results.tag == "error":
115 raise SearxEngineAPIException(search_results.get("description"))
116
117 channel: etree.Element = search_results[0]
118
119 item: etree.Element
120 for item in channel.iterfind('item'):
121 result: dict[str, t.Any] = build_result(item)
122 results.append(result)
123
124 return results
125
126

References build_result().

Here is the call graph for this function:

Variable Documentation

◆ about

dict searx.engines.torznab.about
Initial value:
1= {
2 "website": None,
3 "wikidata_id": None,
4 "official_api_documentation": "https://torznab.github.io/spec-1.3-draft",
5 "use_official_api": True,
6 "require_api_key": False,
7 "results": 'XML',
8}

Definition at line 64 of file torznab.py.

◆ api_key

str searx.engines.torznab.api_key = ''

Definition at line 79 of file torznab.py.

◆ base_url

str searx.engines.torznab.base_url = ''

Definition at line 78 of file torznab.py.

◆ categories

list searx.engines.torznab.categories = ['files']

Definition at line 72 of file torznab.py.

◆ paging

bool searx.engines.torznab.paging = False

Definition at line 73 of file torznab.py.

◆ show_magnet_links

bool searx.engines.torznab.show_magnet_links = True

Definition at line 83 of file torznab.py.

◆ show_torrent_files

bool searx.engines.torznab.show_torrent_files = False

Definition at line 82 of file torznab.py.

◆ time_range_support

bool searx.engines.torznab.time_range_support = False

Definition at line 74 of file torznab.py.

◆ torznab_categories

list searx.engines.torznab.torznab_categories = []

Definition at line 81 of file torznab.py.