a
    
d-                     @   s   d dl m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Zd dlZd dlZddlmZ dZedZdd Zd	d
 ZdddZdddZdS )    )print_functionN   )	parse_urli   ~c                    s    fdd}d | S )Nc                  3   s*    dD ]} |  r |  n| V  q
d S )NT)
splitlinesstrip)lineprefixtext W/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gdown/download.pyprefixed_lines   s    zindent.<locals>.prefixed_lines )join)r   r
   r   r   r	   r   indent   s    r   c                 C   s   d}|   D ]}td|}|r@d| d  }|dd} qtd|}|rl| d }|dd} qtd|}|r| d }|d	d
}|dd} qtd|}|r| d }t|q|std|S )Nr   z"href="(\/uc\?export=download[^"]+)zhttps://docs.google.comr   z&amp;&z!id="download-form" action="(.+?)"z"downloadUrl":"([^"]+)z\u003d=z\u0026z'<p class="uc-error-subcaption">(.*)</p>zCannot retrieve the public link of the file. You may need to change the permission to 'Anyone with the link', or have had many accesses.)r   researchgroupsreplaceRuntimeError)contentsurlr   merrorr   r   r    get_url_from_gdrive_confirmation    s6    
r   Fc           	      C   s   t  }|jddi | d ur<| | d|_td| tjd t	t
d}t|r|rt|}t|}W d    n1 s~0    Y  |D ]\}}||j|< q|r||fS |S d S )Nz
User-Agentz/Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6))httphttpszUsing proxy:filez.cache/gdown/cookies.json)requestssessionheadersupdateproxiesprintsysstderrospr   homeexistsopenjsonloadcookies)	proxyuse_cookiesreturn_cookies_filesesscookies_filefr0   kvr   r   r   _get_session@   s     
(r9   Tc           #   
   C   s  |du | du A st d|dur,dj|d} | }t||dd\}}t| | d\}}|rp|rpdj|d} | }d}z|j| d|d}W nJ tjjy } z.td	|j	t
jd
 t|t
jd
 W Y d}~dS d}~0 0 | |kr|jdkrdj|d} qp|jd drtd|j}|rJ| d drJdj||
du r>dn|
d} qpnr|r| d drdj||
du rxdn|
d} qpn8|r| d drdj||
du rdn|
d} qpnFd|jv r|jd dr|
dvrdj||
du rdn|
d} qp|r|tt|s,tt| t|d4}dd |j D }tj||d d! W d   n1 sr0    Y  d|jv rq.|r.|sq.zt|j} W qp ty* } zftd" d# t!"t#|}t$|d$}td#|d#t
jd
 td%t
jd
 td&|d#t
jd
 W Y d}~dS d}~0 0 qp|rv|rvt%j&j'(|jd }td'|}| d }|)tj*d(}n
t+| }|du r|}t,|t%j-}|r|tj*rt|st| t ||}|rg }t.t|pd)D ]0}|t+|r|/t t|| q|	r|rt0|d*krtd+t
jd
 td# |D ]}td$|t
jd
 qVtd# td,t
jd
 dS |d }n"d-}	t1j2t1j3t+|t|d.}t|d/}nd}|}|dur|4 dkrd0d1|4 i}|j| |d|d2}|std3t
jd
 |	r.td4|t
jd
 || krZtd5|t
jd
 td6| t
jd
 ntd7| t
jd
 td8|r~t5|n|t
jd
 z$z|jd9}|durt6|}|st7j7|d:dd;}t88 }|j9t:d<D ]`} |;|  |s|<t0|  |durd=|j= | }!t88 | }"|"|!k rt8>|!|"  q|sH|?  |rb|?  t@A|| W n@ tBy } z&t|t
jd
 W Y d}~W |?  dS d}~0 0 W |?  n
|?  0 |S )>a  Download file from URL.

    Parameters
    ----------
    url: str
        URL. Google Drive URL is also supported.
    output: str
        Output filename. Default is basename of URL.
    quiet: bool
        Suppress terminal output. Default is False.
    proxy: str
        Proxy.
    speed: float
        Download byte size per second (e.g., 256KB/s = 256 * 1024).
    use_cookies: bool
        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.
    id: str
        Google Drive's file ID.
    fuzzy: bool
        Fuzzy extraction of Google Drive's file Id. Default is False.
    resume: bool
        Resume the download from existing tmp file if possible.
        Default is False.
    format: str, optional
        Format of Google Docs, Spreadsheets and Slides. Default is:
            - Google Docs: 'docx'
            - Google Spreadsheet: 'xlsx'
            - Google Slides: 'pptx'

    Returns
    -------
    output: str
        Output filename.
    Nz$Either url or id has to be specifiedz#https://drive.google.com/uc?id={id})idT)r1   r2   r3   )warning)streamverifyz"An error has occurred using proxy:r    i  z%https://drive.google.com/open?id={id}zContent-Typez	text/htmlz<title>(.+)</title>r   z - Google Docsz>https://docs.google.com/document/d/{id}/export?format={format}Zdocx)r:   formatz - Google SheetszBhttps://docs.google.com/spreadsheets/d/{id}/export?format={format}Zxlsxz - Google SlideszBhttps://docs.google.com/presentation/d/{id}/export?format={format}pptxzContent-Disposition>   Nr?   wc                 S   s"   g | ]\}}| d s||fqS )Zdownload_warning_)
startswith).0r7   r8   r   r   r   
<listcomp>   s   
zdownload.<locals>.<listcomp>   )r   z'Access denied with the following error:
	z:You may still be able to access the file from the browser:z
	zfilename\*=UTF-8''(.*)_.r   z-There are multiple temporary files to resume:z4Please remove them except one to resume downloading.F)suffixr
   dirabRangez	bytes={}-)r$   r<   r=   zDownloading...zResume:zFrom (uriginal):zFrom (redirected):zFrom:zTo:zContent-LengthB)totalunitZ
unit_scale)
chunk_sizeg      ?)C
ValueErrorr>   r9   r   getr"   
exceptions
ProxyErrorr'   r&   r(   r)   status_coder$   rA   r   r   r   r   endswithr*   r,   dirnameosmakedirsr-   r0   itemsr.   dumpr   r   r   textwrapwrapstrr   sixmovesurllib_parseunquoter   sepbasename
isinstancestring_typeslistdirappendlentempfilemktemptemplatetellabspathinttqdmtimeiter_content
CHUNK_SIZEwriter%   nsleepcloseshutilmoveIOError)#r   outputquietr1   speedr2   r=   r:   Zfuzzyresumer>   Z
url_originr4   r5   Zgdrive_file_idZis_gdrive_download_linkreser   r6   r0   r   content_dispositionZfilename_from_urlZoutput_is_pathZexisting_tmp_filesr!   Ztmp_filer$   rN   ZpbarZt_startchunkZelapsed_time_expectedZelapsed_timer   r   r   downloadY   sb   3
0









r   )F)NNFNNTTNFFN)
__future__r   r.   rX   os.pathpathr*   r   rx   r(   rj   r\   rq   r"   r_   rp   r   rs   
expanduserr+   r   r   r9   r   r   r   r   r   <module>   s<   
 
           