Ë
    £| kt�  ã                  óÀ  — d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	 ddl
mZmZ ddlmZmZ ddlmZ ddlmZmZ dd	lmZmZ d
dlmZ d
dlmZmZ d
dlmZ d
dlm Z  d
dl!m"Z" d
dl#m$Z$m%Z%m&Z& d
dl'm(Z(m)Z) d
dl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 d
dl1m2Z2m3Z3  ejh                  e5«      Z6dZ7dZ8dZ9dZ:dZ;dZ<dZ=dZ>dZ? ej€                  d«      ZA ej€                  d«      ZB ej€                  d«      ZC ej€                  d «      ZD ej€                  d!«      ZE ej€                  d"ejŒ                  «      ZG ej€                  d#«      ZH G d$„ d%eI«      ZJe G d&„ d'«      «       ZKd[d(„ZLd\d)„ZMd]d*„ZNe G d+„ d,«      «       ZOd^d-„ZPe G d.„ d/«      «       ZQd_d0„ZRd`dad1„ZSd`dbd2„ZTd`dcd3„ZUddd4„ZVd`ded5„ZWdfdgd6„ZXe G d7„ d8«      «       ZYd`dhd9„ZZe G d:„ d;«      «       Z[d`did<„Z\d`djd=„Z]d`dkd>„Z^dld?„Z_dld@„Z`dmdA„ZadndB„ZbdndC„ZcdodD„ZddpdE„ZedqdF„ZfdrdG„Zge G dH„ dI«      «       ZhdsdJ„ZiddddKœdtdL„ZjdMdMdNœdudO„ZkdvdP„ZldwdQ„ZmdxdR„ZndydS„ZodzdT„Zpe G dU„ dV«      «       Zqe G dW„ dX«      «       Zr G dY„ dZ«      Zsy){uD  Automatic publishing: the autorename bot copies a renamed episode into the Database Channel and says so in the
communication chat; this bot then makes the post for it in the channel that message names.

The message of the autorename bot (it writes the Channel and Episode lines from the format the episode was renamed with):

    File uploaded to the Database Channel
    Episode: 05
    Channel: -1001234567890
    File: Black Clover - S2 E05.mkv
    Post: 1234          <- the copy in the Database Channel (without it: the latest post of that channel)
    Ref: 3f9a1c         <- repeated in every answer

  1. The message arrives -> this bot answers "received".
  2. It looks at that post of the Database Channel and waits for the ðŸ”� Share URL button the file-store bot adds to it a
     few seconds later (never less than MIN_SHARE_URL_WAIT seconds). It looks through the bot; when the bot cannot see
     the post or its buttons, the userbot looks as well (see userbot.UserbotReader).
  3. The post is made from two templates the owner edits with /autopublish: the message text ("Episode {episode}" and
     so on) and the button name ("Download Episode {episode}"). The button opens the file link of the Share URL button.
     It goes to the channel the message names - nothing is guessed from the file name.
  4. "Post published successfully" - or "Publication failed: <why>" - goes back to the communication chat.

Deleting: the "Delete post" button of the autorename bot writes "Delete the published post" in the communication chat
(Channel / Message / File / Ref lines: the channel and the message id of the post, as this bot reported them). This bot then
deletes that post in the channel - only one it published by itself - and in My posts, and answers "Post deleted successfully"
(also when the post was already gone) or "Deletion failed: <why>".
é    )ÚannotationsN)ÚOrderedDict)Ú	dataclassÚfield)ÚAnyÚOptional)Úunquote)ÚerrorsÚtypes)Úadd_surrogateÚdel_surrogateé   )Únorm_url)ÚCtxÚ	send_post)ÚMIN_SHARE_URL_WAIT)Úutcnow)Úapply_span_replacements)Úfetch_messagesÚ
get_top_idÚis_skippable)Údelete_batchÚgone_ids)Ú
button_urlÚde_entitiesÚexplain_rpcÚpeer_ofÚ	post_linkÚser_entities)ÚUserbotErrorÚdelete_problem_textg      @g      $@éÈ   Úautopublishu:   Episode {episode}

Click the button below to download ðŸ‘‡zDownload Episode {episode}i¸  é<   é@   z3(?i)file\s+uploaded\s+to\s+the\s+database\s+channelz*(?i)\A\s*delete\s+the\s+published\s+post\bz"(?i)\A\s*get\s+the\s+share\s+url\bz(?i)\bshare\s*url\bzH(?i)^https?://(?:t\.me|telegram\.me|telegram\.dog)/share(?:/url)?\?(.*)$zU\{\s*(?P<name>episode(?:[\s_]*(?:number|num))?|channel(?:[\s_]*(?:name|title))?)\s*\}z\{[^{}\n]{0,40}\}c                  ó   — e Zd ZdZy)ÚFailurezTWhy a post could not be published: the text goes to the communication chat as it is.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    ú/app/app/dbpublish.pyr'   r'   K   s   „ Ú^r-   r'   c                  ób   — e Zd ZU dZded<   dZded<   dZded<   dZded<   dZded	<   dZ	ded
<   y)ÚNoticeÚ ÚstrÚfileÚepisodeNúOptional[int]ÚchannelÚchannel_rawÚpost_idÚref)
r(   r)   r*   r3   Ú__annotations__r4   r6   r7   r8   r9   r,   r-   r.   r0   r0   P   s<   … à€Dˆ#ƒNØ€GˆSÓØ!€Gˆ]Ó!Ø€K�ÓØ!€Gˆ]Ó!Ø€Cˆ„Mr-   r0   c                óü   — | xs dj                  «       } t        j                  d| «      }|rdt        |j	                  d«      «      z
  S t        j
                  d| «      }|rt        |j	                  d«      «      S dS )zfA chat id from what a person or a bot wrote: -1001234567890, 1234567890 or a t.me/c/1234567890/5 link.r1   z(?i)t\.me/c/(\d+)ìýÿÿÿ J)£r   z(-?\d{1,16})(?!\d)N)ÚstripÚreÚsearchÚintÚgroupÚmatch)ÚrawÚms     r.   Úchat_numberrE   Z   si   € àŠ9�"×
Ñ
Ó
€CÜ
�	‰	Ð&¨Ó,€AÙØ!¤C¨¯©°«
£OÑ3Ð3Ü
�‰Ð&¨Ó,€AÙŒ3ˆq�w‰w�q‹z‹?Ð) TÐ)r-   c                ó„   — t        j                  d|› d�| xs d«      }|r|j                  d«      j                  «       S dS )zTThe value of a ``Label: value`` line of a message; empty when there is no such line.z	(?im)^\s*z\s*:\s*(.+?)\s*$r1   r   )r>   r?   rA   r=   )ÚtextÚlabelrD   s      r.   Ú_fieldrI   d   s>   € ä
�	‰	�Y˜u˜gÐ%5Ð6¸º
ÀÓC€AÙ!"ˆ1�7‰7�1‹:×ÑÓÐ*¨Ð*r-   c           
     ó>  ‡ — t         j                  ‰ xs d«      sydˆ fd„} |d«      } |d«      }t         |d«      dj                   |d«      j	                  «       «      dd	 t        |«      |dd
 |j                  «       rt        |«      nd |d«      dd ¬«      S )zžThe message the autorename bot writes ("File uploaded to the Database Channel", then Episode / Channel / File /
    Post / Ref lines); None for anything else.r1   Nc                ó   •— t        ‰| «      S ©N)rI   )rH   rG   s    €r.   Úfield_ofzparse_notice.<locals>.field_ofp   s   ø€ Ü�d˜EÓ"Ð"r-   Úpostr6   r3   Ú r4   é   r$   r9   é(   )r3   r4   r6   r7   r8   r9   )rH   r2   Úreturnr2   )Ú
