a
    
dD%                  	   @   s   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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 ddlmZ dZG dd	 d	eZd
d ZdddZdd ZdddZdS )    )print_functionN   )_get_session)download)indent2   c                   @   s"   e Zd ZdZdddZdd ZdS )_GoogleDriveFilez"application/vnd.google-apps.folderNc                 C   s(   || _ || _|| _|d ur|ng | _d S N)idnametypechildren)selfr
   r   r   r    r   ^/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gdown/download_folder.py__init__   s    z_GoogleDriveFile.__init__c                 C   s   | j | jkS r	   )r   TYPE_FOLDER)r   r   r   r   	is_folder!   s    z_GoogleDriveFile.is_folder)N)__name__
__module____qualname__r   r   r   r   r   r   r   r      s   
r   c              	   C   st  t j|dd}d}|dD ]`}| }d|v rtd|}ztt	|dd
d}W n tyv   tdY n0  q~q|du rtd	t . tjd
td |dd}W d   n1 s0    Y  t|}|d du rg n|d }	d}
|jjd |
}t|dkr.|
|dd }ntd|jjd t| dd |tjd}dd |	D }||fS )zBExtracts information about the current page file and its children.zhtml.parser)featuresNscriptZ
_DRIVE_ivdz'((?:[^'\\]|\\.)*)'r   z*Couldn't find the folder encoded JS stringzvCannot retrieve the folder information from the link. You may need to change the permission to 'Anyone with the link'.ignore)categoryutf-8unicode_escaper   z -    z-file/folder name cannot be extracted from: {}/r
   r   r   c                 S   s.   g | ]&}|d  |d  dd|d fqS )r   r   Zraw_unicode_escaper      )encodedecode).0er   r   r   
<listcomp>_   s   z,_parse_google_drive_file.<locals>.<listcomp>)bs4ZBeautifulSoupselectZdecode_contentsrecompilefinditernext	itertoolsislicegroupStopIterationRuntimeErrorwarningscatch_warningsfilterwarningsDeprecationWarningr"   r#   jsonloadstitlecontentssplitlenjoinformatr   r   )urlcontentZfolder_soupZencoded_datar   Z
inner_htmlZ
regex_iterdecodedZ
folder_arrZfolder_contentssepZsplittedr   gdrive_fileid_name_type_iterr   r   r   _parse_google_drive_file%   s\    


.

rD   FTc              
   C   s  d}d|v r|d7 }n|d7 }z| j ||d}W nJ tjjyz } z.td| jtjd t|tjd W Y d}~d	S d}~0 0 |jd
krd	S t	||j
d\}}	|	D ]\}
}}|tjkr|std|
| |jt|
||d |s|df  S q| std|
| t| d|
 ||d\}}|s(|df  S |j| qt|jtk}|s||r|ddj|ddjtddddg}t|||fS )z0Get folder structure of Google Drive folder URL.T?z&hl=enz?hl=en)verifyz"An error has occurred using proxy:fileN)FN   )r>   r?   zProcessing filer    zRetrieving folderz'https://drive.google.com/drive/folders/)sessr>   quietremaining_ok z!The gdrive folder with url: {url})r>   zhas more than {max} files,)maxz+gdrive can't download more than this limit,zif you are ok with this,z*please run again with --remaining-ok flag.)getrequests
exceptions
ProxyErrorprintproxiessysstderrstatus_coderD   textr   r   r   append%_download_and_parse_google_drive_linkr;   MAX_NUMBER_FILESr<   r=   r1   )rJ   r>   rK   rL   rF   return_coderesr%   rB   rC   Zchild_idZ
child_nameZ
child_typechildZhas_at_least_max_fileserr_msgr   r   r   rZ   g   s~    	







	rZ   c                 C   s   g }| j D ]|}|jtjd|_| rf|dt||jf t|t||jD ]}|| qTq
|j s
||j	t||jf q
|S )zEConverts a Google Drive folder structure into a local directory list._N)
r   r   replaceosprA   r   rY   r<   _get_directory_structurer
   )rB   Zprevious_pathdirectory_structurerH   ir   r   r   rc      s     
rc   c	              
   C   s  |du | du A st d|dur,dj|d} t||d}	|sJtdtjd zt|	| |||d\}
}W nd ty } zLtd	tjd d
t	
t|}t|d}td
|d
tjd W Y d}~dS d}~0 0 |
s|
S |stdtjd tdtjd |du r
t tj }|tjr(t||j}n|}t||}t|sLt| |sZtd g }|D ]v\}}|du rt|sbt| qbtd| ||||||d}|du r|stdtjd  dS || qb|stdtjd |S )ap  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.

    Returns
    -------
    filenames: list of str
        List of files downloaded, or None if failed.

    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
   )proxyuse_cookieszRetrieving folder listrG   )rK   rL   rF   z#Failed to retrieve folder contents:
	z Retrieving folder list completedzBuilding directory structurez&Building directory structure completedzhttps://drive.google.com/uc?id=)r>   outputrK   rf   speedrg   rF   zDownload ended unsuccessfullyzDownload completed)
ValueErrorr=   r   rS   rU   rV   rZ   r1   r<   textwrapwrapstrr   osgetcwdrb   rA   endswithr   rc   existsmakedirsr   rY   )r>   r
   rj   rK   rf   rk   rg   rL   rF   rJ   r\   rB   r%   errorZroot_folderrd   	filenamesZfile_id	file_pathfilenamer   r   r   download_folder   sv    0







ry   )FFT)	NNNFNNTFT)
__future__r   r-   r6   rp   os.pathpathrb   r)   rU   rm   r2   r'   rP   r   r   r   r[   objectr   rD   rZ   rc   ry   r   r   r   r   <module>   s>   E   
S         