def download(self, custom_path: str = None, out: IO = None, timeout: int = None) -> Union[str, IO]: """ Download this file. By default, the file is saved in the current working directory with its original filename as reported by Telegram. If the file has no filename, it the file ID will be used as filename. If a :attr:`custom_path` is supplied, it will be saved to that path instead. If :attr:`out` is defined, the file contents will be saved to that object using the ``out.write`` method. Note: :attr:`custom_path` and :attr:`out` are mutually exclusive. Args: custom_path (:obj:`str`, optional): Custom path. out (:obj:`io.BufferedWriter`, optional): A file-like object. Must be opened for writing in binary mode, if applicable. timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as the read timeout from the server (instead of the one specified during creation of the connection pool). Returns: :obj:`str` | :obj:`io.BufferedWriter`: The same object as :attr:`out` if specified. Otherwise, returns the filename downloaded to. Raises: ValueError: If both :attr:`custom_path` and :attr:`out` are passed. """ if custom_path is not None and out is not None: raise ValueError('custom_path and out are mutually exclusive') # Convert any UTF-8 char into a url encoded ASCII string. url = self._get_encoded_url() if out: buf = self.bot.request.retrieve(url) if self._credentials: buf = decrypt(b64decode(self._credentials.secret), b64decode(self._credentials.hash), buf) out.write(buf) return out else: if custom_path: filename = custom_path elif self.file_path: filename = basename(self.file_path) else: filename = os.path.join(os.getcwd(), self.file_id) buf = self.bot.request.retrieve(url, timeout=timeout) if self._credentials: buf = decrypt(b64decode(self._credentials.secret), b64decode(self._credentials.hash), buf) with open(filename, 'wb') as fobj: fobj.write(buf) return filename
def test_credentials_decrypt(self, monkeypatch): if not TEST_NO_PASSPORT: monkeypatch.setattr(credentials, 'CRYPTO_INSTALLED', False) with pytest.raises(RuntimeError, match='passport'): credentials.decrypt(1, 1, 1)