_NOTICE_RXr?   r0   ÚjoinÚsplitrE   Úisdigitr@   )rG   rM   rN   r7   s   `   r.   Úparse_noticerW   j   sœ   ø€ ô ×Ñ˜TšZ RÔ(Øõ#ñ �FÓ€DÙ˜9Ó%€KÜÙ�fÓØ—‘™ )Ó,×2Ñ2Ó4Ó5°c°rÐ:Ü˜KÓ(Ø  Ð$Ø!Ÿ\™\œ^”�D”	°Ù�U‹O˜C˜RÐ ôð r-   c                  óX   — e Zd ZU dZdZded<   dZded<   dZded<   dZded	<   dZ	ded
<   y)ÚDeleteNoticez=The autorename bot asking for a published post to be deleted.Nr5   r6   r1   r2   r7   Ú
message_idr3   r9   )
r(   r)   r*   r+   r6   r:   r7   rZ   r3   r9   r,   r-   r.   rY   rY      s5   … áGà!€Gˆ]Ó!Ø€K�ÓØ $€J�Ó$Ø€Dˆ#ƒNØ€Cˆ„Mr-   rY   c           
     ó  — t         j                  | xs d«      syt        | d«      t        | d«      }}t        t	        |«      |dd t        j                  d|«      rt        |«      ndt        | d«      t        | d«      dd	 ¬
«      S )z¢The message the autorename bot writes to have a post deleted ("Delete the published post", then Channel / Message /
    File / Ref lines); None for anything else.r1   Nr6   Úmessager$   ú\d{1,12}r3   r9   rQ   )r6   r7   rZ   r3   r9   )Ú
_DELETE_RXr?   rI   rY   rE   r>   Ú	fullmatchr@   )rG   r7   r\   s      r.   Úparse_delete_noticer`   Š   s€   € ô ×Ñ˜TšZ RÔ(ØÜ! $¨	Ó2´F¸4ÀÓ4K�€KÜÜ˜KÓ(Ø  Ð$Ü#%§<¡<°¸WÔ#E”3�w”<È4Ü�D˜&Ó!Ü�4˜Ó  Ð$ôð r-   c                  ó.   — e Zd ZU dZdZded<   dZded<   y)Ú
LinkNoticezjThe autorename bot asking only for the Share URL of a file in the Database Channel (nothing is published).Nr5   r8   r1   r2   r9   )r(   r)   r*   r+   r8   r:   r9   r,   r-   r.   rb   rb   ™   s   … átà!€Gˆ]Ó!Ø€Cˆ„Mr-   rb   c                óÄ   — t         j                  | xs d«      syt        | d«      }t        t	        j
                  d|«      rt        |«      ndt        | d«      dd ¬«      S )zC"Get the share URL", then Post / Ref lines; None for anything else.r1   NrN   r]   r9   rQ   )r8   r9   )Ú_LINK_RXr?   rI   rb   r>   r_   r@   )rG   rN   s     r.   Úparse_link_noticere   ¡   sU   € ä�?‰?˜4š: 2Ô&ØÜ�$˜Ó€DÜ¬2¯<©<¸ÀTÔ+Jœc $œiÐPTÔZ`ÐaeÐglÓZmÐnqÐoqÐZrÔsÐsr-   c                ó   — dt        | «      z   S )Nu4   ðŸ“¥ Message received - looking for the Share URLâ€¦©Ú_tail©Únotices    r.   Úlink_received_textrk   ©   s   € ØAÄEÈ&ÃMÑQÐQr-   c                óv   — dj                  dd| › �g|D �cg c]  }d|› �‘Œ	 c}¢«      t        |«      z   S c c}w )NÚ
u   ðŸ”— Share URL readyzURL: õ   â„¹ï¸� )rT   rh   )ÚurlÚnotesrj   Úns       r.   Úlink_ready_textrr   ­   sJ   € Ø�9‰9Ð,°°c°U¨mÐ]ÑV[Ó>\ÑV[ÐQRÀÈÈº}ÐV[Ñ>\Ð]Ó^ÔafÐgmÓanÑnÐnùÒ>\s   –6
c                ól   — ddj                  | xs dj                  «       «      d d › �t        |«      z   S )Nu   â�Œ Share URL failed: rO   úunknown reasonéX  )rT   rU   rh   ©Úreasonrj   s     r.   Úlink_failed_textrx   ±   s=   € Ø# C§H¡H¨fÒ.HÐ8H×-OÑ-OÓ-QÓ$RÐSWÐTWÐ$XÐ#YÐZÔ]bÐciÓ]jÑjÐjr-   c                ó@   — | �| j                   rd| j                   › �S dS )Nz
Ref: r1   )r9   ri   s    r.   rh   rh   µ   s#   € Ø%+Ð%7¸F¿JºJˆW�V—Z‘Z�LÐ!ÐNÈBÐNr-   c                ó   — dt        | «      z   S )Nu8   ðŸ“¥ Message received - checking the Database Channelâ€¦rg   ri   s    r.   Úreceived_textr{   ¹   s   € ØEÌÈfËÑUÐUr-   c                ó¬   — dj                  | xs dj                  «       «      } |xs |�|j                  nd}d| d d › �|rd|› �ndz   t        |«      z   S )NrO   rt   r1   u   â�Œ Publication failed: ru   ú
File: ©rT   rU   r3   rh   )rw   rj   r3   Únames       r.   Úfailed_textr€   ½   sd   € Ø�X‰X�vÒ1Ð!1×8Ñ8Ó:Ó;€FØÒ> 6Ð#5�F—K’K¸2€DØ% f¨T¨c l ^Ð4ÉT¸(À4À&Ñ8IÐWYÑZÔ]bÐciÓ]jÑjÐjr-   c                  óx   — e Zd ZU ded<   ded<   ded<   ded<   ded<   dZded	<    ee¬
«      Zded<   dZded<   y)Ú	Publishedr   r6   r2   ÚlinkrH   ro   r3   r1   r4   ©Údefault_factoryÚlistrp   r   r@   rZ   N)	r(   r)   r*   r:   r4   r   r†   rp   rZ   r,   r-   r.   r‚   r‚   Ã   s>   … àƒLØ
ƒIØƒJØ	ƒHØ
ƒIØ€GˆSÓÙ¨Ô-€Eˆ4Ó-Ø€J�Ôr-   r‚   c                ó*  — dd| j                   j                  › �d| j                  › �g}| j                  r|j	                  d| j                  › �«       |j	                  d| j
                  › d| j                  › �«       | j                  r|j	                  d| j                  › �«       || j                  D �cg c]  }d|› �‘Œ	 c}z  }| j                  r|j	                  d	| j                  › �«       d
j                  |«      t        |«      z   S c c}w )Nu   âœ… Post published successfullyú	Channel: zPost: ú	Message: zButton: u    â†’ z	Episode: rn   úFile: rm   )r6   Útitlerƒ   rZ   ÚappendrH   ro   r4   rp   r3   rT   rh   )Úprj   Úlinesrq   s       r.   Úpublished_textr�   Ï   sê   € à)Ø
�A—I‘I—O‘OÐ$Ð%Ø
�—‘�Ðð€Eð
 	‡|‚|Ø�‰�y §¡ Ð/Ô0Ø	‡L�L�8˜AŸG™G˜9 E¨!¯%©%¨Ð1Ô2Ø‡y‚yØ�‰�y §¡ Ð,Ô-Ø	 Q§W¢WÓ-¡W �˜�sŠm WÑ-Ñ-€EØ‡v‚vØ�‰�v˜aŸf™f˜XÐ&Ô'Ø�9‰9�UÓœe F›mÑ+Ð+ùò .s   Â8Dc                  óL   — e Zd ZU ded<   ded<   dZded<    ee¬«      Zd	ed
<   y)ÚDeletedr   r6   r@   rZ   r1   r2   r3   r„   r†   rp   N)r(   r)   r*   r:   r3   r   r†   rp   r,   r-   r.   r‘   r‘   à   s$   … àƒLØƒOØ€Dˆ#ƒNÙ¨Ô-€Eˆ4Ô-r-   r‘   c                ó   — dt        | «      z   S )Nu,   ðŸ“¥ Message received - deleting the postâ€¦rg   ri   s    r.   Údelete_received_textr“   è   s   € Ø9¼EÀ&»MÑIÐIr-   c                ó,  — dd| j                   j                  › �d| j                  › �g}|| j                  D �cg c]  }d|› �‘Œ	 c}z  }| j                  r|j                  d| j                  › �«       dj                  |«      t        |«      z   S c c}w )Nu   ðŸ—‘ Post deleted successfullyrˆ   r‰   rn   rŠ   rm   )r6   r‹   rZ   rp   r3   rŒ   rT   rh   )Údrj   rŽ   rq   s       r.   Údeleted_textr–   ì   sŒ   € Ø-°¸1¿9¹9¿?¹?Ð:KÐ/LÐPYÐZ[×ZfÑZfÐYgÐNhÐi€EØ	 Q§W¢WÓ-¡W �˜�sŠm WÑ-Ñ-€EØ‡v‚vØ�‰�v˜aŸf™f˜XÐ&Ô'Ø�9‰9�UÓœe F›mÑ+Ð+ùò .s   ¹Bc                ó°   — dj                  | xs dj                  «       «      } d| d d › �|�|j                  rd|j                  › �ndz   t        |«      z   S )NrO   rt   u   â�Œ Deletion failed: ru   r}   r1   r~   rv   s     r.   Údelete_failed_textr˜   ô   sr   € Ø�X‰X�vÒ1Ð!1×8Ñ8Ó:Ó;€FØ" 6¨$¨3 < .Ð1ÐQWÐQcÐhn×hsÒhs°xÀÇÁ¸}Ñ5MÐy{Ñ|ô  @Eð  FLó  @Mñ  Mð  Mr-   c                ó.   — | dk  r|  dz
  S t        | «      S )us   The id the database keeps for a channel, from the id as Telegram apps write it (-100â€¦); a bare id stays as it is.r<   l    J)£)Úabs©Úvalues    r.   Úchannel_id_ofr�   ú   s!   € à).Ð2DÒ)DˆEˆ6Ð%Ñ%ÐTÌ#ÈeË*ÐTr-   c                ó   — | dkD  rd| z
  S | S )zjA chat id the way Telethon reports it in events: a bare channel id gets its -100 in front, the rest stays.r   r<   r,   r›   s    r.   Úmarked_chat_idrŸ   ÿ   s   € à).°ªÐ Ñ%Ð=¸Ð=r-   c                ó¶   — dj                  t        j                  ddt        j                  d| xs d«      j                  «       «      j                  «       «      S )zSLower case, no punctuation, single spaces: "Black-Clover_S2!" -> "black clover s2".rO   z[\W_]+ÚNFKCr1   )rT   r>   ÚsubÚunicodedataÚ	normalizeÚcasefoldrU   )rG   s    r.   Únormr¦     sB   € à�8‰8”B—F‘F˜9 c¬;×+@Ñ+@ÀÈÊÐQSÓ+T×+]Ñ+]Ó+_Ó`×fÑfÓhÓiÐir-   c                ó8   — t        j                  dd| xs d«      S )Nz\.[A-Za-z0-9]{2,5}$r1   )r>   r¢   ©r   s    r.   Ú	strip_extr©   	  s   € Ü�6‰6Ð(¨"¨dªj°bÓ9Ð9r-   c                óH   — | j                  «       j                  d«      rdS dS )Nr4   r6   )ÚlowerÚ
startswithr¨   s    r.   Ú	_variabler­     s    € ØŸ
™
›×/Ñ/°	Ô:ˆ9ÐIÀ	ÐIr-   c                óZ   ‡— t        ˆfd„t        j                  | xs d«      D «       «      S )zODoes the template have {episode} (or {channel}) in it, by any of its spellings?c              3  óX   •K  — | ]!  }t        |j                  d «      «      ‰k(  –— Œ# y­w)r   N)r­   rA   )Ú.0rD   Úvariables     €r.   Ú	<genexpr>zuses.<locals>.<genexpr>  s&   øè ø€ Ð`Ñ?_¸!Œy˜Ÿ™ ›Ó)¨XÕ5Ñ?_ùs   ƒ'*r1   )ÚanyÚ_VAR_RXÚfinditer)Útemplater±   s    `r.   Úusesr·     s&   ø€ äÓ`¼w×?OÑ?OÐPXÒP^Ð\^Ô?_Ó`Ó`Ð`r-   c                óÂ   — t         j                  | xs d«      D �cg c]8  }t        j                  |j	                  d«      «      rŒ(|j	                  d«      ‘Œ: c}S c c}w )zvThe {words} of a template that are none of the variables ({ep}, {number} ...): they would end up in the post as typed.r1   r   )Ú_ANY_BRACES_RXrµ   r´   r_   rA   )r¶   rD   s     r.   Úunknown_variablesrº     sM   € ä .× 7Ñ 7¸ºÀBÔ GÓmÑ G˜1Ìw×O`ÑO`Ðab×ahÑahÐijÓakÕOlˆA�G‰G�A�JÐ GÑmÐmùÒms   œ)AÁAc                ó>   ‡— t         j                  ˆfd„| xs d«      S )z`The template with its variables replaced (values: {"episode": "05", "channel": "Black Clover"}).c           	     ój   •— t        ‰j                  t        | j                  d«      «      d«      «      S )Nr   r1   )r2   Úgetr­   rA   )rD   Úvaluess    €r.   Ú<lambda>zfill.<locals>.<lambda>  s"   ø€ ¤ V§Z¡Z´	¸!¿'¹'À&»/Ó0JÈBÓ%OÔ!Pr-   r1   )r´   r¢   )r¶   r¾   s    `r.   ÚfillrÀ     s   ø€ ä�;‰;ÓPÐRZÒR`Ð^`ÓaÐar-   c                ó¬  — t        | xs d«      }t        j                  |«      D �cg c]]  }|j                  «       |j	                  «       t        t        |j                  t        |j                  d«      «      d«      «      «      f‘Œ_ }}|s| xs dt        |xs g «      fS t        |t        |«      |«      \  }}t        |«      t        |«      fS c c}w )zÂ(text, entities) of the message template with its variables replaced; bold, links and the other formatting stay
    on the words they covered (offsets are UTF-16 units, as Telegram counts them).r1   r   )r   r´   rµ   ÚstartÚendr2   r½   r­   rA   r†   r   r   r   r   )rG   Úentitiesr¾   ÚsurrD   ÚreplsÚnew_surÚnew_entss           r.   Ú	fill_textrÉ   !  sÄ   € ô ˜š
 Ó
