示例#1
0
    def download_as_bytearray(self, buf: bytearray = None) -> bytes:
        """Download this file and return it as a bytearray.

        Args:
            buf (:obj:`bytearray`, optional): Extend the given bytearray with the downloaded data.

        Returns:
            :obj:`bytearray`: The same object as :attr:`buf` if it was specified. Otherwise a newly
            allocated :obj:`bytearray`.

        """
        if buf is None:
            buf = bytearray()
        if is_local_file(self.file_path):
            with open(self.file_path, "rb") as file:
                buf.extend(file.read())
        else:
            buf.extend(self.bot.request.retrieve(self._get_encoded_url()))
        return buf
示例#2
0
    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.
            * If neither :attr:`custom_path` nor :attr:`out` is provided and :attr:`file_path` is
              the path of a local file (which is the case when a Bot API Server is running in
              local mode), this method will just return the path.

        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 or the file path of the local file.

        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')

        local_file = is_local_file(self.file_path)

        if local_file:
            url = self.file_path
        else:
            # Convert any UTF-8 char into a url encoded ASCII string.
            url = self._get_encoded_url()

        if out:
            if local_file:
                with open(url, 'rb') as file:
                    buf = file.read()
            else:
                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

        if custom_path and local_file:
            shutil.copyfile(self.file_path, custom_path)
            return custom_path

        if custom_path:
            filename = custom_path
        elif local_file:
            return self.file_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_is_local_file(self, string, expected):
     assert helpers.is_local_file(string) == expected