Ë
    Fü´jž*  ã                  ón  — d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZmZmZ ej4                  dk\  rddlmZ nddlmZ e
rddlmZ ddl Z ddl m!Z!  G d„ d«      Z" G d„ de"«      Z# G d„ de"«      Z$ G d„ de"«      Z% G d„ de"«      Z& G d„ de"«      Z'y)z.Provide classes for uploading media to Reddit.é    )ÚannotationsN)Úcached_property)ÚPath)ÚIOÚTYPE_CHECKINGÚAnyÚClassVarÚcast)ÚElementTree©ÚServerError)ÚAPI_PATHÚJPEG_HEADER)ÚClientExceptionÚRedditAPIExceptionÚRedditErrorItemÚTooLargeMediaException)é   é   )Úguess_file_type)Ú
guess_type)ÚResponse)Úmodelsc                  ó¸   — e Zd ZU dZded<   dZded<   edd„«       Zdd„Zdd„Z	ddd
„Z
dd„Zdd„Z	 	 	 	 	 	 	 	 dd„Zedd„«       Z	 	 	 	 	 	 	 	 dd„Zdd„Zdd„Zy	)ÚMediazÍBase class representing media that can be uploaded to Reddit.

    Use one of the subclasses, e.g., :class:`.EmojiMedia` or :class:`.PostMedia`,
    depending on what the media is being uploaded for.

    zClassVar[str]ÚLEASE_API_PATHÚs3UploadLeaseÚLEASE_RESPONSE_KEYc               ó   — t        | «      ‚©Nr   ©Úresponses    úO/root/workspace/ytshorts/venv/lib/python3.12/site-packages/praw/models/media.pyÚ_raise_upload_errorzMedia._raise_upload_error,   s   € ä˜(Ó#Ð#ó    c                óš   — t        |«      t        | «      u xr4 | j                  |j                  k(  xr | j                  |j                  k(  S )z5Return whether the other instance equals the current.)ÚtypeÚnameÚ_data)ÚselfÚothers     r#   Ú__eq__zMedia.__eq__0   s<   € ä�E‹{œd 4›jÐ(Òb¨T¯Y©Y¸%¿*¹*Ñ-DÒbÈÏÉÐW\×WbÑWbÑIbÐbr%   c                ón   — t        | j                  j                  | j                  | j                  f«      S )z(Return the hash of the current instance.)ÚhashÚ	__class__Ú__name__r(   r)   ©r*   s    r#   Ú__hash__zMedia.__hash__4   s&   € ä�T—^‘^×,Ñ,¨d¯i©i¸¿¹ÐDÓEÐEr%   Nc               óº   — t        |t        «      r|sd}t        |«      ‚|}n+t        |«      }|xs |j                  }|j                  «       }|| _        || _        y)a�  Initialize a :class:`.Media` instance.

        :param fp: The path to a media file as a string, or the content of a media file
            as a :class:`bytes` object.
        :param name: The name of the media file, e.g., ``"picture.png"``. The name is
            used to infer the media's MIME type. When ``fp`` is a path the name is
            derived from it, otherwise this parameter is required.

        z/'name' is required when 'fp' is a bytes object.N)Ú
