Ë
    0IæjM+  ã                   ó4  — d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dl	Z	d dl
m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 ddlmZ d	Z G d
„ de«      Zd„ Z	 	 	 dd„Zd„ Z e j4                  dd«      Z	 	 	 	 	 	 	 	 	 	 	 	 ddedeee   ee   df   fd„Zy)é    N)ÚList)ÚUnioné   )Ú_get_session)Údownload)ÚFolderContentsMaximumLimitError)Úis_google_drive_urlé2   c                   ó   — e Zd ZdZdd„Zd„ Zy)Ú_GoogleDriveFilez"application/vnd.google-apps.folderNc                 óP   — || _         || _        || _        |�|| _        y g | _        y ©N)ÚidÚnameÚtypeÚchildren)Úselfr   r   r   r   s        úX/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/gdown/download_folder.pyÚ__init__z_GoogleDriveFile.__init__   s)   € ØˆŒØˆŒ	ØˆŒ	Ø$,Ð$8˜ˆ�¸bˆ�ó    c                 ó4   — | j                   | j                  k(  S r   )r   ÚTYPE_FOLDER)r   s    r   Ú	is_folderz_GoogleDriveFile.is_folder   s   € Ø�y‰y˜D×,Ñ,Ñ,Ð,r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   r   © r   r   r   r      s   „ Ø6€KóAó-r   r   c                 óf  — t        j                  |d¬«      }d}|j                  d«      D ]l  }|j                  «       }d|v sŒt	        j
                  d«      j                  |«      }	 t        t        j                  |dd«      «      j                  d«      } n |€t        d	«      ‚t        j                  «       5  t        j                  d