#€Cô ×!Ñ! #Ô&óá&ˆAð 
�‰‹�A—E‘E“Gœ]¬3¨v¯z©z¼)ÀAÇGÁGÈFÃOÓ:TÐVXÓ/YÓ+ZÓ[Ò\Ø&ð 
ð ñ ØŠz�rœ4 ¢¨BÓ/Ð/Ð/Ü/°´[ÀÓ5JÈEÓRÑ€GˆXÜ˜Ó!¤<°Ó#9Ð9Ð9ùòs   §A"Cc                  ób   — e Zd ZU eZded<    ee¬«      Zded<   e	Z
ded<   dZded	<   dZded
<   y)Ú	Templatesr2   rG   r„   r†   rÄ   ÚbuttonFÚboolÚcustom_textÚcustom_buttonN)r(   r)   r*   ÚDEFAULT_TEXTrG   r:   r   r†   rÄ   ÚDEFAULT_BUTTONrÌ   rÎ   rÏ   r,   r-   r.   rË   rË   /  s9   … à€Dˆ#ÓÙ¨4Ô0€HˆdÓ0Ø €FˆCÓ Ø€K�ÓØ€M�4Ôr-   rË   c              ƒ  ó¾  K  — | j                  t        «      ƒ d {  –—† xs i }t        «       }t        |j	                  d«      t
        «      rH|d   j                  «       r5|d   t        |j	                  d«      xs g «      dc|_        |_	        |_
        t        |j	                  d«      t
        «      r%|d   j                  «       r|d   dc|_        |_        |S 7 Œ¿­w)NrG   rÄ   TrÌ   )Úget_settingÚSETTING_KEYrË   Ú
isinstancer½   r2   r=   r†   rG   rÄ   rÎ   rÌ   rÏ   )ÚdbrC   Úts      r.   Úload_templatesrØ   8  s³   è ø€ Ø—‘œ{Ó+×
+Ò
1¨r€CÜ‹€AÜ�#—'‘'˜&“/¤3Ô'¨C°©K×,=Ñ,=Ô,?Ø,/°©K¼¸c¿g¹gÀjÓ>QÒ>WÐUWÓ9XÐZ^Ð)ˆŒ�”
˜AœMÜ�#—'‘'˜(Ó#¤SÔ)¨c°(©m×.AÑ.AÔ.CØ$'¨¡M°4Ð!ˆŒ�!”/Ø€Hð ,ús   ‚C›CœC C)rG   rÄ   rÌ   c             ƒ  óæ   K  — t        | j                  t        «      ƒ d{  –—† xs i «      }|�|t        |xs g «      c|d<   |d<   |�||d<   | j	                  t        |«      ƒ d{  –—†  y7 ŒL7 Œ­w)zfKeep a new message text (with its formatting) and / or button name; what is not given stays as it was.NrG   rÄ   rÌ   )ÚdictrÓ   rÔ   r†   Úset_setting)rÖ   rG   rÄ   rÌ   rC   s        r.   Úsave_templatesrÜ   B  sr   è ø€ ä
�R—^‘^¤KÓ0×0Ò6°BÓ
7€CØÐØ'+¬T°(².¸bÓ-AÐ$ˆˆF‰�S˜‘_ØÐØˆˆH‰Ø
�.‰.œ cÓ
*×*Ñ*ð 1øð
 +ús"   ‚A1 A-¡AA1Á'A/Á(A1Á/A1F)rG   rÌ   c             ƒ  ó\  K  — t        | j                  t        «      ƒ d{  –—† xs i «      }|r$|j                  dd«       |j                  dd«       |r|j                  dd«       |r| j	                  t        |«      ƒ d{  –—†  y| j                  t        «      ƒ d{  –—†  y7 Œ…7 Œ%7 Œ	­w)z9Go back to the default message text and / or button name.NrG   rÄ   rÌ   )rÚ   rÓ   rÔ   ÚpoprÛ   Údelete_setting)rÖ   rG   rÌ   rC   s       r.   Úreset_templatesrà   L  s‹   è ø€ ä
�R—^‘^¤KÓ0×0Ò6°BÓ
7€CÙØ�‰�˜ÔØ�‰�
˜DÔ!ÙØ�‰�˜$ÔÙ
Ø�n‰nœ[¨#Ó.×.Ñ.à×Ñ¤Ó,×,Ñ,ð 1øð 	/øà,ús4   ‚B, B&¡A!B,ÂB(ÂB,Â B*Â!B,Â(B,Â*B,c                óÈ   — t        | j                  | j                  |«      \  }}dj                  t	        | j
                  |«      j                  «       «      dt         }|||fS )z@(text, entities, button name) of a post made from the templates.rO   N)rÉ   rG   rÄ   rT   rÀ   rÌ   rU   ÚBUTTON_SHOWN)r×   r¾   rG   rÄ   rH   s        r.   Úrender_postrã   Z  sR   € ä˜qŸv™v q§z¡z°6Ó:�N€Dˆ(Ø�H‰H”T˜!Ÿ(™( FÓ+×1Ñ1Ó3Ó4°]´lÐC€EØ�˜5Ð Ð r-   c                ó    — t        | dd «      d uS )NÚdocument©Úgetattr©Úmsgs    r.   Úis_filerê   b  s   € Ü�3˜
 DÓ)°Ð5Ð5r-   c           	     ó  — t        | t        j                  «      sy| j                  D ]W  }|j                  D ]F  }t        |«      }|sŒt        j                  t        |dd«      xs d«      sŒ6|j                  |fc c S  ŒY y)uh   (text, link) of the first link button called "Share URL" (the ðŸ”� in front does not matter), else None.NrG   r1   )
