L i=dZddlZddlmZddlmZddlmZddl m Z m Z m Z ddl mZddlmZddlmZdd lmZdd lmZmZmZe rdd lmZGd d eZy)z?This module contains an object that represents a Telegram File.N) b64decode)Path) TYPE_CHECKINGBinaryIOOptional)decrypt)TelegramObject) DEFAULT_NONE) is_local_file) FilePathInputJSONDictODVInput)FileCredentialsc^eZdZdZdZ ddddededeedeed eef fd Z d efd Z d e d e fdZ de e e e ddeedeedeedeedeed ef dZe e e e ddedeedeedeedeed df dZ de e e e dd eedeedeedeedeed ef dZddZxZS)Filea This object represents a file ready to be downloaded. The file can be e.g. downloaded with :attr:`download_to_drive`. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling :meth:`telegram.Bot.get_file`. Objects of this class are comparable in terms of equality. Two objects of this class are considered equal, if their :attr:`file_unique_id` is equal. .. versionchanged:: 20.0 ``download`` was split into :meth:`download_to_drive` and :meth:`download_to_memory`. Note: * Maximum file size to download is :tg-const:`telegram.constants.FileSizeLimit.FILESIZE_DOWNLOAD`. * If you obtain an instance of this class from :attr:`telegram.PassportFile.get_file`, then it will automatically be decrypted as it downloads when you call e.g. :meth:`download_to_drive`. Args: file_id (:obj:`str`): Identifier for this file, which can be used to download or reuse the file. file_unique_id (:obj:`str`): Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file. file_size (:obj:`int`, optional): File size in bytes, if known. file_path (:obj:`str`, optional): File path. Use e.g. :meth:`download_to_drive` to get the file. Attributes: file_id (:obj:`str`): Identifier for this file, which can be used to download or reuse the file. file_unique_id (:obj:`str`): Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file. file_size (:obj:`int`): Optional. File size in bytes, if known. file_path (:obj:`str`): Optional. File path. Use e.g. :meth:`download_to_drive` to get the file. ) _credentialsfile_id file_path file_sizefile_unique_idN api_kwargsrrrrrct||t||_t||_||_||_d|_|jf|_|jy)Nr) super__init__strrrrrr _id_attrs_freeze)selfrrrrr __class__s Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/telegram/_files/file.pyrz File.__init__Us^ J/ L #&~#6(1(17;--/ returnc 4tjt|j}tjtj |j |jtj|j|j|jS)zPConvert any UTF-8 char in :obj:`File.file_path` into a url encoded ASCII string.) urllib_parseurlsplitrr urlunsplit SplitResultschemenetlocquotepathqueryfragment)rsress r!_get_encoded_urlzFile._get_encoded_urlmsi$$S%89&&  $ $ T[[,*<*` .. versionchanged:: 20.0 * :paramref:`custom_path` parameter now also accepts :class:`pathlib.Path` as argument. * Returns :class:`pathlib.Path` object in cases where previously a :obj:`str` was returned. * This method was previously called ``download``. It was split into :meth:`download_to_drive` and :meth:`download_to_memory`. .. versionchanged:: 21.7 Raises :exc:`RuntimeError` if :attr:`file_path` is not set. Note that files without a :attr:`file_path` could never be downloaded, as this attribute is mandatory for that operation. Args: custom_path (:class:`pathlib.Path` | :obj:`str` , optional): The path where the file will be saved to. If not specified, will be saved in the current working directory with :attr:`file_path` as file name or the :attr:`file_id` if :attr:`file_path` is not set. Keyword Args: read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. Returns: :class:`pathlib.Path`: Returns the Path object the file was downloaded to. Raises: RuntimeError: If :attr:`file_path` is not set. 9No `file_path` available for this file. Can not download.Nz /decrypted_r7)r RuntimeErrorr r0rrr6 read_bytesrparentname write_bytesshutilcopyfileget_botrequestretrieve) rr<r8r9r:r; local_fileurlfile_to_decryptr1r,filenames r!download_to_drivezFile.download_to_driveysD~~Z[ ["4>>2  dd&;&;&= $++"4>>2O''(B(B(DEC&K(C 6 67-G/J^J^^_   S !K  "z OODNNC ,< = $ $ K(H ' 'D0556HLLN**33 %'+% 4     '',CS! sFG F? 5GoutcK|js tdt|j}|rdn|j}|rt |jnd}|r|j } n6|j jj|||||d{} |jr|j| } |j| y73w)a Download this file into memory. :paramref:`out` needs to be supplied with a :obj:`io.BufferedIOBase`, the file contents will be saved to that object using the :obj:`out.write` method. .. seealso:: :wiki:`Working with Files and Media ` Hint: If you want to immediately read the data from ``out`` after calling this method, you should call ``out.seek(0)`` first. See also :meth:`io.IOBase.seek`. .. versionadded:: 20.0 .. versionchanged:: 21.7 Raises :exc:`RuntimeError` if :attr:`file_path` is not set. Note that files without a :attr:`file_path` could never be downloaded, as this attribute is mandatory for that operation. Args: out (:obj:`io.BufferedIOBase`): A file-like object. Must be opened for writing in binary mode. Keyword Args: read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. Raises: RuntimeError: If :attr:`file_path` is not set. r>Nr7) rr?r r0rr@rFrGrHrr6write) rrNr8r9r:r;rIrJr,r1s r!download_to_memoryzFile.download_to_memorys`~~Z[ ["4>>2  dd&;&;&='1tDNN#t //#C ..77)+ /) 8C   '',C #sBC C!4CcK|js td| t}t|jr$t |jj }nD|j jj|j||||d{}|jr"|j|j||S|j||S7Ew)a Download this file and return it as a bytearray. .. versionchanged:: 21.7 Raises :exc:`RuntimeError` if :attr:`file_path` is not set. Note that files without a :attr:`file_path` could never be downloaded, as this attribute is mandatory for that operation. Args: buf (:obj:`bytearray`, optional): Extend the given bytearray with the downloaded data. Keyword Args: read_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.read_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. .. versionadded:: 20.0 write_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.write_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. .. versionadded:: 20.0 connect_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.connect_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. .. versionadded:: 20.0 pool_timeout (:obj:`float` | :obj:`None`, optional): Value to pass to :paramref:`telegram.request.BaseRequest.post.pool_timeout`. Defaults to :attr:`~telegram.request.BaseRequest.DEFAULT_NONE`. .. versionadded:: 20.0 Returns: :obj:`bytearray`: The same object as :paramref:`buf` if it was specified. Otherwise a newly allocated :obj:`bytearray`. Raises: RuntimeError: If :attr:`file_path` is not set. r>Nr7) rr? bytearrayr rr@rFrGrHr0rextendr6)rr1r8r9r:r; bytes_datas r!download_as_bytearrayzFile.download_as_bytearray'sb~~Z[ [ ;+C  (dnn-88:J#||~55>>%%')+ /) ? J    JJt,,Z8 9  JJz " sBC&C$AC&c||_y)zSets the passport credentials for the file. Args: credentials (:class:`telegram.FileCredentials`): The credentials. N)r)r credentialss r!set_credentialszFile.set_credentialsns (r")NNr3)rXrr#N)__name__ __module__ __qualname____doc__ __slots__rrintr rr0bytesr6r r rfloatrrMrrQrSrVrY __classcell__)r s@r!rr%s%NI$(#' *.C=  C= X&0 # dEded 04h)5)5+7(4hm,huo h   h "% huoh h\)5)5+7(4B Buo B   B "% BuoB BL$(E)5)5+7(4E i Euo E   E "% EuoE EN(r"r)r]rD urllib.parseparser%base64rpathlibrtypingrrrtelegram._passport.credentialsrtelegram._telegramobjectr telegram._utils.defaultvaluer telegram._utils.filesr telegram._utils.typesr r rtelegramrrr"r!rosC&F #44235/CC(O(>O(r"