isinstanceÚbytesÚ
ValueErrorr   r(   Ú
read_bytesr)   )r*   Úfpr(   ÚmsgÚdataÚpaths         r#   Ú__init__zMedia.__init__8   sV   € ô �bœ%Ô ÙØG�Ü  “oÐ%Ø‰Dä˜“8ˆDØÒ$˜4Ÿ9™9ˆDØ—?‘?Ó$ˆDØˆŒ
Øˆ�	r%   c                óP   — d| j                   j                  › d| j                  ›d�S )z/Return a string representation of the instance.ú<z name=ú>)r/   r0   r(   r1   s    r#   Ú__repr__zMedia.__repr__N   s&   € à�4—>‘>×*Ñ*Ð+¨6°$·)±)°¸aÐ@Ð@r%   c                ó8   — | j                   | j                  dœ|¥S )N)ÚfilepathÚmimetype)r(   Ú
_mime_type)r*   Úadditional_datas     r#   Ú_build_lease_datazMedia._build_lease_dataR   s   € Ø ŸI™I°4·?±?ÑVÀoÐVÐVr%   c               ó¾   —  | j                   di |¤Ž}| j                  |||«      \  }}}| j                  |||«      }|j                  s| j	                  |«       |||fS )N© )rF   Ú_obtain_leaseÚ_post_to_s3Úokr$   )	r*   Ú	lease_urlÚredditÚadditional_lease_dataÚ
lease_dataÚlease_responseÚupload_dataÚ
upload_urlr"   s	            r#   Ú_lease_and_postzMedia._lease_and_postU   sm   € ð ,�T×+Ñ+ÑDÐ.CÑDˆ
Ø26×2DÑ2DÀZÐQZÐ\bÓ2cÑ/ˆ˜ ZØ×#Ñ# F¨K¸ÓDˆØ�{Š{Ø×$Ñ$ XÔ.Ø˜{¨JÐ6Ð6r%   c                óp   — t        | j                  «      \  }}|€d| j                  ›d�}t        |«      ‚|S )Nz%Unable to determine the MIME type of ú.)r   r(   r   )r*   Ú	mime_typeÚ_r9   s       r#   rD   zMedia._mime_type_   s=   € ä& t§y¡yÓ1‰ˆ	�1ØÐØ9¸$¿)¹)¸ÀaÐHˆCÜ! #Ó&Ð&ØÐr%   c               óœ   — |j                  ||¬«      }|| j                     }|d   D �ci c]  }|d   |d   “Œ }}d|d   › �}|||fS c c}w )N)r:   Úfieldsr(   Úvaluezhttps:Úaction)Úpostr   )	r*   rO   rL   rM   rP   Úupload_leaseÚitemrQ   rR   s	            r#   rI   zMedia._obtain_leaseg   sp   € ð  Ÿ™ Y°Z˜Ó@ˆØ% d×&=Ñ&=Ñ>ˆØ?KÈHÑ?UÖV°t�t˜F‘| T¨'¡]Ñ2ÐVˆÐVØ˜l¨8Ñ4Ð5Ð6ˆ
Ø˜{¨JÐ6Ð6ùò Ws   ªA	c               ó    — |j                   €J ‚|j                   j                  j                  d||d| j                  | j                  fi¬«      S )NÚPOSTÚfile©r:   Úfiles)Ú_coreÚ	requestorÚrequestr(   r)   )r*   rM   rQ   rR   s       r#   rJ   zMedia._post_to_s3p   sR   € Ø�|‰|Ð'Ð'Ð'Ø�|‰|×%Ñ%×-Ñ-ØØØØ˜DŸI™I t§z¡zÐ2Ð3ð	 .ó 
ð 	
r%   c               ó’   — | j                   j                  |¬«      } | j                  ||j                  fi |¤Ž\  }}}|› d|d   › �S )a  Upload the media to Reddit.

        :param subreddit: The subreddit the media is associated with.
        :param additional_lease_data: Additional data to include in the upload lease
            request.

        :returns: The URL of the uploaded media.

        ©Ú	subredditú/Úkey©r   ÚformatrS   Ú_reddit)r*   ri   rN   rL   rW   rQ   rR   s          r#   Ú_uploadzMedia._uploady   s[   € ð ×'Ñ'×.Ñ.¸Ð.ÓCˆ	Ø%9 T×%9Ñ%9¸)ÀY×EVÑEVÑ%pÐZoÑ%pÑ"ˆˆ;˜
Ø�˜Q˜{¨5Ñ1Ð2Ð3Ð3r%   ©r"   r   ÚreturnÚNone)r+   Úobjectrq   Úbool)rq   Úintr    )r(   ú
str | Noner8   zstr | bytesrq   rr   ©rq   Ústr)rE   rx   rq   údict[str, str])rL   rx   rM   úpraw.RedditrN   rx   rq   ú*tuple[dict[str, Any], dict[str, str], str])rO   ry   rL   rx   rM   rz   rq   r{   )rM   rz   rQ   ry   rR   rx   rq   r   ©ri   úmodels.SubredditrN   rx   rq   rx   )r0   Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   Ústaticmethodr$   r,   r2   r<   r@   rF   rS   r   rD   rI   rJ   ro   rH   r%   r#   r   r   !   s±   … ñð "Ó!Ø(7Ð˜Ó7àò$ó ð$ócóFôó,AóWð7Øð7Ø&1ð7ØORð7à	3ó7ð òó ðð7Ø(ð7Ø58ð7ØBMð7à	3ó7ó
ô4r%   r   c                  ó"   — e Zd ZdZed   Zdd„Zy)Ú
EmojiMediazVMedia to be uploaded as a subreddit emoji.

    See :meth:`.SubredditEmoji.add`.

    Úemoji_leasec               óˆ   — | j                   j                  |¬«      } | j                  ||j                  fi |¤Ž\  }}}|d   S )a  Upload the media to Reddit.

        :param subreddit: The subreddit the emoji is associated with.
        :param additional_lease_data: Additional data to include in the upload lease
            request.

        :returns: The S3 key of the uploaded media.

        rh   rk   rl   )r*   ri   rN   rL   rW   rQ   s         r#   ro   zEmojiMedia._upload‘   sO   € ð ×'Ñ'×.Ñ.¸Ð.ÓCˆ	Ø0˜D×0Ñ0°¸I×<MÑ<MÑgÐQfÑgÑˆˆ;˜Ø˜5Ñ!Ð!r%   Nr|   )r0   r~   r   r€   r   r   ro   rH   r%   r#   r„   r„   ˆ   s   „ ñð ˜mÑ,€Nô"r%   r„   c                  ó`   — e Zd ZdZed   ZdZed
