
    ;Si;9                     T   d Z ddlZddlZddlZddl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  G d dej                        Z ej                   e      ZddZd Zd	 Zd
 ZdddZd ZddZd Zd Zd Zd Zd Zd Z G d de       Z! G d dejD                        Z#d Z$y)z6Various helpers not related to the Telegram API itself    N)Path)sha1c                       e Zd ZdZdZdZy)_EntityTyper         N)__name__
__module____qualname__USERCHATCHANNEL     U/var/www/OnlineNewsSite/project/venv/lib/python3.12/site-packages/telethon/helpers.pyr   r      s    DDGr   r   c                 X    t         j                  t        j                  d      | d      S )zEGenerates a random long integer (8 bytes), which is optionally signed   little)signed	byteorder)int
from_bytesosurandomr   s    r   generate_random_longr      s    >>"**Q-(>KKr   c                 v    t         j                  j                  |       }|rt        j                  |d       yy)z(Ensures that the parent directory existsT)exist_okN)r   pathdirnamemakedirs)	file_pathparents     r   ensure_parent_dir_existsr$       s*    WW__Y'F
FT* r   c                 2    dj                  d | D              S )N c           
   3      K   | ]X  }d t        |      cxk  rdk  r>n n;dj                  d t        j                  d|j	                  d            D              n| Z yw)i   i r&   c              3   2   K   | ]  }t        |        y wN)chr).0ys     r   	<genexpr>z*add_surrogate.<locals>.<genexpr>.<genexpr>+   s     K1AK   z<HHzutf-16leN)ordjoinstructunpackencoder+   xs     r   r-   z add_surrogate.<locals>.<genexpr>(   sU       78Gs1v)) 	KeQXXj5I JKK01	2s   AA )r0   texts    r   add_surrogater8   '   s#    77  <@	  r   c                 D    | j                  dd      j                  d      S )Nzutf-16surrogatepass)r3   decoder6   s    r   del_surrogater<   0   s    ;;x188BBr   )lengthc                    |t        |       }d|cxk  xr t        |       k  nc xr+ d| |dz
     cxk  xr dk  nc xr d| |   cxk  xr dk  S c S )zS
    `True` if ``index`` is within a surrogate (before and after it, not at!).
    r   u   u   u   )len)r7   indexr=   s      r   within_surrogaterA   4   sd     ~T !D	! 0UQY3830U/x/ 0r   c                    |s| j                         S t        |       }| j                         } |t        |       z
  }| j                         } t        |       }t	        t        t        |                  D ]  }||   }|j                  dk(  r||= |j                  |j                  z   |kD  rN|j                  |k\  r|xj                  |z  c_        n-|j                  |j                  z   |z
  |_        d|_        n||= |j                  |j                  z   |k  r|j                  |k\  r||= ||j                  z
  |_         | S )z
    Strips whitespace from the given surrogated text modifying the provided
    entities, also removing any empty (0-length) entities.

    This assumes that the length of entities is greater or equal to 0, and
    that no entity is out of bounds.
    r   )stripr?   lstriprstripreversedranger=   offset)r7   entitieslen_orileft_offset	len_finalies          r   
strip_textrO   B   s-    zz|$iG;;=DCI%K;;=DD	IeCM*+ 1,QK88q=88ahh,xx;& K' 88ahh.< 88ahh)+ 88y   !188+AHc1,l Kr   c              #   V   K   |r| 
| dk  s| dz  } d}|| k7  r|dz  }| || k7  ryyw)z
    Generates an integer sequence starting from 1. If `retries` is
    not a zero or a positive integer value, the sequence will be
    infinite, otherwise it will end at `retries + 1`.
    Nr   r   r   )retriesforce_retryattempts      r   retry_rangerT      sC      GOw{1G
W
1 W
s   $))c                 P   K   t        j                  |       r
|  d {   S | S 7 wr)   )inspectisawaitable)values    r   _maybe_awaitrY      s&     5!{ s   &$&c           	        K   |j                         D ]#  \  }}|s	|j                          	 | d{    % y7 # t        j                  $ r Y >t        $ r Y Ht
        $ r6}|j                  dk7  r| j                  d|t        |      |       Y d}~d}~wt        $ r  | j                  d|t        |      |       Y w xY ww)zL
    Helper to cancel one or more tasks gracefully, logging exceptions.
    N)z"yield from wasn't used with futurez4Unhandled exception from %s after cancelling %s (%s))
