o
    ¾Ùøi£9  ã                   @   sú  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Zd dl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ZddlmZ ddlmZ ddlmZ d	Ze d
¡Zde de fdd„Z!de de fdd„Z"dej#de dB fdd„Z$dej#de j dB fdd„Z%de dB de&de de'ej(e f fdd„Z)													d*de dB de eB dB d e&de dB d!e*dB de&d"e&e B d#e dB d$e&d%e dB de dB d&e+e e f dB d'ee,e,dB gdf dB de eB fd(d)„Z-dS )+é    N)ÚCallable)ÚMozillaCookieJar)ÚBinaryIOé   )ÚDownloadError)ÚFileURLRetrievalError)Ú	parse_urli   ú~ÚcontentsÚreturnc                 C   s€  d}|   ¡ D ]±}t d|¡}|r d| ¡ d  }| dd¡} n˜tj|dd�}| d	¡}|d ur‰|d
 }t|t	ƒs;J ‚| dd¡}t
j |¡}t
j |j¡}|jdddid�D ]}	|	d }
|	d }t|
t	ƒshJ ‚t|t	ƒsoJ ‚|g||
< qWt
jj|dd�}t
j |j|d�¡} n/t d|¡}|r¥| ¡ d }| dd¡}| dd¡} nt d|¡}|r·| ¡ d }t|ƒ‚q|s¾tdƒ‚|S )NÚ z"href="(\/uc\?export=download[^"]+)zhttps://docs.google.comr   z&amp;ú&zhtml.parser)Úfeaturesz#download-formÚactionÚinputÚtypeÚhidden)ÚattrsÚnameÚvalueT)Údoseq)Úqueryz"downloadUrl":"([^"]+)z\u003dú=z\u0026z'<p class="uc-error-subcaption">(.*)</p>zÏCannot retrieve the public link of the file. 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.)Ú
splitlinesÚreÚsearchÚgroupsÚreplaceÚbs4ÚBeautifulSoupÚ
select_oneÚ
isinstanceÚstrÚurllibÚparseÚurlsplitÚparse_qsr   Úfind_allÚ	urlencodeÚ
urlunsplitÚ_replacer   )r
   ÚurlÚlineÚmÚsoupÚformr   Úurl_componentsÚquery_paramsÚparamÚ
param_nameÚparam_valuer   Úerror© r6   ú[/home/develop/edictos_py/central_edictos/env/lib/python3.10/site-packages/gdown/download.pyÚ get_url_from_gdrive_confirmation   sP   
þÿr8   Úfilenamec                 C   s4   |   dd¡} |   dd¡  dd¡ ¡ } | dv rdS | S )Nú r   ú/Ú_ú\)r   Ú.z..)r   Ústrip©r9   r6   r6   r7   Ú_sanitize_filenameJ   s
   rA   Úresponsec                 C   sZ   t j | jd ¡}t d|¡}|rt| ¡ d d�S t d|¡}|r+t| ¡ d d�S d S )NúContent-Dispositionzfilename\*=UTF-8''(.*)r   r@   zattachment; filename="(.*?)")r#   r$   ÚunquoteÚheadersr   r   rA   r   )rB   Úcontent_dispositionr-   r6   r6   r7   Ú_get_filename_from_responseR   s   rG   c                 C   s0   d| j vrd S | j d }|d u rd S tj |¡S )NzLast-Modified)rE   ÚemailÚutilsÚparsedate_to_datetime)rB   Úrawr6   r6   r7   Ú _get_modified_time_from_response`   s   

rL   ÚproxyÚuse_cookiesÚ
user_agentc              
   C   sÊ   t  ¡ }|j d|i¡ | d ur| | dœ|_td| tjd� t 	t
