o
    dôjâ5  ã                   @  sF  d dl m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mZ d dlmZ d dlmZ d dl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dlmZ ddlmZ ddlmZ eddd�G dd„ dƒƒZd=dd„Z														 d>d?d1d2„Zd@d3d4„Z dAd9d:„Z!dBd;d<„Z"dS )Cé    )ÚannotationsN)Ú	dataclass)Úfield)Ú
HTTPStatus)ÚClassVar)ÚFinalé   )ÚGoogleDriveFileToDownload)Ú_get_session)Ú_sanitize_filename)Ú_validate_retries)Údownload)ÚDownloadError)Ú_parse_google_drive_folder_idTF)Úkw_onlyÚeqc                   @  s|   e Zd ZU ded< ded< ded< ee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dd„Zddd„ZdS )Ú_GoogleDriveFileÚstrÚidÚnameÚtype)Údefault_factoryzlist[_GoogleDriveFile]Úchildrenz"application/vnd.google-apps.folderzClassVar[str]ÚTYPE_FOLDERz$application/vnd.google-apps.documentÚTYPE_DOCUMENTz'application/vnd.google-apps.spreadsheetÚTYPE_SPREADSHEETz(application/vnd.google-apps.presentationÚTYPE_PRESENTATIONÚreturnÚboolc                 C  s   | j | jkS )N)r   r   ©Úself© r!   úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/gdown/download_folder.pyÚ	is_folder(   s   z_GoogleDriveFile.is_folderc                 C  s   | j  d¡o
|  ¡  S )Núapplication/vnd.google-apps.)r   Ú
startswithr#   r   r!   r!   r"   Úis_google_native+   s   ÿz!_GoogleDriveFile.is_google_nativeN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__annotations__r   Úlistr   r   r   r   r   r#   r&   r!   r!   r!   r"   r      s   
 
r   Úgdrive_fileÚprevious_pathr   r   ú)list[tuple[_GoogleDriveFile | None, str]]c                 C  sˆ   g }| j D ]<}t|jd�|_| ¡ r2| d t ||j¡f¡ t|t ||j¡d�D ]}| |¡ q)q|j sA| |t ||j¡f¡ q|S )N©Úfilename©r,   r-   )r   r   r   r#   ÚappendÚospÚjoinÚ_get_directory_structure)r,   r-   Údirectory_structureÚfileÚir!   r!   r"   r5   3   s   

þü€r5   Úurlú
str | Noner   ÚoutputÚquietr   ÚproxyÚspeedúfloat | NoneÚuse_cookiesÚverifyú
bool | strÚ
user_agentÚskip_downloadÚresumeÚcookies_fileÚtimeoutú"float | tuple[float, float] | NoneÚretriesÚintú+list[str] | list[GoogleDriveFileToDownload]c                 C  sà  t |d� |du | du A stdƒ‚|du r!| dusJ ‚t| d�}n|}d}t|||du r.|n||d�\}}z|s?tdtjd� t|||||d	�}W | ¡  n| ¡  w t	|j
d
�|_
|sjtdtjd� tdtjd� t|dd�}|sytdtjd� |du r„t ¡ tj }| tj¡r’t ||j
¡}n|}|	s t |¡s t |¡ g }g }|D ]¬\}}t ||¡}|du rÁ|	sÀt |¡sÀt |¡ q¦|	rÓ| ¡ sÓ| t|j||d�¡ q¦|}| ¡ rå|	rÝdnt |¡tj }ztd|j |||||||
|||	||d�}W n, t�y' } z|	�r‚ | |¡ |�std|› d|› �tjd� W Y d}~q¦d}~ww |	�rMt|tƒ�s3J ‚t t |¡|j¡}| t|j|t ||¡d�¡ q¦| |¡ q¦|�rdtdd dd„ |D ƒ¡ ƒ‚|�sntdtjd� |S )aB	  Downloads entire folder from URL.

    Parameters
    ----------
    url:
        URL of the Google Drive folder.
        Must be of the format 'https://drive.google.com/drive/folders/{url}'.
    id:
        Google Drive's folder ID.
    output:
        String containing the path of the output folder.
        Defaults to current working directory.
    quiet:
        Suppress terminal output.
    proxy:
        Proxy.
    speed:
        Download byte size per second (e.g., 256KB/s = 256 * 1024).
    use_cookies:
        Flag to use cookies. Default is True.
    verify:
        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:
        User-agent to use in the HTTP request.
    skip_download:
        If True, return the list of files to download without downloading them.
        Defaults to False.
    resume:
        Resume interrupted transfers.
        Completed output files will be skipped.
        Partial tempfiles will be reused, if the transfer is incomplete.
        Default is False.
    cookies_file:
        Netscape cookies file to load when a session opens and save after
        every Google Drive file response. Default is
        ~/.cache/gdown/cookies.txt. Ignored when use_cookies is False.
    timeout:
        Seconds to wait for the server between bytes, either as a single
        value or as a (connect, read) pair, as in requests. Default is None,
        which waits forever.
    retries:
        Additional attempts per file for transient network failures. Default is
        zero. Retries resume the current transfer; resume=True also reuses earlier
        downloads. Folder discovery and skip_download do not retry.

    Returns
    -------
    files:
        If skip_download is False, list of local file paths downloaded.
        If skip_download is True, list of GoogleDriveFileToDownload that contains
        id, path, and local_path.

    Raises
    ------
    ValueError
        If neither url nor id is specified, or both are specified.
    DownloadError
        If a file in the folder fails to download.

    Example
    -------
    gdown.download_folder(
        "https://drive.google.com/drive/folders/" +
        "1ZXEhzbLRLU1giKKRJkjm8N04cO_JoYE2",
    )
    )rI   Nz$Either url or id has to be specified©r9   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)r=   r@   rC   rF   zRetrieving folder contents)r7   ©ÚsessÚ	folder_idr<   rA   rG   r/   z$Retrieving folder contents completedzBuilding directory structureÚ r1   z&Building directory structure completed)r   ÚpathÚ