itemscancelasyncioCancelledErrorRuntimeErrorAssertionErrorargs	exceptiontype	Exception)logtasksnametaskrN   s        r   _cancelri      s      kkm =
d	=JJ= %% 	 	  	A vv@@ ()-tDz4A 	=MM $%)4:t=	=s[   *C<:<C<C CC CC #,BC)C =C?C  Cc                     t        | d      r| j                  }n| j                  j                  }|j                         rt	        d      |j                  | j                               S )za
    Helps to cut boilerplate on async context
    managers that offer synchronous variants.
    loopz[You must use "async with" if the event loop is running (i.e. you are inside an "async def"))hasattrrk   _client
is_runningr_   run_until_complete
__aenter__)selfrk   s     r   _sync_enterrr      s\    
 tVyy||  >
 	

 ""4??#455r   c                     t        | d      r| j                  }n| j                  j                  }|j                   | j                  |       S )Nrk   )rl   rk   rm   ro   	__aexit__)rq   ra   rk   s      r   
_sync_exitru      s@    tVyy||  "">4>>4#899r   c                    	 | j                   dvrt        dj                  |             	 | j                  j
                  }d|v rt        j                  S d|v rt        j                  S d|v rt        j                  S d|v rt        j                  S t        dj                  |             # t        $ r t        dj                  |             w xY w)N)iVl   9 l   F?L i@iwy-l   ]^ iaFl   &4) z {} does not have any entity typez2{} is not a TLObject, cannot determine entity typeUserChatChannelSelf)
