
    i                         S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	J
r  S SKJrJrJrJrJr  SSKJr  SS	KJr  SS
KJr  \" \5      r " S S\5      rg)    )	HttpError)MediaIoBaseDownload)FileIO)	getLogger)makedirspath)retrywait_exponentialstop_after_attemptretry_if_exception_type
RetryError   )async_to_sync)SetInterval)GoogleDriveHelperc                   x   ^  \ rS rSrU 4S jrS rS r\" \" SSSS9\	" S5      \
" \5      S	9SS
 j5       rSrU =r$ )GoogleDriveDownload   c                 V   > Xl         S U l        X l        [        TU ]  5         SU l        g )NT)listener_updater_pathsuper__init__is_downloading)selfr   r   	__class__s      ;/app/bot/helper/mirror_leech_utils/gdrive_utils/download.pyr   GoogleDriveDownload.__init__   s'     
"    c                    U R                  U R                  R                  U R                  R                  5      nU R	                  5       U l        [        U R                  U R                  5      U l	         U R                  U5      nUR                  S5      U R                  :X  a1  U R                  XR                  U R                  R                  5        OU[!        U R                  SS9  U R#                  XR                  U R                  R                  UR                  S5      5         U R                  R?                  5         U R                  RB                  (       a  g [E        U R                  RF                  5        g ! [$         Ga  n['        U[(        5      (       aF  [*        R-                  SUR.                  R0                   35        UR.                  R3                  5       n[5        U5      R7                  SS5      R7                  SS5      nSU;   a  S	nOS
U;   a  U R8                  (       d  U R:                  (       a  SU l        SU l        [*        R=                  S5        U R                  R?                  5         U RA                  5       s S nAU R                  R?                  5         U R                  RB                  (       a    g [E        U R                  RF                  5          g Sn[E        U R                  RH                  U5        SU R                  l!         S nAGNS nAff = f! U R                  R?                  5         U R                  RB                  (       a     g [E        U R                  RF                  5           g = f)NmimeTypeT)exist_okzTotal Attempts: > <downloadQuotaExceededzDownload Quota Exceeded.zFile not foundFz+File not found. Trying with token.pickle...zFile not found!)%get_id_from_urlr   linkuser_id	authorizeservicer   update_intervalprogressr   get_file_metadatagetG_DRIVE_DIR_MIME_TYPE_download_folderr   namer   _download_file	Exception
isinstancer   LOGGERinfolast_attemptattempt_number	exceptionstrreplacealt_authuse_saerrorcanceldownloadis_cancelledr   on_download_completeon_download_error)r   file_idmetaerrs       r   rB   GoogleDriveDownload.download   sr   &&t}}'9'94==;P;PQ~~'#D$8$8$--H	))'2Dxx
#t'A'AA%%gzz4==;M;MNd3##ZZ););TXXj=Q* MM  "}}))$--<<=-  	.#z**.s/?/?/N/N.OPQ&&002c(""3+33C<C&#-0!S(}}$(DM"'DKLL!NOMM((*==?*
 MM  "}}))$--<<= ($--993?)-DMM&&!	.$ MM  "}}))$--<<=sW   6A F L. AF +L. L+DL&L+L. -3L& L. &L++L. .7N	' N	c                    UR                  SS5      n[        R                  " U SU 35      (       d  [        U SU 35        USU 3-  nU R	                  U5      n[        U5      S:X  a  g [        US S9nU GH  nUS   nUS   nUR                  S5      nUb  US	   nUS
   n	OUR                  S5      n	XR                  :X  a  U R                  XbU5        GO[        R                  " U U 35      (       a  M  U R                  R                  (       aQ  UR                  5       R                  5       R                  [!        U R                  R                  5      5      (       d  M  U R                  R                  (       dR  UR                  5       R                  5       R                  [!        U R                  R"                  5      5      (       a  GM[  U R%                  XbXy5        U R                  R&                  (       d  GM    g    g )N/r%   r   c                     U S   $ )Nr3    )ks    r   <lambda>6GoogleDriveDownload._download_folder.<locals>.<lambda>J   s    air    )keyidr3   shortcutDetailstargetIdtargetMimeTyper"   )r=   ospathexistsr   get_files_by_folder_idlensortedr0   r1   r2   isfiler   included_extensionsstriplowerendswithtupleexcluded_extensionsr4   rC   )
