def _get_encoded_url(self):
     """Convert any UTF-8 char in :obj:`File.file_path` into a url encoded ASCII string."""
     sres = urllib_parse.urlsplit(self.file_path)
     return urllib_parse.urlunsplit(
         urllib_parse.SplitResult(sres.scheme, sres.netloc,
                                  urllib_parse.quote(sres.path), sres.query,
                                  sres.fragment))
Example #2
0
    def download(self, custom_path=None, out=None, timeout=None):
        """
        Download this file. By default, the file is saved in the current working directory with its
        original filename as reported by Telegram. 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:
            `custom_path` and `out` are mutually exclusive.

        Args:
            custom_path (:obj:`str`, optional): Custom path.
            out (:obj:`object`, optional): A file-like object. Must be opened 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).

        Raises:
            ValueError: If both ``custom_path`` and ``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.
        sres = urllib_parse.urlsplit(self.file_path)
        url = urllib_parse.urlunsplit(
            urllib_parse.SplitResult(sres.scheme, sres.netloc,
                                     urllib_parse.quote(sres.path), sres.query,
                                     sres.fragment))

        if out:
            buf = self.bot.request.retrieve(url)
            out.write(buf)

        else:
            if custom_path:
                filename = custom_path
            else:
                filename = basename(self.file_path)

            self.bot.request.download(url, filename, timeout=timeout)