rÕ   r   ÚReplyInlineMarkupÚrowsÚbuttonsr   Ú	_SHARE_RXr?   rç   rG   )ÚmarkupÚrowÚbro   s       r.   Úshare_buttonró   f  sk   € ä�fœe×5Ñ5Ô6ØØ�{Œ{ˆØ—”ˆAÜ˜Q“-ˆCÚ”y×'Ñ'¬°°6¸2Ó(>Ò(DÀ"ÕEØ—v‘v˜s�{Ô"ñ ð ð
 r-   c                ó"  — | xs dj                  «       } t        j                  | «      }|s| S t        j                  d|j                  d«      «      }|r|j                  d«      nd}d|v rt        |«      }t        j                  d|«      r|S dS )aA  The link the file store means. A "Share URL" button usually opens Telegram's share sheet for the file link
    (t.me/share/url?url=<file link>): the file link is what a post's button needs, so that wrapper is taken off.
    A share-sheet link that holds no link of its own gives "" (there is nothing to download from it).r1   z(?:^|&)url=(.*?)(?=&text=|$)r   Ú%z(?i)^(https?://|tg://)\S+$)r=   Ú_SHARE_LINK_RXrB   r>   r?   rA   r	   )ro   rD   Úinnerrƒ   s       r.   Ú	file_linkrø   r  s�   € ð Š9�"×
Ñ
Ó
€CÜ×Ñ˜SÓ!€AÙØˆ
Ü�I‰IÐ5°q·w±w¸q³zÓB€EÙ"ˆ5�;‰;�qŒ>¨€DØ
ˆd�{Ü�t‹}ˆÜ—8‘8Ð9¸4Ô@ˆ4ÐHÀbÐHr-   c                óF   — t        | dd «      }|�t        |dd «      xs dS dS )Nr3   r   r1   ræ   )ré   Úfs     r.   Úname_ofrû   �  s-   € Ü��V˜TÓ"€AØ/0¨}ŒG�A�v˜tÓ$Ò*¨ÐDÀ"ÐDr-   c                  ó0   — e Zd ZU dZded<   ded<   ded<   y)	ÚSeenzmOne look at the post: the message as one account saw it, the Share URL button on it (if any), and who looked.r   ré   úOptional[tuple]Úfoundr2   ÚviaN)r(   r)   r*   r+   r:   r,   r-   r.   rý   rý   ‡  s   … áwà	ƒHØÓØ	„Hr-   rý   c                  óJ   — e Zd ZU dZdZded<   dZded<    ee¬«      Z	d	ed
<   y)ÚLookzDWhat survives from one look to the next while the button is awaited.Nr   ÚreaderFrÍ   Úreader_failedr„   rÚ   rp   )
r(   r)   r*   r+   r  r:   r  r   rÚ   rp   r,   r-   r.   r  r  �  s(   … áNà€FˆCÓØ€M�4ÓÙ¨Ô-€Eˆ4Ô-r-   r  c                  óö   — e Zd Zej                  ej                  dœdd„Zedd„«       Z	dd„Z
dd„Zdd„Zdd„Zdd„Zd d	„Zd!d
„Zd"d„Zddœd#d„Zd$d„Zd%d„Zed&d„«       Zd'd„Zd(d„Zd)d„Zd*d„Zd)d„Zd+d„Zy),ÚAutoPublisher)ÚsleepÚclockc               ó€   — || _         ||c| _        | _        t        j                  «       | _        t        «       | _        y rL   )ÚctxÚ_sleepÚ_clockÚasyncioÚLockÚlockr   Ú_handled)Úselfr
  r  r  s       r.   Ú__init__zAutoPublisher.__init__š  s/   € ØˆŒØ#(¨%Ð ˆŒ�T”[Ü—L‘L“NˆŒ	Ü2=³-ˆ�r-   c                óB   — | j                   j                  j                  S rL   )r
  ÚcfgÚauto_publish)r  s    r.   ÚenabledzAutoPublisher.enabled   s   € à�x‰x�|‰|×(Ñ(Ð(r-   c           
   ƒ  ó’  K  — t        |dd«      }t        |«      }|�dn
t        |«      }|€|�dn
t        |«      }|€|€|�|j                  | j
                  v ryd| j
                  |j                  <   t        | j
                  «      t        kD  r9| j
                  j                  d¬«       t        | j
                  «      t        kD  rŒ9|�| j                  ||«      ƒ d{  –—†  y|�| j                  ||«      ƒ d{  –—†  y| j                  |t        |«      «      ƒ d{  –—†  | j                  4 ƒd{  –—†  	 | j                  |«      ƒ d{  –—† }t        ||«      }ddd«      ƒd{  –—†  | j                  ||«      ƒ d{  –—†  y7 Œ«7 Œ�7 Œn7 Œ[7 ŒC# t         $ r5}t"        j%                  d|«       t'        t)        |«      |«      }Y d}~Œnd}~wt*        $ rQ}t"        j-                  d«       t'        dt/        |«      j0                  › dt)        |«      dd	 › d
�|«      }Y d}~ŒÆd}~ww xY w7 ŒÂ# 1 ƒd{  –—†7  sw Y   ŒÒxY w7 Œ¿­w)z:A message of the autorename bot in the communication chat.Úraw_textNF)Úlastzautomatic publishing: %szautomatic publishing failedú%something went wrong inside the bot (ú: é–   Ú))rç   rW   r`   re   Úidr  ÚlenÚKEEP_NOTICESÚpopitemÚ_removeÚ_lookupÚ_answerr{   r  Úpublishr�   r'   ÚlogÚinfor€   r2   Ú	ExceptionÚ	exceptionÚtyper(   )	r  ÚeventrG   rj   ÚremovalÚlookupÚdonerú   Úes	            r.   ÚhandlezAutoPublisher.handle¥  sý  è ø€ ä�u˜j¨$Ó/ˆÜ˜dÓ#ˆØ Ð,‘$Ô2EÀdÓ2KˆØÐ+¨wÐ/B‘ÔHYÐZ^ÓH_ˆØˆN˜w˜°6°>ÀeÇhÁhÐRV×R_ÑR_ÑF_ØØ"&ˆ�‰�e—h‘hÑÜ�$—-‘-Ó ¤<Ò/Ø�M‰M×!Ñ! uÐ!Ô-ô �$—-‘-Ó ¤<Ó/àÐØ—,‘,˜u gÓ.×.Ð.ØØÐØ—,‘,˜u fÓ-×-Ð-ØØ�l‰l˜5¤-°Ó"7Ó8×8Ð8Ø—9—9“9ðxØ!Ÿ\™\¨&Ó1×1�Ü% d¨FÓ3�÷ —9ð �l‰l˜5 $Ó'×'Ñ'ð! /øð .øà8øØøà1ùäò 3Ü—‘Ð3°QÔ7Ü"¤3 q£6¨6Ó2•ûÜò xÜ—‘Ð;Ô<Ü"Ð%JÌ4ÐPQË7×K[ÑK[ÐJ\Ð\^Ô_bÐcdÓ_eÐfjÐgjÐ_kÐ^lÐlmÐ#nÐpvÓw•ûðxúð ø—9—9‘9úð 	(úsÙ   ‚CIÃIÃFÃ IÃ<FÃ=#IÄ FÄ!IÄ5FÄ6IÄ9H0Ä;FÅFÅFÅ IÅ+H.Å,IÆIÆIÆIÆIÆIÆFÆ	H+Æ+GÇ	H0ÇH+ÇAH&È!H0È&H+È+H0È.IÈ0IÈ6H9È7IÈ>Ic           
   ƒ  óˆ  K  — | j                  |t        |«      «      ƒ d{  –—†  | j                  4 ƒd{  –—†  	 t        | j	                  |«      ƒ d{  –—† |«      }ddd«      ƒd{  –—†  | j                  |«      ƒ d{  –—†  y7 Œh7 ŒU7 Œ8# t
        $ r5}t        j                  d|«       t        t        |«      |«      }Y d}~Œjd}~wt        $ rQ}t        j                  d«       t        dt        |«      j                  › dt        |«      dd › d�|«      }Y d}~ŒÂd}~ww xY w7 Œ¾# 1 ƒd{  –—†7  sw Y   ŒÎxY w7 Œ»­w)zLA request to delete a published post: "received", then "deleted" or why not.Nzautomatic deletion: %szautomatic deletion failedr  r  r  r  )r$  r“   r  r–   Údeleter'   r&  r'  r˜   r2   r(  r)  r*  r(   ©r  r+  rj   rG   rú   r/  s         r.   r"  zAutoPublisher._removeÃ  s	  è ø€ à�l‰l˜5Ô"6°vÓ">Ó?×?Ð?Ø—9—9“9ðÜ#¨$¯+©+°fÓ*=×$=¸vÓF�÷ —9ð �l‰l˜5 $Ó'×'Ñ'ð 	@øØøà$=ùÜò :Ü—‘Ð1°1Ô5Ü)¬#¨a«&°&Ó9•ûÜò Ü—‘Ð9Ô:Ü)Ð,QÔRVÐWXÓRY×RbÑRbÐQcÐceÔfiÐjkÓflÐmqÐnqÐfrÐesÐstÐ*uÐw}Ó~•ûðúð ø—9—9‘9úð 	(ús¨   ‚E¡B