r   	folder_idr   folder_nameresultitemrF   filenameshortcut_details	mime_types
             r   r2   $GoogleDriveDownload._download_folderB   s   !))#r2}}vQ{m455vQ{m,-!K=!!,,Y7v;!$78D4jGF|H#xx(9:+*:6,-=>	 HHZ0	666%%gX>$z23311 (% A ABCD MM55NN$% A ABCD ##G8G}})))= r       r      )
multiplierminmax)waitstopr	   c                    U(       a)  U R                   R                  5       R                  USS9nO)U R                   R                  5       R                  USSS9nUR	                  SS5      nU(       a  U S3n[        UR                  5       5      S:  aj  [        R                  " U5      S	   nUS S
  U 3nU R                  R                  R                  5       R                  U5      (       a  X0R                  l
        U R                  R                  (       a  g [        U SU 3S5      n[        XSS9n	Sn
SnU
(       dN  U R                  R                  (       a  UR!                  5         O" U	R#                  5       u  U l        n
U
(       d  MN  SU l!        g ! [&         Ga  n[(        R+                  U5        UR,                  R$                  S;   a  US:  a  US	-  n S nAM  UR,                  R/                  SS5      R1                  S5      (       Ga:  [3        UR4                  5      R/                  S5      R/                  S5      S   R/                  S5      nSU;   a  SU;   a  U R7                  XX4S5      s S nA$ US;  a  UeU R8                  (       a  U R:                  U R<                  :  a$  [(        R?                  SU R:                   35        UeU R                  R                  (       a   S nAg U RA                  5         [(        R?                  SU S35        U R7                  XX45      s S nA$ [(        R+                  SU 35        Ue S nAGNS nAff = f)Nzapplication/pdf)fileIdr"   T)rr   supportsAllDrivesacknowledgeAbuserK   r%   z.pdf         wbi  @)	chunksizeFr   )i  i  i  i  i  
   zcontent-typezapplication/jsonr@   errorsreasonfileNotDownloadabledocument)r'   dailyLimitExceededz?Reached maximum number of service accounts switching, which is zGot: z, Trying Again...)"r,   filesexport_media	get_mediar=   rY   encoderV   splitextr   r3   r]   r_   rC   r   r   close
next_chunkstatusr   r7   r@   respr0   
startswithevalcontentr4   r?   sa_count	sa_numberr8   switch_service_accountfile_processed_bytes)r   rF   r   rf   rh   exportrequestextfh
downloaderdoneretriesrH   r|   s                 r   r4   "GoogleDriveDownload._download_filek   s    ll((*77): 8 G ll((*44$ 5 G ##C,"4(Hx !C'//(+A.C"4C.)#/H}}!!'')22377%-"==%%tfAhZ($/(@QR
}}))
$"$.$9$9$;!T $R %&!G  ""S!88??&??GbLqLG88<<3>>?QRRS[[)--g6::8DQGKKHU  -6:;R#22#8    &  "	{{==DNN:"KK"abfboboap q #&I#}}99 & 779"KK%x7H(IJ#'#6#6 'x$  uVH%56!	; S""s>   .F M":M"BM6M<A2M3:M-M3MM)	r   r   r>   r   r   r   r,   r   r?   )F)__name__
__module____qualname____firstlineno__r   rB   r2   r	   r
   r   r   r5   r4   __static_attributes____classcell__)r   s   @r   r   r      sK    ##J'R q9"%i0
A&
A&r    r   N)googleapiclient.errorsr   googleapiclient.httpr   ior   loggingr   osr   r   rV   tenacityr	   r
   r   r   r   ext_utils.bot_utilsr   r   &mirror_leech_utils.gdrive_utils.helperr   r   r7   r   rM   r    r   <module>r      sB    , 4   '  1 . G	8	\&+ \&r    