d¡}|rat |¡rat|ƒ}z| ¡  |j |¡ W ||fS  ty` } ztjd|› d|› �dd	� W Y d }~||fS d }~ww ||fS )
Nz
User-Agent)ÚhttpÚhttpszUsing proxy:©Úfilez.cache/gdown/cookies.txtzFailed to load cookies from z: é   )Ú
stacklevel)ÚrequestsÚsessionrE   ÚupdateÚproxiesÚprintÚsysÚstderrÚospÚjoinÚhomeÚexistsr   ÚloadÚcookiesÚOSErrorÚwarningsÚwarn)rM   rN   rO   ÚsessÚcookies_fileÚ
cookie_jarÚer6   r6   r7   Ú_get_sessionm   s*   úþ€úrj   FTr+   ÚoutputÚquietÚspeedÚverifyÚidÚresumeÚformatÚlog_messagesÚprogressc           )      C   sd  |du | du A st dƒ‚|durd|› �} | dusJ ‚|
du r!d}
|du r'i }| }t|||
d�\}}t| d�\}}|rDd|› �} | }d}	 |j| d|d�}|rQ|sRnÇ| |kra|jd	krad
|› �} qD|jd  d¡r²d|jv r�d|jvr�dj||	du r|dn|	d�} qDd|jv r™d|jvr™dj||	du r”dn|	d�} qDd|jv r±d|jvr±dj||	du r¬dn|	d�} qDnd|jv rÑ|jd  	d¡rÑ|	dvrÑdj||	du rÌdn|	d�} qD|ræt
|ƒ}|jD ]}| |¡ qÚ| ¡  d|jv rìn-zt|jƒ} W n$ t�y } zd tjd t t|ƒ¡¡dd�|¡}t|ƒ‚d}~ww qEd}d}|�r-|�r-t|d�}t|d�}|du �r:tt | ¡d�}|du �rA|}t|tƒ�rd| 	d ¡�sSt |¡�rdt |¡�s^t |¡ t ||¡}t|tƒ�r|�rƒtj  !|¡�rƒ|�s�t"d!|› �t#j$d"� |S g }t %t &|¡�pŽd#¡D ]}| t |¡¡�r­| 	d$¡�r­| 't t &|¡|¡¡ �q�|�rä|�rät(|ƒd%k�rßd&d'g}|D ]}| 'd|› �¡ �qÂ| 'd'¡ | 'd(¡ t)d |¡ƒ‚|d) }nd*}t*j+d$t |¡t &|¡d*d+�}|j,}| -¡  t.|d,ƒ}nd}|}|du�r(| /¡ d)k�r(| /¡ }d-d.|› d/�i} |j| | d|d0�}nd)}|�s}t"| d1d2¡t#j$d'd3� |�rDt"d4|t#j$d"� || k�rZt"d5|t#j$d"� t"d6| t#j$d"� nt"d7| t#j$d"� t"| d8d9t|tƒ�rrt 0|¡n|› d�¡t#j$d'd3� z¯|j d:¡}!|!du�r�t1|!ƒ| }!|�s›t2j2|!d;|dd<�}"t3 3¡ }#d)}$|j4t5d=�D ]?}%| 6|%¡ |$t(|%ƒ7 }$|�s¾|" 7t(|%ƒ¡ |du�rÊ||$| |!ƒ |du�rå|$| }&t3 3¡ |# }'|'|&k �råt3 8|&|' ¡ �q§|�sî|" -¡  |�r| -¡  t|tƒ�sýJ ‚t9 :||¡ t|tƒ�r|�r&| ;¡ }(t <||(|(f¡ W | -¡  |S W | -¡  |S W | -¡  |S | -¡  w )>a©  Download file from URL.

    Parameters
    ----------
    url:
        URL. Google Drive URL is also supported.
    output:
        Output filename/directory. Default is basename of URL.
        If output is an existing directory or ends with a path separator,
        the basename will be appended automatically.
    quiet:
        Suppress terminal output. Default is False.
    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.
    id:
        Google Drive's file ID.
    resume:
        Resume interrupted downloads while skipping completed ones.
        Default is False.
    format:
        Format of Google Docs, Spreadsheets and Slides. Default is:
            - Google Docs: 'docx'
            - Google Spreadsheet: 'xlsx'
            - Google Slides: 'pptx'
    user_agent:
        User-agent to use in the HTTP request.
    log_messages:
        Log messages to customize. Currently it supports:
        - 'start': the message to show the start of the download
        - 'output': the message to show the output filename
    progress:
        Callback called after each chunk: ``progress(bytes_so_far, bytes_total)``.
        *bytes_total* is None when Content-Length is unavailable.
        Raise any exception from the callback to abort the download.

    Returns
    -------
    output:
        Output filename.

    Raises
    ------
    ValueError
        If neither url nor id is specified, or both are specified.
    FileURLRetrievalError
        If the file URL cannot be retrieved from Google Drive.
    DownloadError
        If the download fails (e.g., multiple temporary files exist during
        resume).
    Nz$Either url or id has to be specifiedzhttps://drive.google.com/uc?id=zxMozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36)rM   rN   rO   )r+   T)Ústreamrn   iô  z!https://drive.google.com/open?id=zContent-Typez	text/htmlz
/document/z/exportz>https://docs.google.com/document/d/{id}/export?format={format}Údocx)ro   rq   z/spreadsheets/zBhttps://docs.google.com/spreadsheets/d/{id}/export?format={format}Úxlsxz/presentation/zBhttps://docs.google.com/presentation/d/{id}/export?format={format}ÚpptxrC   >   Nrw   z�Failed to retrieve file url:

{}

You may still be able to access the file from the browser:

	{}

but Gdown can't. Please check connections and permissions.Ú
ú	)Úprefix)rB   r@   )r;   r=   z!Skipping already downloaded file rR   r>   z.partr   z-There are multiple temporary files to resume:r   z4Please remove them except one to resume downloading.r   F)Úsuffixrz   ÚdirÚdeleteÚabÚRangezbytes=ú-)rE   rt   rn   ÚstartzDownloading...
)rS   ÚendzResume:zFrom (original):zFrom (redirected):zFrom:rk   zTo: zContent-LengthÚB)ÚtotalÚunitÚinitialÚ
unit_scale)Ú
chunk_size)=Ú
ValueErrorrj   r   ÚgetÚstatus_coderE   Ú
startswithr+   rq   Úendswithr   rb   Ú
set_cookieÚsaver8   Útextr   ÚtextwrapÚindentr^   Úwrapr"   rG   rL   rA   r]   Úbasenamer!   Úisdirr`   ÚosÚmakedirsÚpathÚisfilerZ   r[   r\   ÚlistdirÚdirnameÚappendÚlenr   ÚtempfileÚNamedTemporaryFiler   ÚcloseÚopenÚtellÚabspathÚintÚtqdmÚtimeÚiter_contentÚ
CHUNK_SIZEÚwriterX   ÚsleepÚshutilÚmoveÚ	timestampÚutime))r+   rk   rl   rM   rm   rN   rn   ro   rp   rq   rO   rr   rs   Ú
url_originrf   rg   Úgdrive_file_idÚis_gdrive_download_linkÚresrh   Úcookieri   ÚmessageÚfilename_from_urlÚlast_modified_timeÚexisting_tmp_filesrS   ÚlinesÚtmp_fileÚtmp_file_objÚfÚ
start_sizerE   r„   ÚpbarÚt_startÚ
downloadedÚchunkÚelapsed_time_expectedÚelapsed_timeÚmtimer6   r6   r7   Údownload‰   sx  I

ý

ýÿýÿýÿ€
ÿýÿ

þû	€ö¾N



$
€


ü
 þú	




€úú
þrÄ   )NNFNNTTNFNNNN).ÚdatetimeÚemail.utilsrH   r–   Úos.pathr˜   r]   r   r«   r[   rž   r‘   r¦   Úurllib.parser#   rd   Úcollections.abcr   Úhttp.cookiejarr   Útypingr   r   rV   r¥   Ú
exceptionsr   r   r   r¨   Ú
expanduserr_   r"   r8   rA   ÚResponserG   rL   ÚboolÚtupleÚSessionrj   ÚfloatÚdictr¤   rÄ   r6   r6   r6   r7   Ú<module>   s¤    
-ÿ
þÿþý
üóÿ
þýüûúùø	÷
öõôóò