¢E¶B·EºD+¼BÁB
Á	BÁEÁ*D)Á+EÂE ÂEÂEÂBÂ	D&Â+C	ÃD+Ã	D&ÃAD!ÄD+Ä!D&Ä&D+Ä)EÄ+D=Ä1D4Ä2D=Ä9Ec           
   ƒ  ó^  K  — | j                  |t        |«      «      ƒ d{  –—†  | j                  4 ƒd{  –—†  	 t        g | j	                  |«      ƒ d{  –—† ¢|‘­Ž }ddd«      ƒd{  –—†  | j                  |«      ƒ d{  –—†  y7 Œi7 ŒV7 Œ8# t
        $ r}t        t        |«      |«      }Y d}~ŒTd}~wt        $ rQ}t        j                  d«       t        dt        |«      j                  › dt        |«      dd › d�|«      }Y d}~Œ¬d}~ww xY w7 Œ¨# 1 ƒd{  –—†7  sw Y   Œ¸xY w7 Œ¥­w)zdA request for the Share URL of a file in the Database Channel: "received", then the link or why not.Nz looking for the share url failedr  r  r  r  )r$  rk   r  rr   Ú
share_linkr'   rx   r2   r(  r&  r)  r*  r(   r3  s         r.   r#  zAutoPublisher._lookupÑ  sÿ   è ø€ à�l‰l˜5Ô"4°VÓ"<Ó=×=Ð=Ø—9—9“9ð}Ü&ÐN¨d¯o©o¸fÓ.E×(EÐNÀvÒN�÷ —9ð �l‰l˜5 $Ó'×'Ñ'ð 	>øØøà(EùÜò 8Ü'¬¨A«°Ó7•ûÜò }Ü—‘Ð@ÔAÜ'Ð*OÔPTÐUVÓPW×P`ÑP`ÐOaÐacÔdgÐhiÓdjÐkoÐloÐdpÐcqÐqrÐ(sÐu{Ó|•ûð}úð ø—9—9‘9úð 	(ús¨   ‚D-¡B¢D-¶B·D-ºD¼BÁBÁ	BÁ D-Á+DÁ,D-ÂD+ÂD-ÂD-ÂBÂ	DÂB4Â/DÂ4DÃ ADÄDÄDÄDÄD-ÄD(ÄDÄD(Ä$D-c              ƒ  óx  K  — |j                   st        d«      ‚| j                  | j                  «       ƒ d{  –—† t	        |j                   ¬«      «      ƒ d{  –—† }t        t        |j                  d   «      «      }|st        d|j                  d   dd › �«      ‚||j                  dk(  rdgfS g fS 7 Œ}7 Œ^­w)	uÈ   (file link, notes) of the ðŸ”� Share URL button of that Database Channel post - waited for like when publishing, the
        userbot looking too when the bot cannot see it. Nothing is posted anywhere.uX   The message does not say which post of the Database Channel: it has no â€œPost:â€� line.N)r8   r   õ@   The ðŸ”� Share URL button of that post has a link I cannot use: r  Úuserbotú2The Share URL button was read through the userbot.)	r8   r'   Ú_wait_for_shareÚ_database_channelr0   r   rø   rÿ   r   )r  rj   Úseenrƒ   s       r.   r5  zAutoPublisher.share_linkÞ  s½   è ø€ ð �~Š~ÜÐtÓuÐuØ×)Ñ)°×0FÑ0FÓ0H×*HÌ&ÐY_×YgÑYgÔJhÓi×iˆÜœ	 $§*¡*¨Q¡-Ó0Ó1ˆÙÜÐ\Ð]a×]gÑ]gÐhiÑ]jÐkoÐloÐ]pÐ\qÐrÓsÐsØÐPT×PXÑPXÐ\eÒPeÐKÐLÐnÐnÐkmÐnÐnð	 +IøÐiús"   ‚6B:¸B6¹ B:ÁB8ÁAB:Â8B:c              ƒ  ó¢   K  — 	 |j                  |d d¬«      ƒ d {  –—†  y 7 Œ# t        $ r }t        j                  d|«       Y d }~y d }~ww xY w­w)NF)Ú
parse_modeÚlink_previewz.could not answer in the communication chat: %s)Úreplyr(  r&  Úwarning)r  r+  rG   r/  s       r.   r$  zAutoPublisher._answeré  sG   è ø€ ð	MØ—+‘+˜d¨tÀ%�+ÓH×HÒHùÜò 	MÜ�K‰KÐHÈ!×LÑLûð	Müs6   ‚A„# ›!œ#  A¡# £	A¬AÁAÁAÁAc              ƒ  óz  K  — | j                   syd| j                  j                  j                  fd| j                  j                  j                  ffD ]3  \  }}	 | j                  j
                  j                  |«      ƒ d{  –—†  Œ5 y7 Œ# t        $ r"}t        j                  d|||«       Y d}~Œ_d}~ww xY w­w)z6At start-up: say in the log what the bot cannot reach.NÚCOMMUNICATION_CHATÚ
DB_CHANNELz6cannot reach %s %s: %s (add the bot there as an admin))
r  r
  r  Ú	comm_chatÚ
db_channelÚclientÚget_input_entityr(  r&  rA  )r  r   r9   r/  s       r.   ÚcheckzAutoPublisher.checkï  sž   è ø€ à�|Š|ØØ/°·±·±×1GÑ1GÐHÈ<ÐY]×YaÑYa×YeÑYe×YpÑYpÐJqÓr‰IˆD�#ðdØ—h‘h—o‘o×6Ñ6°sÓ;×;Ñ;ñ sà;ùÜò dÜ—‘ÐTÐVZÐ\_Ðab×cÑcûðdüsB   ‚AB;Á(BÂBÂBÂB;ÂBÂ	B8ÂB3Â.B;Â3B8Â8B;c              ƒ  óâ  K  — | j                  |«      ƒ d {  –—† }t        | j                  j                  «      ƒ d {  –—† }|j                  s7t        |j                  d«      st        |j                  d«      rt        d«      ‚| j                  «       ƒ d {  –—† }| j                  ||«      ƒ d {  –—† }t        |j                  «      xs |j                  }|j                  €p|j                  rdt        |j                  «      rOt        t!        |«      «      t        t!        |j                  «      «      k7  rt        d|› d|j                  › d�«      ‚t#        t%        |j&                  d   «      «      }|st        d|j&                  d   d d › �«      ‚| j)                  |||||«      ƒ d {  –—† }|j*                  d	k(  r|j,                  j/                  d
«       |S 7 �ŒÊ7 �Œ¦7 �ŒN7 �Œ77 Œ<­w)Nr4   uv   The message has no â€œEpisode:â€� line, and the text or the button name of the post uses {episode} (see /autopublish).u7   The latest post of the Database Channel is the file â€œu   â€�, not â€œu.   â€� that was reported - nothing was published.r   r7  r  r8  r9  )Ú_target_channelrØ   r
  rÖ   r4   r·   rG   rÌ   r'   r;  r:  rû   ré   r3   r8   r¦   r©   r   rø   rÿ   Ú_postr   rp   rŒ   )	r  rj   ÚchÚ	templatesÚdbchr<  r   rƒ   r.  s	            r.   r%  zAutoPublisher.publishú  s»  è ø€ Ø×'Ñ'¨Ó/×/ˆÜ(¨¯©¯©Ó5×5ˆ	Ø�~Š~¤4¨	¯©¸	Ô#BÄdÈ9×K[ÑK[Ð]fÔFgÜð Ióð ð ×+Ñ+Ó-×-ˆØ×)Ñ)¨$°Ó7×7ˆÜ�t—x‘xÓ Ò/ F§K¡KˆØ�>‰>Ð! f§k¢k´g¸d¿h¹hÔ6GÌDÔQZÐ[_ÓQ`ÓLaÔeiÔjsÐtz×tÑtó  kAó  fBò  MBÜØIÈ$ÈÈ|Ð\b×\gÑ\gÐ[hð i)ð )óð ô œ	 $§*¡*¨Q¡-Ó0Ó1ˆÙÜÐ\Ð]a×]gÑ]gÐhiÑ]jÐkoÐloÐ]pÐ\qÐrÓsÐsØ—Z‘Z  I¨v°t¸TÓB×BˆØ�8‰8�yÒ Ø�J‰J×ÑÐRÔSØˆð) 0ùØ5ùð
 .ùØ7ùð CúsW   ‚G/—G!˜&G/¾G$¿AG/ÂG'ÂG/Â3G*Â4C<G/Æ0G-Æ11G/Ç$G/Ç'G/Ç*G/Ç-G/c           	   ƒ  ó  K  — | j                   | j                   j                  }}| j                  |d¬«      ƒ d{  –—† }|j                  }|st	        d«      ‚t        |«      }|j                  |j                  |«      ƒ d{  –—† }t        |||j                  ¬«      }|€^	 t        |j                  ||g«      ƒ d{  –—† }	||	vrt	        d|› d	|j                  › d
