Skip to content

Commit

Permalink
Add support for search/downloadTorrent
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 committed May 24, 2024
1 parent 49bad8a commit ce41119
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/source/apidoc/search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Search
.. autoclass:: qbittorrentapi.search.SearchAPIMixIn
:members:
:undoc-members:
:exclude-members: search, search_installPlugin, search_uninstallPlugin, search_enablePlugin, search_updatePlugins
:exclude-members: search, search_installPlugin, search_uninstallPlugin, search_enablePlugin, search_updatePlugins, search_downloadTorrent
:show-inheritance:

.. autoclass:: qbittorrentapi.search.Search
:members:
:undoc-members:
:exclude-members: installPlugin, uninstallPlugin, enablePlugin, updatePlugins
:exclude-members: installPlugin, uninstallPlugin, enablePlugin, updatePlugins, downloadTorrent

.. autoclass:: qbittorrentapi.search.SearchJobDictionary
:members:
Expand Down
37 changes: 37 additions & 0 deletions src/qbittorrentapi/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,32 @@ def search_update_plugins(self, **kwargs: APIKwargsT) -> None:

search_updatePlugins = search_update_plugins

def search_download_torrent(
self,
url: str | None = None,
plugin: str | None = None,
**kwargs: APIKwargsT,
) -> None:
"""
Download a .torrent file or magnet for a search plugin.
:param url: URL for .torrent file or magnet
:param plugin: Name of the plugin
"""
data = {
"torrentUrl": url,
"pluginName": plugin,
}
self._post(
_name=APINames.Search,
_method="downloadTorrent",
data=data,
version_introduced="2.11",
**kwargs,
)

search_downloadTorrent = search_download_torrent


class SearchJobDictionary(ClientCache[SearchAPIMixIn], Dictionary[JsonValueT]):
"""Response for :meth:`~SearchAPIMixIn.search_start`"""
Expand Down Expand Up @@ -513,3 +539,14 @@ def update_plugins(self, **kwargs: APIKwargsT) -> None:
return self._client.search_update_plugins(**kwargs)

updatePlugins = update_plugins

@wraps(SearchAPIMixIn.search_download_torrent)
def download_torrent(
self,
url: str | None = None,
plugin: str | None = None,
**kwargs: APIKwargsT,
) -> None:
return self._client.search_download_torrent(url=url, plugin=plugin)

downloadTorrent = download_torrent
42 changes: 42 additions & 0 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SearchStatusesList,
)

from tests.conftest import TORRENT2_HASH, TORRENT2_URL
from tests.utils import check, retry

PLUGIN_NAME = "one337x"
Expand Down Expand Up @@ -278,3 +279,44 @@ def test_delete(client):
def test_delete_not_implemented(client):
with pytest.raises(NotImplementedError):
client.search_stop(search_id=100)


@pytest.mark.skipif_before_api_version("2.11")
@pytest.mark.parametrize(
"client_func",
[
"search_download_torrent",
"search_downloadTorrent",
"search.download_torrent",
"search.downloadTorrent",
],
)
def test_download_torrent(client, client_func):
# run update to ensure plugins are loaded
client.search.update_plugins()
check(
lambda: [p.name for p in client.search.plugins],
"eztv",
reverse=True,
)
try:
client.func(client_func)(url=TORRENT2_URL, plugin="eztv")
check(
lambda: [t.hash for t in client.torrents_info()],
TORRENT2_HASH,
reverse=True,
)
finally:
client.torrents.delete(torrent_hashes=TORRENT2_HASH)
check(
lambda: [t.hash for t in client.torrents_info()],
TORRENT2_HASH,
reverse=True,
negate=True,
)


@pytest.mark.skipif_after_api_version("2.11")
def test_download_torrent_not_implemented(client):
with pytest.raises(NotImplementedError):
client.search_download_torrent(search_id=100)

0 comments on commit ce41119

Please sign in to comment.