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

Functions

 init (engine_settings=None)
 
Dict[str, Any] request (str query, Dict[str, Any] params)
 
List[Dict[str, Any]] response (httpx.Response resp)
 
Dict[str, 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

logging logger .Logger
 
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 183 of file torznab.py.

183def _map_leechers(leechers: str | None, seeders: str | None, peers: str | None) -> str | None:
184 if leechers:
185 return leechers
186 if seeders and peers:
187 return str(int(peers) - int(seeders))
188 return None
189
190

Referenced by searx.engines.torznab.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 208 of file torznab.py.

213) -> str | None:
214 if magneturl and magneturl.startswith('magnet'):
215 return magneturl
216 if guid and guid.startswith('magnet'):
217 return guid
218 if enclosure_url and enclosure_url.startswith('magnet'):
219 return enclosure_url
220 if link and link.startswith('magnet'):
221 return link
222 return None
223
224

Referenced by searx.engines.torznab.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 191 of file torznab.py.

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

Referenced by searx.engines.torznab.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 175 of file torznab.py.

175def _map_result_url(guid: str | None, comments: str | None) -> str | None:
176 if guid and guid.startswith('http'):
177 return guid
178 if comments and comments.startswith('http'):
179 return comments
180 return None
181
182

Referenced by searx.engines.torznab.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 200 of file torznab.py.

200def _map_torrent_file(link: str | None, enclosure_url: str | None) -> str | None:
201 if link and link.startswith('http'):
202 return link
203 if enclosure_url and enclosure_url.startswith('http'):
204 return enclosure_url
205 return None
206
207

Referenced by searx.engines.torznab.build_result().

+ Here is the caller graph for this function:

◆ build_result()

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

Definition at line 131 of file torznab.py.

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

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

Referenced by searx.engines.torznab.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 225 of file torznab.py.

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

Referenced by searx.engines.torznab.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 233 of file torznab.py.

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

References searx.format.

Referenced by searx.engines.torznab.build_result().

+ Here is the caller graph for this function:

◆ init()

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

Definition at line 90 of file torznab.py.

90def init(engine_settings=None): # pylint: disable=unused-argument
91 """Initialize the engine."""
92 if len(base_url) < 1:
93 raise ValueError('missing torznab base_url')
94
95

◆ request()

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

Definition at line 96 of file torznab.py.

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

◆ response()

List[Dict[str, Any]] searx.engines.torznab.response ( httpx.Response resp)
Parse the XML response and return a list of results.

Definition at line 112 of file torznab.py.

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

References searx.engines.torznab.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 68 of file torznab.py.

◆ api_key

str searx.engines.torznab.api_key = ''

Definition at line 83 of file torznab.py.

◆ base_url

str searx.engines.torznab.base_url = ''

Definition at line 82 of file torznab.py.

◆ categories

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

Definition at line 76 of file torznab.py.

◆ logger

logging searx.engines.torznab.logger .Logger

Definition at line 65 of file torznab.py.

◆ paging

bool searx.engines.torznab.paging = False

Definition at line 77 of file torznab.py.

◆ show_magnet_links

bool searx.engines.torznab.show_magnet_links = True

Definition at line 87 of file torznab.py.

◆ show_torrent_files

bool searx.engines.torznab.show_torrent_files = False

Definition at line 86 of file torznab.py.

◆ time_range_support

bool searx.engines.torznab.time_range_support = False

Definition at line 78 of file torznab.py.

◆ torznab_categories

list searx.engines.torznab.torznab_categories = []

Definition at line 85 of file torznab.py.