�«      ‚|j                   j#                  d«       |S |j$                  dk7  s|j&                  rt	        d|› d	|j                  › d�«      ‚|j(                  �#|j(                  j+                  |«      ƒ d{  –—† nd\  }}	 t-        |j                  ||g|«      ƒ d{  –—† }	 |�|j/                  «       ƒ d{  –—†  	 ||j0                  vrSt3        |j(                  |j4                  |j6                  |j8                  ¬«      }t	        d|j                  › d|› d�«      ‚|j;                  |j<                  |g«      ƒ d{  –—†  |j>                  r|j                   j#                  d«       |S 7 �Œ7 �ŒØ7 �Œ # t        j                  $ r)}
t	        d|j                  › dt        |
«      › �«      ‚d}
~
ww xY w7 �Œ?7 �Œ# t        j                  $ r)}
t	        d|j                  › dt        |
«      › �«      ‚d}
~
ww xY w7 �ŒB# |�|j/                  «       ƒ d{  –—†7   w w xY w7 ŒÞ­w)zàDelete a post this bot made by itself: in its channel (the userbot takes over where Telegram refuses the bot, as in
        My posts) and in My posts. A post that is gone from the channel and from My posts counts as deleted.T©ÚdeletingNuM   The message does not say which post to delete: it has no â€œMessage:â€� line.)r6   rZ   r3   u#   I could not look at the post in â€œõ   â€�: zMessage u    of â€œuc   â€� is not one of the posts this bot published (My posts does not have it), so I did not delete it.z=The post was already gone from the channel and from My posts.Úbotui   â€� was not published automatically, so I did not delete it. Delete it from My posts if you want it gone.)NNu+   Telegram refused to look at the post in â€œ)Ú	bot_errorÚfallback_errorÚtriedu'   Telegram did not delete the post in â€œu   â€�. z It is still in My posts.z9The bot was not allowed to delete it, so the userbot did.) r
  rÖ   rK  rZ   r'   r   Úfind_by_messager  r‘   r3   r   rG  r
   ÚRPCErrorr‹   r   rp   rŒ   ÚsourceÚ
created_byr8  Úfallback_forr   ÚcloseÚgoner!   rU  rV  Útried_userbotÚforget_postsÚ
channel_idÚ
by_userbot)r  rj   r
  rÖ   rM  ÚmidÚpeerrN   r.  r^  r/  ÚfallbackÚdeleterÚoutÚwhys                  r.   r2  zAutoPublisher.delete  sà  è ø€ ð —(‘(˜DŸH™HŸK™KˆRˆØ×'Ñ'¨¸Ð'Ó>×>ˆØ×ÑˆÙÜÐiÓjÐjÜ�r‹{ˆØ×'Ñ'¨¯©¨sÓ3×3ˆÜ˜r¨c¸¿¹ÔDˆØˆ<ðeÜ% c§j¡j°$¸¸Ó>×>�ð ˜$‰ÜØ˜s˜e 7¨2¯8©8¨*ð 5.ð .óð ð �J‰J×ÑÐ]Ô^ØˆKØ�;‰;˜%Ò 4§?¢?ÜØ˜3˜%˜w r§x¡x jð 1?ð ?óð ð CFÇ+Á+ÐBY #§+¡+×":Ñ":¸2Ó">×>Ð>Ð_kÑˆ�'ð	&Ü$ S§Z¡Z°¸°u¸hÓG×G‰Cð Ð"Ø—m‘m“o×%Ñ%Ø�c—h‘hÑÜ% c§k¡k¸S¿]¹]Ð[^×[mÑ[mÐux÷  vGñ  vGô  HˆCÜÐCÀBÇHÁHÀ:ÈUÐSVÐRWÐWpÐqÓrÐrØ�o‰o˜dŸo™o°¨uÓ5×5Ð5Ø�>Š>Ø�J‰J×ÑÐYÔZØˆðM ?ùð
 4ùð ?úÜ—?‘?ò eÜÐ CÀBÇHÁHÀ:ÈUÔS^Ð_`ÓSaÐRbÐcÓdÐdûðeúð ?ùàGúÜ�‰ò 	iÜÐGÈÏÉÀzÐQVÔWbÐcdÓWeÐVfÐgÓhÐhûð	iúð &úð Ð"Ø—m‘m“o×%Ò%ð #úð
 	6úsÒ   ‚9L»I¼ALÂIÂLÂ#I! Â>IÂ?I! ÃBLÅ!J Å"	LÅ,J& ÆJ#Æ	J& ÆLÆ#K%Æ$BLÈ*L	È+.LÉLÉI! É!JÉ4$JÊJÊLÊ#J& Ê&K"Ê9$KËK"Ë"K( Ë%LË(LË>LË?LÌLFrQ  c             ƒ  ó  K  — |j                   €=|j                  rt        d|j                  › d�«      ‚|rt        d«      ‚t        d«      ‚t        |j                   «      }|t        | j                  j
                  j                  «      k(  rt        |rd«      ‚d«      ‚| j                  j                  j                  |«      ƒ d{  –—† }|�|j                  s t        d|j                   › d	|rd
nd› d�«      ‚|S 7 Œ4­w)zgThe channel the message names: connected to this bot, switched on, and not the Database Channel itself.Nu   The message names â€œuD   â€� as the channel, which is not a channel id (like -1001234567890).uU   The message does not say which channel the post is in: it has no â€œChannel:â€� line.u‘   The message does not say which channel to post in: it has no â€œChannel:â€� line (the Channel option of the format the episode was renamed with).z�The channel in the message is the Database Channel itself - this bot only deletes the posts it made in the channel of the series.zlThe channel in the message is the Database Channel itself - the post has to go to the channel of the series.zThe channel zf is not connected to this bot (/channels lists the connected ones, /addchannel adds one), so I cannot r2  rN   z there.)
r6   r7   r'   r�   r
  r  rF  rÖ   Úget_channelÚactive)r  rj   rR  ÚcidrM  s        r.   rK  zAutoPublisher._target_channel=  s  è ø€ à�>‰>Ð!Ø×!Ò!ÜÐ 5°f×6HÑ6HÐ5Ið  JNð  Oó  Pð  PÙÜÐuÓvÐvÜðSóð ô ˜FŸN™NÓ+ˆØ”- §¡§¡× 7Ñ 7Ó8Ò8Üáð Tóð ð Dóð ð
 —8‘8—;‘;×*Ñ*¨3Ó/×/ˆØˆ:˜RŸYšYÜØ˜vŸ~™~Ð.ð /Ù+3™x¸Ð@ÀðIóð ð ˆ	ð 0ús   ‚CC?ÃC=Ã	5C?c              ƒ  óü  K  — t        | j                  j                  j                  «      }| j                  j                  j                  |«      ƒ d {  –—† }|�|S 	 | j                  j                  j                  t        j                  |«      «      ƒ d {  –—† }t        |t        j                  «      st        d«      ‚|S 7 Œp7 Œ-# t        $ r'}t        dt        |«      j                  › d�«      ‚d }~ww xY w­w)Nz$I cannot open the Database Channel (z). Is the bot an admin there?zDB_CHANNEL is not a channel.)r�   r
  r  rF  rÖ   rj  rG  Ú
get_entityr   ÚPeerChannelr(  r'   r*  r(   rÕ   ÚChannel)r  rl  rM  Úentr/  s        r.   r;  zAutoPublisher._database_channelW  sÎ   è ø€ Ü˜DŸH™HŸL™L×3Ñ3Ó4ˆØ—8‘8—;‘;×*Ñ*¨3Ó/×/ˆØˆ>ØˆIð	rØŸ™Ÿ™×2Ñ2´5×3DÑ3DÀSÓ3IÓJ×JˆCô ˜#œuŸ}™}Ô-ÜÐ8Ó9Ð9Øˆ
ð 0øð KùÜò 	rÜÐ@ÄÀaÃ×AQÑAQÐ@RÐRoÐpÓqÐqûð	rüsH   ‚AC<ÁCÁC<Á;C	 ÂCÂC	 Â(C<ÃC	 Ã		C9Ã"C4Ã4C9Ã9C<c              ƒ  óš  K  — t        t        | j                  j                  j                  «      }| j                  «       }t        «       }d}	 	 | j                  ||j                  || j                  «       |z
  t        k\  ¬«      ƒ d{  –—† }|�=|j                  r1||j                  �#|j                  j                  «       ƒ d{  –—†  S S |�=t        |j                  «      s(t        d| j!                  |j                  «      › d�«      ‚| j                  «       |z
  |k\  rn| j#                  t$        «      ƒ d{  –—†  Œò	 |j                  �#|j                  j                  «       ƒ d{  –—†  	 t        | j'                  |||«      «      ‚7 �Œ7 ŒÉ7 ŒV7 Œ(# |j                  �$|j                  j                  «       ƒ d{  –—†7   w w xY w­w)zÖLook at the Database Channel post until it shows its Share URL button. The file-store bot adds that button a
        few seconds after the post appears, so "no button" is only an answer once the whole wait is over.N)Úuserbot_tooz0The post of the Database Channel is not a file (z"), so there is nothing to publish.)Úmaxr   r
  r  Úshare_url_waitr  r  Ú_lookr8   ÚBOT_ONLY_SECONDSrÿ   r  r]  rê   ré   r'   Ú_kindr  ÚPOLL_SECONDSÚ
_no_button)r  rM  rj   ÚwaitÚstartedÚlookr<  s          r.   r:  zAutoPublisher._wait_for_sharee  s�  è ø€ ô Ô% t§x¡x§|¡|×'BÑ'BÓCˆØ—+‘+“-ˆÜ‹vˆØ#ˆð	*ØØ!ŸZ™Z¨¨F¯N©N¸DÈdÏkÉkËmÐ^eÑNeÔiyÑNy˜ZÓz×z�ØÐ#¨¯
ª
Øð �{‰{Ð&Ø—k‘k×'Ñ'Ó)×)Ñ)ð 'ð Ð#¬G°D·H±HÔ,=Ü!Ð$TÐUY×U_ÑU_Ð`d×`hÑ`hÓUiÐTjð  kMð  #Nó  Oð  OØ—;‘;“= 7Ñ*¨dÒ2ØØ—k‘k¤,Ó/×/Ð/ð ð ð �{‰{Ð&Ø—k‘k×'Ñ'Ó)×)Ñ)Ü�d—o‘o d¨D°$Ó7Ó8Ð8ð {ùð *øð 0øð *ùð �{‰{Ð&Ø—k‘k×'Ñ'Ó)×)Ò)ð 'üs   ‚AGÁ;F Â	FÂ
F Â)GÃFÃGÃA.F Ä;FÄ<F Å)GÅ+FÅ,!GÆF ÆGÆF ÆGÆ*GÇ GÇGÇGc                ód   — t        | dd «      �yt        | dd«      xs dj                  «       rdS dS )NÚphotoza photor\   r1   rG   zan unsupported message)rç   r=   rè   s    r.   rx  zAutoPublisher._kind{  s9   € ä�3˜ Ó&Ð2ØÜ! # y°"Ó5Ò;¸×BÑBÔDˆvÐbÐJbÐbr-   c                ó�  — t        |«      }|€3dj                  |j                  j                  «       «      xs d}d|› d�S | j                  j
                  }|j                  €|�|j                  r|j                  sd}nd}|j                  r-ddj                  |j                  j                  «       «      › d�nd	}d
