.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 184 of file torznab.py.

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

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 209 of file torznab.py.

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

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 192 of file torznab.py.

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

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 176 of file torznab.py.

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

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 201 of file torznab.py.

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

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 130 of file torznab.py.

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

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 226 of file torznab.py.

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

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 234 of file torznab.py.

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

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

◆ request()

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

Definition at line 95 of file torznab.py.

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

◆ 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 111 of file torznab.py.

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

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 67 of file torznab.py.

◆ api_key

str searx.engines.torznab.api_key = ''

Definition at line 82 of file torznab.py.

◆ base_url

str searx.engines.torznab.base_url = ''

Definition at line 81 of file torznab.py.

◆ categories

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

Definition at line 75 of file torznab.py.

◆ logger

logging searx.engines.torznab.logger .Logger

Definition at line 64 of file torznab.py.

◆ paging

bool searx.engines.torznab.paging = False

Definition at line 76 of file torznab.py.

◆ show_magnet_links

bool searx.engines.torznab.show_magnet_links = True

Definition at line 86 of file torznab.py.

◆ show_torrent_files

bool searx.engines.torznab.show_torrent_files = False

Definition at line 85 of file torznab.py.

◆ time_range_support

bool searx.engines.torznab.time_range_support = False

Definition at line 77 of file torznab.py.

◆ torznab_categories

list searx.engines.torznab.torznab_categories = []

Definition at line 84 of file torznab.py.