def test_client_add_read_file_in_base64(): with open("tests/fixtures/iso.torrent", "rb") as f: content = f.read() f.seek(0) data = _try_read_torrent(f) assert base64.b64encode(content).decode() == data, "should base64 encode torrent file"
def add_torrent( self, torrent: Union[BinaryIO, str, bytes, pathlib.Path], timeout: _Timeout = None, *, download_dir: str = None, files_unwanted: List[int] = None, files_wanted: List[int] = None, paused: bool = None, peer_limit: int = None, priority_high: List[int] = None, priority_low: List[int] = None, priority_normal: List[int] = None, cookies: str = None, bandwidthPriority: int = None, ) -> Torrent: """ Add torrent to transfers list. ``torrent`` can be: - ``http://``, ``https://`` or ``magnet:`` URL - torrent file-like object in binary mode - bytes of torrent content - ``pathlib.Path`` for local torrent file, will be read and encoded as base64. - **deprecated** str of base64 encoded torrent file content - **deprecated** ``file://`` URL .. NOTE:: url starts with ``file://`` will be load by this package instead of transmission daemon Additional arguments are: ===================== ===== =========== ============================================================= Argument RPC Replaced by Description ===================== ===== =========== ============================================================= ``bandwidthPriority`` 8 - Priority for this transfer. ``cookies`` 13 - One or more HTTP cookie(s). ``download_dir`` 1 - The directory where the downloaded contents will be saved in. ``files_unwanted`` 1 - A list of file id's that shouldn't be downloaded. ``files_wanted`` 1 - A list of file id's that should be downloaded. ``paused`` 1 - If True, does not start the transfer when added. ``peer_limit`` 1 - Maximum number of peers allowed. ``priority_high`` 1 - A list of file id's that should have high priority. ``priority_low`` 1 - A list of file id's that should have low priority. ``priority_normal`` 1 - A list of file id's that should have normal priority. ===================== ===== =========== ============================================================= Returns a Torrent object with the fields. """ if torrent is None: raise ValueError("add_torrent requires data or a URI.") kwargs: Dict[str, Any] = {} if download_dir is not None: kwargs["download-dir"] = download_dir if files_unwanted is not None: kwargs["files-unwanted"] = files_unwanted if files_wanted is not None: kwargs["files-wanted"] = files_wanted if paused is not None: kwargs["paused"] = paused if peer_limit is not None: kwargs["peer-limit"] = peer_limit if priority_high is not None: kwargs["priority-high"] = priority_high if priority_low is not None: kwargs["priority-low"] = priority_low if priority_normal is not None: kwargs["priority-normal"] = priority_normal if bandwidthPriority is not None: kwargs["bandwidthPriority"] = bandwidthPriority if cookies is not None: kwargs["cookies"] = cookies torrent_data = _try_read_torrent(torrent) if torrent_data: kwargs["metainfo"] = torrent_data else: kwargs["filename"] = torrent _rpc_version_check("torrent-add", kwargs, self.rpc_version) return list( self._request("torrent-add", kwargs, timeout=timeout).values())[0]
def test_client_add_torrent_bytes(): with open("tests/fixtures/iso.torrent", "rb") as f: content = f.read() data = _try_read_torrent(content) assert base64.b64encode(content).decode() == data, "should base64 bytes"
def test_client_add_file_protocol(): with open("tests/fixtures/iso.torrent", "rb") as f: b64 = base64.b64encode(f.read()).decode() p = pathlib.Path("tests/fixtures/iso.torrent").absolute() with pytest.warns(DeprecationWarning): assert _try_read_torrent(f"file://{p}") == b64, "should skip handle base64 content"
def test_client_add_pathlib_path(): p = pathlib.Path("tests/fixtures/iso.torrent") b64 = base64.b64encode(p.read_bytes()).decode() assert _try_read_torrent(p) == b64, "should skip handle base64 content"
def test_client_add_base64_raw_data(): with open("tests/fixtures/iso.torrent", "rb") as f: b64 = base64.b64encode(f.read()).decode() with pytest.warns(DeprecationWarning): assert _try_read_torrent(b64) == b64, "should skip handle base64 content"
def test_client_add_magnet(): assert _try_read_torrent(magnet_url) is None, "handle magnet URL with daemon"
def test_client_add_url(): assert _try_read_torrent(torrent_url) is None, "handle http URL with daemon"