|› d|› d|› �S )Nz; zit is not therez3I could not read the post of the Database Channel (z).z'through the bot and through the userbotz3through the bot (no userbot is set up or reachable)rO   Ú.r1   uV   No ðŸ”� Share URL button was found on that post of the Database Channel after waiting z seconds (looked )	r@   rT   rp   r¾   r
  r8  r  r  r  )	r  r<  r}  r{  Úsecsrh  ÚubÚwhereÚextras	            r.   rz  zAutoPublisher._no_button�  sÝ   € Ü�4‹yˆØˆ<Ø—)‘)˜DŸJ™J×-Ñ-Ó/Ó0ÒEÐ4EˆCØHÈÈÈRÐPÐPØ�X‰X×ÑˆØ�;‰;Ð" r ~¸"¿*º*ÈT×M_ÒM_Ø=‰EàIˆEØ9=¿º�!�D—I‘I˜dŸj™j×/Ñ/Ó1Ó2Ð3°1Ñ5ÈˆØgÐhlÐgmÐm~ð  @Eð  Fð  FHð  INð  HOð  Pð  	Pr-   c             ƒ  óŒ  K  — | j                  |||«      ƒ d{  –—† }|� t        |«      rt        |j                  «      nd}|rt	        ||d«      S |�|rZt        |«      rO| j                  |||«      ƒ d{  –—† }|�2t	        |t        |«      rt        |j                  «      d«      S dd«      S |�t	        |dd«      S dS 7 Œ¦7 ŒK­w)zžOne look: through the bot; when the bot cannot read the post, or sees no button once the file-store bot has had
        its time, through the userbot as well.NrT  r8  )Ú	_bot_readrê   ró   Úreply_markuprý   Ú_userbot_read)r  rM  r8   r}  rs  ré   rÿ   Úothers           r.   rv  zAutoPublisher._lookŽ  sÇ   è ø€ ð —N‘N 2 w°Ó5×5ˆØ25°/ÄgÈcÄl”˜S×-Ñ-Ô.ÐX\ˆÙÜ˜˜U EÓ*Ð*Øˆ;™;¬7°3¬<Ø×,Ñ,¨R°¸$Ó?×?ˆEØÐ Ü˜EÄwÈuÄ~¤<°×0BÑ0BÓ#CÐajÓkÐkÐ[_ÐajÓkÐkØ),¨Œt�C˜˜uÓ%ÐB¸dÐBð 6øð
 @ús#   ‚C™C šACÁ6CÁ7A
CÃCc              ƒ  óŠ  K  — 	 |rUt        | j                  j                  t        |«      |g«      ƒ d {  –—† D �cg c]  }t	        |«      rŒ|‘Œ }}|r|d   S d S | j                  |«      ƒ d {  –—† S 7 Œ?c c}w 7 Œ# t        j                  $ r0}dt        |«      j                  › d�|j                  d<   Y d }~y d }~ww xY w­w)Nr   zTelegram refused the bot (r  rT  )r   r
  rG  r   r   Ú_bot_latestr
   rY  r*  r(   rp   )r  rM  r8   r}  rD   Úmsgsr/  s          r.   r‡  zAutoPublisher._bot_read›  s¶   è ø€ ð	ÙÜ)7¸¿¹¿¹ÌÐQSËÐW^ÐV_Ó)`×#`Ð#`ÓxÑ#`˜aÔhtÐuvÕhwšÐ#`�ÐxÙ"&�t˜A‘wÐ0¨DÐ0Ø×)Ñ)¨"Ó-×-Ð-ð $aúÒxà-ùÜ�‰ò 	Ø"<¼TÀ!»W×=MÑ=MÐ<NÈaÐ PˆD�J‰J�uÑÜûð	üsy   ‚C„0A= ´A4µA= »A6ÁA6ÁA= ÁCÁA= ÁCÁA= Á/A;Á0A= Á3CÁ4A= Á6A= Á=C Â&B;Â6CÂ;C Ã Cc              ƒ  ó  K  — | j                   j                  t        |«      }}| j                   j                  j	                  |j
                  «      ƒ d{  –—† }|s't        ||«      ƒ d{  –—† xs d}t        d|dz
  «      }d}t        d«      D ]h  }t        ||t        t        ||dz   «      «      «      ƒ d{  –—† D �cg c]  }t        |«      rŒ|‘Œ }	}|	s |S t        |	d„ ¬«      }|j
                  dz   }Œj |S 7 Œ§7 Œ“7 ŒGc c}w ­w)ziThe newest post of a channel, as far as a bot can tell: look upwards from the newest post My posts knows.Nr   r"   é   éd   c                ó   — | j                   S rL   )r  )rD   s    r.   r¿   z+AutoPublisher._bot_latest.<locals>.<lambda>±  s   € ¨Q¯TªTr-   )Úkey)r
  rG  r   rÖ   Úmax_saved_idr  r   rt  Úranger   r†   r   )
r  rM  rG  rd  ÚfirstÚtopÚnewestÚ_rD   r�  s
             r.   rŒ  zAutoPublisher._bot_latest¥  s÷   è ø€ à—x‘x—‘¬°«�ˆØ—h‘h—k‘k×.Ñ.¨r¯u©uÓ5×5ˆÙÜ" 6¨4Ó0×0Ò5°AˆCÜ˜˜3 ™9Ó%ˆEØˆÜ�q–ˆAÜ%3°F¸DÄ$ÄuÈUÐTYÐ\_ÑT_ÓG`ÓBaÓ%b×bÐbÓzÑb˜!ÔjvÐwxÕjy’AÐbˆDÐzÙØð ˆô ˜¡>Ô2ˆFØ—I‘I ‘M‰Eð ð ˆð 6øà0øð  cúÒzùsO   ‚AD	ÁC>ÁD	Á,D Á-AD	Â:DÂ;D	ÃDÃDÃ)D	Ä D	ÄD	ÄD	c              ƒ  ó¨  K  — | j                   j                  }|j                  €h|j                  s\|�|j	                  |«      ƒ d {  –—† nd }|€6d|_        |�4|j
                  r(d|j                  xs d› �|j                  d<   n||_        |j                  €y 	 |j                  j                  |«      ƒ d {  –—† D �cg c]  }t        |«      rŒ|‘Œ }}|r|d   S d S 7 Œ˜7 Œ(c c}w # t        t        j                  f$ rM}d|_        d|› �|j                  d<   |j                  j                  «       ƒ d {  –—†7   d |_        Y d }~y d }~ww xY w­w)NTzthe userbot is not connected: rt   r8  zthe userbot could not read it: r   )r
  r8  r  r  Ú