d„«       Zed
d„«       Z	dddœ	 	 	 	 	 	 	 dd	„Z
y)Ú	PostMediazqMedia to be uploaded as part of a submission.

    See :meth:`.Subreddit.submit` and :class:`.InlineMedia`.

    Úmedia_assetÚargsc               ó   — t        j                  | j                  «      }|D �cg c]  }|j                  ‘Œ }}|dd g d¢k(  r7d„ |dd D «       �^ }}}t	        t        |xs d«      t        |xs d«      ¬«      ‚yc c}w )z9Parse the XML from a response and raise any errors found.Né   )ÚCodeÚMessageÚProposedSizeÚMaxSizeAllowedc              3  ó4   K  — | ]  }|j                   –— Œ y ­wr    )Útext)Ú.0Úelements     r#   ú	<genexpr>z0PostMedia._parse_xml_response.<locals>.<genexpr>±   s   è ø€ Ò'M¸¨¯­Ñ'Mùs   ‚r   )ÚactualÚmaximum_size)r   Ú
fromstringr’   Útagr   ru   )r"   Úrootr”   ÚtagsrW   r–   r—   s          r#   Ú_parse_xml_responsezPostMedia._parse_xml_responseª   s‚   € ô ×%Ñ% h§m¡mÓ4ˆØ+/Ö0 �—“Ð0ˆÐ0Ø��ˆ8ÒLÒLá'MÀDÈÈ!ÀHÔ'MÑ$ˆQ�˜Ü(´°F²K¸aÓ0@ÌsÐS_ÒSdÐcdÓOeÔfÐfð Mùò 1s   ¤A;c               óX   — t         j                  | «       t        j                  | «       y r    )rˆ   rœ   r   r$   r!   s    r#   r$   zPostMedia._raise_upload_error´   s   € ä×%Ñ% hÔ/Ü×!Ñ! (Õ+r%   NÚlink)Úexpected_mime_prefixÚupload_typec              ó  — |�L| j                   j                  d«      d   |k7  r+d|›d| j                   ›d| j                  ›d�}t        |«      ‚| j	                  | j
                  |«      \  }}}|dk(  r
|› d|d   › �S |d	   d
   S )a¯  Upload the media to Reddit (undocumented endpoint).

        Unlike the other :class:`.Media` subclasses, post media is not associated with a
        subreddit when uploaded, so this method takes a :class:`.Reddit` instance.

        :param reddit: The :class:`.Reddit` instance to upload with.
        :param expected_mime_prefix: If provided, enforce that the media has a MIME type
            that starts with the provided prefix.
        :param upload_type: One of ``"link"``, ``"gallery"``, or ``"selfpost"``
            (default: ``"link"``).

        :returns: The URL of the uploaded media when ``upload_type`` is ``"link"``,
            otherwise the media's asset ID.

        rj   r   z"Expected a mimetype starting with z but got mimetype z (from file name z).rž   rk   ÚassetÚasset_id)rD   Ú	partitionr(   r   rS   r   )r*   rM   rŸ   r    r9   rP   rQ   rR   s           r#   ro   zPostMedia._upload¹   sÊ   € ð.  Ð+°·±×0IÑ0IÈ#Ó0NÈqÑ0QÐUiÒ0iØ6Ð7KÐ6NÐN`Ðae×apÑapÐ`sð  tEð  FJ÷  FOñ  FOð  ERð  RTð  UˆCÜ! #Ó&Ð&Ø26×2FÑ2FÀt×GZÑGZÐ\bÓ2cÑ/ˆ˜ ZØ˜&Ò Ø �\  ;¨uÑ#5Ð"6Ð7Ð7Ø˜gÑ& zÑ2Ð2r%   rp   )rM   rz   rŸ   rv   r    rx   rq   rx   )r0   r~   r   r€   r   r   r   r‚   rœ   r$   ro   rH   r%   r#   rˆ   rˆ       st   „ ñð ˜mÑ,€NØÐàògó ðgð ò,ó ð,ð ,0Ø!ñ3àð3ð
 )ð3ð ð3ð 