local_pathzhttps://drive.google.com/uc?id=)r9   r;   r<   r=   r>   r@   rA   rE   rF   rC   rD   rG   rI   zFailed to download z: z(Failed to download the following files:
Ú
c                 s  s   � | ]}d |› �V  qdS )z- Nr!   )Ú.0rQ   r!   r!   r"   Ú	<genexpr>
  s   € z"download_folder.<locals>.<genexpr>zDownload completed)r   Ú
ValueErrorÚ_extract_folder_idr
   ÚprintÚsysÚstderrÚ%_download_and_parse_google_drive_linkÚcloser   r   r5   ÚosÚgetcwdr3   ÚsepÚendswithr4   ÚexistsÚmakedirsr&   r2   r	   r   Údirnamer   r   Ú
isinstancerQ   )r9   r   r;   r<   r=   r>   r@   rA   rC   rD   rE   rF   rG   rI   rO   ZFOLDER_USER_AGENTrN   Ú_r,   r6   Úroot_dirÚfilesZfailed_pathsrQ   rR   Zdownload_outputZdownloaded_fileÚer!   r!   r"   Údownload_folderF   sÒ   
T
ûûÿ

ÿÿÿ
ó
€ú
ýÿÿÿri   c                 C  s(   t | d�ptj | ¡j d¡ d¡d S )NrL   ú/éÿÿÿÿ)r   ÚurllibÚparseÚurlparserQ   ÚrstripÚsplitrL   r!   r!   r"   rW     s   
þrW   rN   úrequests.SessionrO   ú&tuple[str, list[tuple[str, str, str]]]c                 C  sx  t j d|i¡}d|› �}| j|||d�}|jtjkr'td|› d|j› d�ƒ‚tj	|j
dd�}|jd u s:|jjd u rBtd	|› d
�ƒ‚|jj}g }	|jdd�D ]i}
|
 dd¡}t|tƒs\qNtjd|d�}|ry| d¡}|
jdd�}|	 ||df¡ qNtjd|d�}|rž| ¡ \}}|
jdd�}d| d¡ }|	 |||f¡ qNt|d�}|d ur·|
jdd�}|	 ||tjf¡ qNqN||	fS )Nr   z,https://drive.google.com/embeddedfolderview?)rA   rG   z2Failed to retrieve folder contents for folder ID: z (status code z¥). 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.zhtml.parser)Úfeaturesz/Failed to parse folder contents for folder ID: z&. The page structure may have changed.Úa)r   ÚhrefrP   z3https://drive\.google\.com/file/d/([-\w]{25,})/view)ÚpatternÚstringr   T)Ústripzapplication/octet-streamz/https://docs\.google\.com/(\w+)/d/([-\w]{25,})/r$   ÚsrL   )rl   rm   Ú	urlencodeÚgetÚstatus_coder   ÚOKr   Úbs4ÚBeautifulSoupÚtextÚtitlerw   Úfind_allrd   r   ÚreÚmatchÚgroupÚget_textr2   ÚgroupsÚremovesuffixr   r   r   )rN   rO   rA   rG   Úparamsr9   ÚresÚsoupÚfolder_namer   Za_tagru   Z
file_matchÚfile_idÚ	file_nameZ
docs_matchÚkindÚ	file_typeZchild_folder_idÚ
child_namer!   r!   r"   Ú_parse_embedded_folder_view  s\   
ÿÿ
ÿ
þ
þ
ýr’   c                 C  s˜   t | |||d�\}}t||tjd�}|D ]5\}}	}
|
tjkr2|s&td||	ƒ |j t||	|
d�¡ q|s:td||	ƒ t| ||||d�}|j |¡ q|S )N)rN   rO   rA   rG   )r   r   r   zProcessing filezRetrieving folderrM   )r’   r   r   rX   r   r2   r[   )rN   rO   r<   rA   rG   rŒ   r   r,   Zchild_idr‘   Z
child_typeÚchildr!   r!   r"   r[   Z  sN   
ÿý
ýýÿýûr[   )r,   r   r-   r   r   r.   )NNNFNNTTNFFNNr   )r9   r:   r   r:   r;   r:   r<   r   r=   r:   r>   r?   r@   r   rA   rB   rC   r:   rD   r   rE   r   rF   r:   rG   rH   rI   rJ   r   rK   )r9   r   r   r   )
rN   rq   rO   r   rA   rB   rG   rH   r   rr   )rN   rq   rO   r   r<   r   rA   rB   rG   rH   r   r   )#Ú
__future__r   r]   Úos.pathrQ   r3   rƒ   rY   Úurllib.parserl   Údataclassesr   r   Úhttpr   Útypingr   r   r~   Úrequestsr   r	   r
   r   r   Ú
exceptionsr   Ú	parse_urlr   r   r5   ri   rW   r’   r[   r!   r!   r!   r"   Ú<module>   sT    

ò 
L
B