SUBCLASS_OF_ID	TypeErrorformatAttributeError	__class__r	   r   r   r   r   )entityrg   s     r   _entity_typer      s    ]   	)
 	
 >EEfMNN	
 $$D~	4	d	"""	4 6==fE
FF  ]LSSTZ[\\]s   (B+ +$Cc                 .   | j                  ddd      } |j                  ddd      }t        || z         j                         }t        | |z         j                         }t        ||z         j                         }||dd z   }|dd |z   |dd	 z   }||fS )
z7Generates the key data corresponding to the given nonce   r   Tr       N         )to_bytesr   digest)server_nonce	new_noncehash1hash2hash3keyivs          r   generate_key_data_from_noncer     s    ((Xd(CL""2x"=I\)*113E	)*113EY&'..0E
%*
C	r"		"1	-B7Nr   c                   .     e Zd ZdZ fdZd Zd Z xZS )	TotalLista  
    A list with an extra `total` property, which may not match its `len`
    since the total represents the total amount of items *available*
    somewhere else, not the items *in this list*.

    Examples:

        .. code-block:: python

            # Telethon returns these lists in some cases (for example,
            # only when a chunk is returned, but the "total" count
            # is available).
            result = await client.get_messages(chat, limit=10)

            print(result.total)  # large number
            print(len(result))  # 10
            print(result[0])  # latest message

            for x in result:  # show the 10 messages
                print(x.text)

    c                 2    t        |   |i | d| _        y )Nr   )super__init__total)rq   ra   kwargsr   s      r   r   zTotalList.__init__8  s    $)&)
r   c                 f    dj                  dj                  d | D              | j                        S )N[{}, total={}], c              3   2   K   | ]  }t        |        y wr)   )strr4   s     r   r-   z$TotalList.__str__.<locals>.<genexpr>>  s     +c!f+r.   r}   r0   r   rq   s    r   __str__zTotalList.__str__<  s-    &&II+d++TZZ9 	9r   c                 f    dj                  dj                  d | D              | j                        S )Nr   r   c              3   2   K   | ]  }t        |        y wr)   )reprr4   s     r   r-   z%TotalList.__repr__.<locals>.<genexpr>B  s     ,!d1g,r.   r   r   s    r   __repr__zTotalList.__repr__@  s-    &&II,t,,djj: 	:r   )r	   r
   r   __doc__r   r   r   __classcell__)r   s   @r   r   r   !  s    ,9:r   r   c                       e Zd ZdZdddZd Zd Zed        Zed        Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)_FileStreama:  
    Proxy around things that represent a file and need to be used as streams
    which may or not need to be closed.

    This will handle `pathlib.Path`, `str` paths, in-memory `bytes`, and
    anything IO-like (including `aiofiles`).

    It also provides access to the name and file size (also necessary).
    N)	file_sizec                    t        |t              rt        |j                               }|| _        d | _        || _        d | _        d | _        y r)   )	
isinstancer   r   absolute_file_name_size_stream_close_stream)rq   filer   s      r   r   z_FileStream.__init__O  s@    dD!t}}'D


!r   c                    K   t        | j                  t              rt        j                  j                  | j                        | _        t        j                  j                  | j                        | _        t        | j                  d      | _
        d| _        | S t        | j                  t              rGt        | j                        | _        t        j                  | j                        | _
        d| _        | S t!        t#        | j                  dd             st%        d      t#        | j                  dd       | _        | j                  | _
        d| _        | j                  t!        t#        | j                  dd             r,t'        | j                  j)                                d {   }nd}|rt'        | j                  j+                                d {   }t'        | j                  j-                  dt        j.                               d {    t'        | j                  j+                                d {   | _        t'        | j                  j-                  |t        j0                               d {    | S t2        j5                  d	       t'        | j                  j7                                d {   }t        |      | _        t        j                  |      | _
        d| _        | S 7 P7 #7 7 7 7 Cw)
NrbTreadz,file description should have a `read` methodrg   Fseekabler   zRCould not determine file size beforehand so the entire file will be read in-memory)r   r   r   r   r   basenamer   getsizer   openr   r   bytesr?   ioBytesIOcallablegetattrr|   rY   r   tellseekSEEK_ENDSEEK_SET_logwarningr   )rq   r   posdatas       r   rp   z_FileStream.__aenter__Y  s    djj#&))$**5DJ4DJ

D1DL!%DKdjj%(TZZDJ::djj1DL!%DK

FD9:JKKTZZ6
zz"::

J=>!-djj.A.A.C!DD ():;;"4::??1bkk#BCCC#/

0A#BB
"4::??3#DEEE  23 *$**//*;<< Y
!zz$/%)"' E
 <CBE =sm   F.L0L 1/L L!:LL*LL?LL
ALL	8LLLL
LLc                    K   | j                   r9| j                  r,t        | j                  j                                d {    y y y 7 wr)   )r   r   rY   close)rq   exc_typeexc_valexc_tbs       r   rt   z_FileStream.__aexit__  s;     $,,t||113444 #/4s   ?AA	Ac                     | j                   S r)   )r   r   s    r   r   z_FileStream.file_size      zzr   c                     | j                   S r)   )r   r   s    r   rg   z_FileStream.name  r   r   c                 :     | j                   j                  |i |S r)   )r   r   rq   ra   r   s      r   r   z_FileStream.read      ,=DLL,=,=t,Nv,N%Nr   c                 :     | j                   j                  |i |S r)   )r   readintor   s      r   r   z_FileStream.readinto      0E0E0Et0Vv0V)Vr   c                 :     | j                   j                  |i |S r)   )r   writer   s      r   r   z_FileStream.write      -?T\\-?-?-P-P&Pr   c                 :     | j                   j                  |i |S r)   )r   filenor   s      r   r   z_FileStream.fileno      .Adll.A.A4.R6.R'Rr   c                 :     | j                   j                  |i |S r)   )r   flushr   s      r   r   z_FileStream.flush  r   r   c                 :     | j                   j                  |i |S r)   )r   isattyr   s      r   r   z_FileStream.isatty  r   r   c                 :     | j                   j                  |i |S r)   )r   readabler   s      r   r   z_FileStream.readable  r   r   c                 :     | j                   j                  |i |S r)   )r   readliner   s      r   r   z_FileStream.readline  r   r   c                 :     | j                   j                  |i |S r)   )r   	readlinesr   s      r   r   z_FileStream.readlines  s    1G1G1G1XQW1X*Xr   c                 :     | j                   j                  |i |S r)   )r   r   r   s      r   r   z_FileStream.seek  r   r   c                 :     | j                   j                  |i |S r)   )r   r   r   s      r   r   z_FileStream.seekable  r   r   c                 :     | j                   j                  |i |S r)   )r   r   r   s      r   r   z_FileStream.tell  r   r   c                 :     | j                   j                  |i |S r)   )r   truncater   s      r   r   z_FileStream.truncate  r   r   c                 :     | j                   j                  |i |S r)   )r   writabler   s      r   r   z_FileStream.writable  r   r   c                 :     | j                   j                  |i |S r)   )r   
writelinesr   s      r   r   z_FileStream.writelines  s    2I$,,2I2I42ZSY2Z+Zr   c                      y r)   r   r   s      r   r   z_FileStream.close  s    r   )r	   r
   r   r   r   rp   rt   propertyr   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   E  sy     +/ "*X5     OVPRPRVVXNVNVVZr   r   c                      t         j                  dk\  r	 t        j                         S t        j                         S # t        $ r% t        j
                         j                         cY S w xY w)N)      )sysversion_infor]   get_running_loopr_   get_event_loop_policyget_event_loopr   r   r   r   r     sa    
6!	D++-- %%''  	D002AACC	Ds   = +A+*A+)T)%r   r]   r   enumr   r1   rV   logging	functoolsr   pathlibr   hashlibr   Enumr   	getLoggerr	   r   r   r$   r8   r<   rA   rO   rT   rY   ri   rr   ru   r   r   listr   IOBaser   r   r   r   r   <module>r     s    <  	  	     
  $))  w"L
+C -1 GT&"=J6&:!GP
$!: !:Hb")) bL(r   