reader_forr  Ú
last_errorrp   Úreadr   r    r
   rY  r]  )	r  rM  r8   r}  rƒ  r  rD   r�  r/  s	            r.   r‰  zAutoPublisher._userbot_readµ  s:  è ø€ Ø�X‰X×ÑˆØ�;‰;Ð t×'9Ò'9Ø02°˜2Ÿ=™=¨Ó,×,Ð,ÀDˆFØˆ~Ø%)�Ô"Ø�> b§j¢jØ.LÈRÏ]É]ÒMnÐ^nÐLoÐ,p�D—J‘J˜yÒ)à$�”Ø�;‰;ÐØð	Ø%)§[¡[×%5Ñ%5°gÓ%>×>Ð>ÓVÑ>˜!ÄlÐSTÅo’AÐ>ˆDÐVñ ˆt�A‰wÐ( DÐ(ð# -øð  ?úÒVøÜœfŸo™oÐ.ò 	Ø!%ˆDÔØ&EÀaÀSÐ$IˆD�J‰J�yÑ!Ø—+‘+×#Ñ#Ó%×%Ñ%ØˆDŒKÜûð	üss   ‚AEÁC ÁAEÂC) Â9C"Â:C) Ã C$ÃC$ÃC) Ã
EÃ"C) Ã$C) Ã)EÄ6E
Ä8D;Ä9E
ÅEÅ
EÅEc              ƒ  ó8  K  — | j                   j                  }t        ||j                  |j                  xs ddœ«      \  }}}	|j                  «       r|	st        d«      ‚|j                  |j                  ddd|||	|dœggd d d¬	«
      ƒ d {  –—† }
	 t        | j                   j                  t        |«      |
«      ƒ d {  –—† }|j#                  |j                  |j                  |
j                  ¬«      ƒ d {  –—†  |j%                  |
j                  d|j                  t'        «       ¬«      ƒ d {  –—†  t)        |t+        ||j                  «      |	||xs |j,                  |j                  |j                  ¬«      S 7 Œò7 ŒÁ# t        j                  $ rM}|j                  |
j                  «      ƒ d {  –—†7   t        d
|j                  › dt        |«      › �«      ‚d }~wt         $ r& |j                  |
j                  «      ƒ d {  –—†7   ‚ w xY w7 �Œ7 Œã­w)Nr1   )r4   r6   zUThe message text or the button name of the post comes out empty - check /autopublish.r   ÚdraftrT  )r×   ÚuF)
ra  r[  ÚstatusrZ  rG   rÄ   rî   Ú
media_kindÚmedia_file_idr?  u*   Telegram would not let the bot post in â€œrS  )Úkeep_pidÚsent)r   rZ   Úsent_at)r6   rƒ   rH   ro   r3   r4   rZ   )r
  rÖ   rã   r4   r‹   r=   r'   Úcreate_postr  r   rG  r   r
   rY  Údelete_postr   r(  Úrelease_slotÚupdate_postr   r‚   r   r3   )r  rM  rN  rj   rƒ   r   rÖ   rG   rÄ   rH   rN   r¤  r/  s                r.   rL  zAutoPublisher._postÌ  sÉ  è ø€ Ø�X‰X�[‰[ˆÜ +¨IÀ6Ç>Á>Ð^`×^fÑ^fÒ^lÐjlÑ7mÓ nÑˆˆh˜Ø�z‰zŒ|¡5ÜÐqÓrÐrØ—^‘^Ø—u‘u¨°7À5ÈtÐ^fØ!¨Ñ-Ð.Ð/¸DÐPTÐchð $ó 
÷ 
ˆð	Ü" 4§8¡8§?¡?´G¸B³KÀÓF×FˆDð �o‰o˜bŸe™e T§W¡W°t·w±wˆoÓ?×?Ð?Ø�n‰n˜TŸW™W¨VÀÇÁÔQWÓQYˆnÓZ×ZÐZÜØœY r¨4¯7©7Ó3¸5ÀdÐQUÒQdÐY_×YdÑYdÐnt×n|Ñn|Ø—w‘wô
ð 	
ð
øð
 GùÜ�‰ò 	hØ—.‘. §¡Ó)×)Ñ)ÜÐFÀrÇxÁxÀjÐPUÔVaÐbcÓVdÐUeÐfÓgÐgûÜò 	Ø—.‘. §¡Ó)×)Ñ)Øð	úð 	@ùØZús   ‚BHÂFÂHÂ-F ÃFÃF Ã5HÃ;HÃ<8HÄ4HÄ5AHÆF ÆHÆG Æ6F9Æ7)G Ç *HÈ
HÈHÈHÈHN)r
  r   )rR   rÍ   )r+  r   rR   ÚNone)r+  r   rj   rY   rR   rª  )r+  r   rj   rb   rR   rª  )rj   rb   rR   Útuple)r+  r   rG   r2   rR   rª  )rR   rª  )rj   r0   rR   r‚   )rj   rY   rR   r‘   )rj   r   rR  rÍ   rR   r   )rR   r   )rM  r   rj   r0   rR   rý   ©ré   r   rR   r2   )r<  úOptional[Seen]r}  r  r{  ÚfloatrR   r2   )
rM  r   r8   r5   r}  r  rs  rÍ   rR   r­  )rM  r   r8   r5   r}  r  rR   r   )rM  r   rR   r   )rM  r   rN  rË   rj   r0   rƒ   r2   r   r2   rR   r‚   )r(   r)   r*   r  r  ÚtimeÚ	monotonicr  Úpropertyr  r0  r"  r#  r5  r$  rI  r%  r2  rK  r;  r:  Ústaticmethodrx  rz  rv  r‡  rŒ  r‰  rL  r,   r-   r.   r  r  ™  s¡   „ Ø*1¯-©-¸t¿~¹~õ @ð ò)ó ð)ó(ó<(ó(ó	oóMódóó.*ðX FKõ ó4ó9ð, òcó ðcó
PóCóóó )ô.
r-   r  )rC   úOptional[str]rR   r5   )rG   r³  rH   r2   rR   r2   )rG   r³  rR   úOptional[Notice])rG   r³  rR   úOptional[DeleteNotice])rG   r³  rR   úOptional[LinkNotice]rL   )rj   r¶  rR   r2   )ro   r2   rp   r†   rj   r¶  rR   r2   )rw   r2   rj   r¶  rR   r2   )rj   r   rR   r2   )rj   r´  rR   r2   )Nr1   )rw   r2   rj   r´  r3   r2   rR   r2   )r�   r‚   rj   r´  rR   r2   )rj   rµ  rR   r2   )r•   r‘   rj   rµ  rR   r2   )rw   r2   rj   rµ  rR   r2   )rœ   r@   rR   r@   )rG   r³  rR   r2   )r   r2   rR   r2   )r¶   r³  r±   r2   rR   rÍ   )r¶   r³  rR   r†   )r¶   r³  r¾   rÚ   rR   r2   )rG   r³  rÄ   úOptional[list]r¾   rÚ   rR   r«  )rR   rË   )rG   r³  rÄ   r·  rÌ   r³  rR   rª  )rG   rÍ   rÌ   rÍ   rR   rª  )r×   rË   r¾   rÚ   rR   r«  )ré   r   rR   rÍ   )rð   r   rR   rþ   )ro   r2   rR   r2   r¬  )tr+   Ú
__future__r   r  Úloggingr>   r¯  r£   Úcollectionsr   Údataclassesr   r   Útypingr   r   Úurllib.parser	   Útelethonr
   r   Útelethon.helpersr   r   rî   r   Úcommonr   r   Úconfigr   rÖ   r   Úlinkswapr   Úreplace_enginer   r   r   Úrepost_enginer   r   Útgutilr   r   r   r   r   r   r8  r    r!   Ú	getLoggerr(   r&  ry  rw  r   rÔ   rÐ   rÑ   ÚTEXT_MAXÚ
BUTTON_MAXrâ   ÚcompilerS   r^   rd   rï   rö   ÚIr´   r¹   r(  r'   r0   rE   rI   rW   rY   r`   rb   re   rk   rr   rx   rh   r{   r€   r‚   r�   r‘   r“   r–   r˜   r�   rŸ   r¦   r©   r­   r·   rº   rÀ   rÉ   rË   rØ   rÜ   rà   rã   rê   ró   rø   rû   rý   r  r  r,   r-   r.   Ú<module>rË     s¶  ðñõ4 #ã Û Û 	Û Û Ý #ß (ß  Ý  ç "ß 9å ß "Ý &Ý Ý -ß DÑ Dß 1ß Z× Zß 6à€g×Ñ˜Ó!€à€ØÐ Ø€à€ØM€Ø-€Ø€Ø€
Ø€àˆR�Z‰ZÐNÓO€
ØˆR�Z‰ZÐEÓF€
Øˆ2�:‰:Ð;Ó<€ØˆB�J‰JÐ-Ó.€	Ø�—‘ÐgÓh€à
ˆ"�*‰*ÐmÐoq×osÑosÓ
t€Ø�—‘Ð0Ó1€ô_ˆiô _ð
 ÷ð ó ðó*ó+óð* ÷ð ó ðóð ÷ð ó ðótôRôoôkóOôVôkð ÷ð ó ðô,ð" ÷.ð .ó ð.ôJô,ôMóUó
>ó
jó
:ó
Jóaó
nó
bó
:ð ÷ ð  ó ð óð 7;ÐW[Ðuyõ +ð /4ÀEõ -ó!ó6ó	óIóEð ÷ð ó ðð ÷.ð .ó ð.÷I
ò I
r-   