t         ¬«       |j#                  d«      j%                  d«      }ddd«       t'        j(                  «      }|d   €g n|d   }	d}
|j*                  j,                  d   j/                  |
«      }t1        |«      dk\  r|
j3                  |dd «      }n1t        dj5                  |j*                  j,                  d   «      «      ‚t7        | j/                  d«      d   |t6        j8                  ¬«      }|	D �cg c].  }|d   |d   j#                  d«      j%                  d«      |d   f‘Œ0 }}||fS # t        $ r t        d«      ‚w xY w# 1 sw Y   �Œ,xY wc c}w )zBExtracts information about the current page file and its children.zhtml.parser)ÚfeaturesNÚscriptÚ
_DRIVE_ivdz'((?:[^'\\]|\\.)*)'r   z*Couldn't find the folder encoded JS stringzØCannot retrieve the folder information from the link. You may need to change the permission to 'Anyone with the link', or have had many accesses. Check FAQ in https://github.com/wkentaro/gdown?tab=readme-ov-file#faq.Úignore)Úcategoryzutf-8Úunicode_escaper   z - é   éÿÿÿÿz-file/folder name cannot be extracted from: {}ú/©r   r   r   Úraw_unicode_escapeé   )Úbs4ÚBeautifulSoupÚselectÚdecode_contentsÚreÚcompileÚfinditerÚnextÚ	itertoolsÚisliceÚgroupÚStopIterationÚRuntimeErrorÚwarningsÚcatch_warningsÚfilterwarningsÚDeprecationWarningÚencodeÚdecodeÚjsonÚloadsÚtitleÚcontentsÚsplitÚlenÚjoinÚformatr   r   )ÚurlÚcontentÚfolder_soupÚencoded_datar    Ú
inner_htmlÚ
regex_iterÚdecodedÚ
folder_arrÚfolder_contentsÚsepÚsplittedr   Úgdrive_fileÚeÚid_name_type_iters                   r   Ú_parse_google_drive_filerT   #   sA  € ô ×#Ñ# G°mÔD€Kð €LØ×$Ñ$ XÓ.ò ˆØ×+Ñ+Ó-ˆ
à˜:Ò%äŸ™Ð$:Ó;×DÑDÀZÓPˆJðQÜ#¤I×$4Ñ$4°ZÀÀDÓ$IÓJ×PÑPÐQRÓS�ñ ðð ÐÜðUó
ð 	
ô 
×	 Ñ	 Ó	"ñ HÜ×Ñ Ô3EÕFØ×%Ñ% gÓ.×5Ñ5Ð6FÓGˆ÷Hô —‘˜GÓ$€Jà& q™MÐ1‘b°zÀ!±}€Oà
€CØ× Ñ ×)Ñ)¨!Ñ,×2Ñ2°3Ó7€HÜ
ˆ8ƒ}˜ÒØ�x‰x˜  "˜Ó&‰äØ;×BÑBØ×!Ñ!×*Ñ*¨1Ñ-óó
ð 	
ô #Ø�9‰9�S‹>˜"ÑØÜ×)Ñ)ô€Kð !öàð 
ˆ1‰ˆq�‰t�{‰{Ð/Ó0×7Ñ7¸Ó@À!ÀAÁ$ÒGðÐð ð
 Ð)Ð)Ð)øôU !ò QÜ"Ð#OÓPÐPðQú÷Hñ Hüò0s   Á(/H	Â;<H!Ç3H.È	HÈ!H+c                 óL  — d}t        d«      D ]p  }t        |«      rd|v r|dz  }n|dz  }| j                  ||¬«      }|j                  dk7  r yt        |«      r n%t        |j                  «      s n|j                  }Œr t        |j                  ¬	«      \  }}	|	D ]Ÿ  \  }
}}|t        j                  k7  r?|st        d
|
|«       |j                  j                  t        |
||¬«      «       |s|dfc S ŒY|st        d|
|«       t        | d|
z   ||¬«      \  }}|s|dfc S |j                  j                  |«       Œ¡ t        |j                  «      t        k(  }|sE|rCdj                  dj!                  |¬«      dj!                  t        ¬«      dg«      }t#        |«      ‚||fS )z0Get folder structure of Google Drive folder URL.Tr%   Ú?z&hl=enz?hl=en)ÚverifyéÈ   )FN)rF   rG   zProcessing filer(   NzRetrieving folderz'https://drive.google.com/drive/folders/)ÚsessrF   ÚquietÚremaining_okú z!The gdrive folder with url: {url})rF   zhas more than {max} files,)Úmaxz+gdrive can't download more than this limit.)Úranger	   ÚgetÚstatus_coderF   rT   Útextr   r   Úprintr   ÚappendÚ%_download_and_parse_google_drive_linkrC   ÚMAX_NUMBER_FILESrD   rE   r   )rY   rF   rZ   r[   rW   Úreturn_codeÚ_ÚresrQ   rS   Úchild_idÚ
child_nameÚ
child_typeÚchildÚhas_at_least_max_filesÚmessages                   r   rd   rd   `   sá  € ð €Kä�1‹Xò ˆÜ˜sÔ#à�c‰zØ�x‘‘à�x‘�à�h‰h�s 6ˆhÓ*ˆØ�?‰?˜cÒ!Ùä˜sÔ#Ùä" 3§7¡7Ô+Ùð �g‰g‰ð'ô* &>ØØ—‘ô&Ñ"€KÐ"ð
 ->ò !+Ñ(ˆ�*˜jØÔ)×5Ñ5Ò5ÙÜØ%ØØôð
 × Ñ ×'Ñ'Ü ØØ#Ø#ôôñ Ø" DÐ(Ò(ØáÜØ#ØØôô
 CØØ9¸HÑDØØ%ô	
Ñˆ�Uñ Ø Ð$Ò$Ø×Ñ×#Ñ# EÕ*ðC!+ôD ! ×!5Ñ!5Ó6Ô:JÑJÐÙÑ2Ø—(‘(à3×:Ñ:¸sÐ:ÓCØ,×3Ñ3Ô8HÐ3ÓIØ=ðó
ˆô .¨gÓ6Ð6Ø˜Ð#Ð#r   c                 ó  — g }| j                   D ]û  }|j                  j                  t        j                  d«      |_        |j                  «       rr|j                  dt        j                  ||j                  «      f«       t        |t        j                  ||j                  «      «      D ]  }|j                  |«       Œ Œ´|j                   rŒÁ|j                  |j                  t        j                  ||j                  «      f«       Œý |S )zEConverts a Google Drive folder structure into a local directory list.rg   N)
r   r   ÚreplaceÚosprO   r   rc   rD   Ú_get_directory_structurer   )rQ   Úprevious_pathÚdirectory_structureÚfileÚis        r   rr   rr   ´   sÑ   € ð ÐØ×$Ñ$ò VˆØ—I‘I×%Ñ%¤c§g¡g¨sÓ3ˆŒ	Ø�>‰>ÔØ×&Ñ&¨¬c¯h©h°}ÀdÇiÁiÓ.PÐ'QÔRÜ-¨d´C·H±H¸]ÈDÏIÉIÓ4VÓWò .�Ø#×*Ñ*¨1Õ-ñ.à—“Ø×&Ñ&¨¯©´·±¸-ÈÏÉÓ1SÐ'TÕUðVð Ðr   ÚGoogleDriveFileToDownload©r   ÚpathÚ
local_pathÚskip_downloadÚreturnc                 óZ  — |du | du z  st        d«      ‚|�dj                  |¬«      } |	€d}	t        |||	¬«      }|st        dt        j
                  ¬«       t        || |||¬	«      \  }}|st        d
t        j
                  ¬«       y|s6t        dt        j
                  ¬«       t        dt        j
                  ¬«       t        |d¬«      }|st        dt        j
                  ¬«       |€%t        j                  «       t        j                  z   }|j                  t        j                  «      r!t        j                  ||j                  «      }n|}|
s*t        j                  |«      st        j                   |«       g }|D �]  \  }}t        j                  ||«      }|€-|
s*t        j                  |«      st        j                   |«       ŒL|
r|j#                  t%        |||¬«      «       Œl|rQt        j&                  j)                  |«      r2|st        d|› �t        j
                  ¬«       |j#                  |«       Œ¿t+        d|z   |||||||¬«      }|€|st        dt        j
                  ¬«        y|j#                  |«       �Œ	 |st        dt        j
                  ¬«       |S )aÆ  Downloads entire folder from URL.

    Parameters
    ----------
    url: str
        URL of the Google Drive folder.
        Must be of the format 'https://drive.google.com/drive/folders/{url}'.
    id: str
        Google Drive's folder ID.
    output: str, optional
        String containing the path of the output folder.
        Defaults to current working directory.
    quiet: bool, optional
        Suppress terminal output.
    proxy: str, optional
        Proxy.
    speed: float, optional
        Download byte size per second (e.g., 256KB/s = 256 * 1024).
    use_cookies: bool, optional
        Flag to use cookies. Default is True.
    verify: bool or string
        Either a bool, in which case it controls whether the server's TLS
        certificate is verified, or a string, in which case it must be a path
        to a CA bundle to use. Default is True.
    user_agent: str, optional
        User-agent to use in the HTTP request.
    skip_download: bool, optional
        If True, return the list of files to download without downloading them.
        Defaults to False.
    resume: bool
        Resume interrupted transfers.
        Completed output files will be skipped.
        Partial tempfiles will be reused, if the transfer is incomplete.
        Default is False.

    Returns
    -------
    files: List[str] or List[GoogleDriveFileToDownload] or None
        If dry_run is False, list of local file paths downloaded or None if failed.
        If dry_run is True, list of GoogleDriveFileToDownload that contains
        id, path, and local_path.

    Example
    -------
    gdown.download_folder(
        "https://drive.google.com/drive/folders/" +
        "1ZXEhzbLRLU1giKKRJkjm8N04cO_JoYE2",
    )
    Nz$Either url or id has to be specifiedz+https://drive.google.com/drive/folders/{id})r   zyMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36)ÚproxyÚuse_cookiesÚ
user_agentzRetrieving folder contents)ru   )rZ   r[   rW   z"Failed to retrieve folder contentsz$Retrieving folder contents completedzBuilding directory structureÚ )rs   z&Building directory structure completedrx   z!Skipping already downloaded file zhttps://drive.google.com/uc?id=)rF   ÚoutputrZ   r~   Úspeedr   rW   ÚresumezDownload ended unsuccessfullyzDownload completed)Ú
ValueErrorrE   r   rb   ÚsysÚstderrrd   rr   ÚosÚgetcwdrq   rO   ÚendswithrD   r   ÚexistsÚmakedirsrc   rw   ry   Úisfiler   )rF   r   r‚   rZ   r~   rƒ   r   r[   rW   r€   r{   r„   rY   Ú
is_successrQ   rt   Úroot_dirÚfilesry   rz   s                       r   Údownload_folderr‘   È   sK  € ð~ �$ˆJ˜3 $˜;Ò'ÜÐ?Ó@Ð@Ø	€~Ø;×BÑBÀbÐBÓIˆØÐð Qˆ
ä˜e°ÈÔT€DáÜÐ*´·±Õ<ÜCØØØØ!ØôÑ€J�ñ ÜÐ2¼¿¹ÕDØáÜÐ4¼3¿:¹:ÕFÜÐ,´3·:±:Õ>Ü2°;ÈbÔQÐÙÜÐ6¼S¿Z¹ZÕHà€~Ü—‘“œsŸw™wÑ&ˆØ‡�”s—w‘wÔÜ—8‘8˜F K×$4Ñ$4Ó5‰àˆÙ¤§¡¨HÔ!5Ü
�‰�HÔà€EØ'ó $%‰ˆˆDÜ—X‘X˜h¨Ó-ˆ
àˆ:Ù ¬¯©°JÔ)?Ü—‘˜JÔ'ØáØ�L‰LÜ)¨R°dÀzÔRõñ œ"Ÿ'™'Ÿ.™.¨Ô4ÙÜØ;¸J¸<ÐHÜ ŸZ™Zõð —‘˜ZÔ(Øä!Ø5¸Ñ:Ø!ØØØØ'ØØô	ˆJð Ð!ÙÜÐ9ÄÇ
Á
ÕKÙØ�L‰L˜Ö$ðI$%ñJ ÜÐ"¬¯©Õ4Ø€Lr   )FFT)NNNFNNTFTNFF)Úcollectionsr3   r>   rˆ   Úos.pathry   rq   r/   r†   r8   Útypingr   r   r+   r   r   Ú
exceptionsr   Ú	parse_urlr	   re   Úobjectr   rT   rd   rr   Ú
namedtuplerw   ÚboolÚstrr‘   r   r   r   ú<module>r›      sÔ   ðÛ Û Û Û 	Ý Û 	Û 
Û Ý Ý ã 
å "Ý Ý 7Ý *àÐ ô
-�vô 
-ò:*ð@ ØØóQ$òhð 3˜K×2Ñ2ØÐ!=óÐ ð 	ØØØ
Ø
Ø
ØØØØØØñNð ðNð ˆ4�‰9�dÐ4Ñ5°tÐ;Ñ<ôNr   