ô3r%   rˆ   c                  ó:   ‡ — e Zd ZdZed   Z	 	 	 	 	 	 dˆ fd„Zˆ xZS )ÚStylesheetAssetz{Media to be uploaded as a subreddit style asset, e.g., a banner.

    See :meth:`.SubredditStylesheet.upload_banner`.

    Ústyle_asset_leasec              ó&   •— t         ‰| �  ||¬«      S )zûUpload the media to Reddit.

        :param subreddit: The subreddit the style asset is associated with.
        :param image_type: The type of style asset, e.g., ``"bannerBackgroundImage"``.

        :returns: The URL of the uploaded media.

        )Ú	imagetype)Úsuperro   )r*   ri   Ú
image_typer/   s      €r#   ro   zStylesheetAsset._uploadâ   s   ø€ ô ‰w‰˜y°JˆÓ?Ð?r%   )ri   r}   r«   rx   rq   rx   )r0   r~   r   r€   r   r   ro   Ú__classcell__)r/   s   @r#   r¦   r¦   Ù   s<   ø„ ñð Ð1Ñ2€Nð@Ø)ð@Ø=@ð@à	÷@ñ @r%   r¦   c                  ó@   — e Zd ZdZed   Zedd„«       Z	 	 	 	 	 	 dd„Zy)ÚStylesheetImageziMedia to be uploaded as a subreddit stylesheet image.

    See :meth:`.SubredditStylesheet.upload`.

    Úupload_imagec                óH   — | j                   j                  t        «      rdS dS )NÚjpgÚpng)r)   Ú
startswithr   r1   s    r#   Ú_image_typezStylesheetImage._image_typeù   s   € àŸ
™
×-Ñ-¬kÔ:ˆuÐEÀÐEr%   c               óv  — d| j                   i|¥}| j                  j                  |¬«      }t        dd| j                  | j
                  fi«      }|j                  j                  |||¬«      }|d   rE|d   d   }|j                  dd	g«      d   }|d
v sJ d«       ‚t        t        |d	|xs d	¬«      g«      ‚|S )a8  Upload the media to Reddit.

        :param subreddit: The subreddit the stylesheet image is associated with.
        :param additional_data: Additional data to include in the upload request.

        :returns: A dictionary containing a link to the uploaded image under the key
            ``img_src``.

        Úimg_typerh   zdict[str, IO[Any]]ra   rb   Úerrorsr   Úerrors_valuesÚ >   ÚIMAGE_ERRORÚBAD_CSS_NAMEzPlease file a bug with PRAW.)Ú
error_typeÚfieldÚmessage)r´   ÚUPLOAD_API_PATHrm   r
   r(   r)   rn   r\   Úgetr   r   )	r*   ri   rE   r:   Úurlrc   r"   r¼   Úerror_values	            r#   ro   zStylesheetImage._uploadý   så   € ð ˜D×,Ñ,Ð@°Ð@ˆØ×"Ñ"×)Ñ)°IÐ)Ó>ˆô Ð)¨F°T·Y±YÀÇ
Á
Ð4KÐ+LÓMˆØ×$Ñ$×)Ñ)¨#°DÀÐ)ÓFˆØ�HÒØ! (Ñ+¨AÑ.ˆJØ"Ÿ,™, ¸¸Ó=¸aÑ@ˆKØð "ñ ð .ð .ó.ð ô %¤oÀÐSUÐ_jÒ_pÐnpÔ&qÐ%rÓsÐsØˆr%   Nrw   )ri   r}   rE   rx   rq   zdict[str, Any])	r0   r~   r   r€   r   r¿   r   r´   ro   rH   r%   r#   r®   r®   ð   sD   „ ñð ˜~Ñ.€OàòFó ðFðØ)ðØADðà	ôr%   r®   c                  ó   — e Zd ZdZed   Zy)ÚWidgetMediaztMedia to be uploaded for use in a subreddit widget.

    See :meth:`.SubredditWidgetsModeration.upload_image`.

    Úwidget_leaseN)r0   r~   r   r€   r   r   rH   r%   r#   rÄ   rÄ     s   „ ñð ˜nÑ-�Nr%   rÄ   )(r€   Ú
__future__r   ÚsysÚ	functoolsr   Úpathlibr   Útypingr   r   r   r	   r
   Ú
defusedxmlr   Úprawcore.exceptionsr   Ú
praw.constr   r   Úpraw.exceptionsr   r   r   r   Úversion_infoÚ	mimetypesr   r   Úrequestsr   Úprawr   r   r„   rˆ   r¦   r®   rÄ   rH   r%   r#   ú<module>rÓ      sœ   ðÙ 4å "ã 
Ý %Ý ß 9Õ 9å "Ý +ç ,÷ó ð ×Ñ�wÒÞ)å7áÝ!ãÝ÷d4ñ d4ôN"�ô "ô063�ô 63ôr@�eô @ô.'�eô 'ôT.�%õ .r%   