
    |h-                         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j$                  d      Zd Zd Zdd	Z	 	 	 	 	 	 	 	 	 	 	 dd
Zy)    )print_functionN   )	parse_urli   ~c                 >      fd}dj                   |             S )Nc               3   l   K   j                  d      D ]  } | j                         r| z   n|   y w)NT)
splitlinesstrip)lineprefixtexts    M/var/www/html/test/engine/venv/lib/python3.12/site-packages/gdown/download.pyprefixed_lineszindent.<locals>.prefixed_lines   s3     OOD) 	<D$(JJL6D=d;	<s   14 )join)r   r   r   s   `` r   indentr      s    < 77>#$$    c                 `   d}| j                         D ]
  }t        j                  d|      }|r*d|j                         d   z   }|j	                  dd      } nt        j                  d|      }|r'|j                         d   }|j	                  dd      } nt        j                  d|      }|r9|j                         d   }|j	                  d	d
      }|j	                  dd      } n8t        j                  d|      }|s|j                         d   }t        |       |st        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errors        r    get_url_from_gdrive_confirmationr        s#   
C##% &II;TB+ahhjm;C++gs+CII94@((*Q-C++gs+CII.5((*Q-C++i-C++i-CII?FHHJqMEu%%)&* A
 	

 Jr   c                    t        j                         }|j                  j                  ddi       | &| | d|_        t        d| t        j                         t        j                  t        d      }t        j                  |      rD|rBt        |      5 }t        j                  |      }d d d        D ]  \  }}||j                  |<    |r||fS |S # 1 sw Y   *xY w)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fr4   kvs	            r   _get_sessionr=   @   s    DLL	HI  %6ne#**5 88D"=>L
zz,K, 	#1iilG	# 	 DAqDLLO	  \!!	# 	#s   CCc                 N   |du | du z  st        d      |dj                  |      } | }t        ||d      \  }}t        | |       \  }}|r|rdj                  |      } | }d}	 	 |j	                  | d|      }| |k(  r"|j                  dk(  rdj                  |      } =|j                  d   j                  d      rt        j                   d|j"                        }|r:|j%                         d   j'                  d      rdj                  ||
dn|
      } |r:|j%                         d   j'                  d      rdj                  ||
dn|
      } |r|j%                         d   j'                  d      rbdj                  ||
dn|
      } 0d|j                  v r;|j                  d   j'                  d      r|
dvrdj                  ||
dn|
      } y|rt)        j*                  t)        j,                  |            s(t/        j0                  t)        j,                  |             t3        |d      5 }|j4                  j7                         D cg c]  \  }}|j                  d      s||f }}}t9        j:                  ||d        ddd       d|j                  v rn|r|sn	 t=        |j"                        } b|r|rtJ        jL                  jN                  jQ                  |j                  d         }t        j                   d&|      }|j%                         d   }|jS                  t(        jT                  d'      }nt)        jV                  |       }||}tY        |tJ        jZ                        }|r_|j'                  t(        jT                        r@t)        j*                  |      st/        j0                  |       t)        j@                  ||      }|rsg }t/        j\                  t)        j,                  |      xs d(      D ]_  }|j                  t)        jV                  |            s(|j_                  t)        j@                  t)        j,                  |      |             a |	r|rta        |      d)k7  rpt        d*t        j                  
       t        d"       |D ]  }t        d#|t        j                  
         t        d"       t        d+t        j                  
       y|d   }nNd,}	tc        jd                  tb        jf                  t)        jV                  |      t)        j,                  |      -      }t3        |d.      }nd}|}|I|ji                         dk7  r6d/d0j                  |ji                               i}|j	                  | |d|1      }|st        d2t        j                  
       |	rt        d3|t        j                  
       || k7  r9t        d4|t        j                  
       t        d5| t        j                  
       nt        d6| t        j                  
       t        d7|rt)        jj                  |      n|t        j                  
       	 |j                  j	                  d8      }|tm        |      }|sto        jn                  |d9d:      } tq        jp                         }!|js                  tt        ;      D ]y  }"|jw                  |"       |s jy                  ta        |"             |3d< jz                  z  |z  }#tq        jp                         |!z
  }$|$|#k  sbtq        j|                  |#|$z
         { |s j                          |r&|j                          t        j                  ||       |j                          |S # t
        j                  j                  $ rK}t        d	|j                  t        j                  
       t        |t        j                  
       Y d}~yd}~ww xY wc c}}w # 1 sw Y   xY w# t>        $ r}t        d!       d"jA                  tC        jD                  tG        |                  }tI        |d#      }t        d"|d"t        j                  
       t        d$t        j                  
       t        d%|d"t        j                  
       Y d}~yd}~ww xY w# t        $ r5}t        |t        j                  
       Y d}~|j                          yd}~ww xY w# |j                          w xY w)=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)r5   r6   r7   )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}docx)r?   formatz - Google SheetszBhttps://docs.google.com/spreadsheets/d/{id}/export?format={format}xlsxz - Google SlideszBhttps://docs.google.com/presentation/d/{id}/export?format={format}pptxzContent-Disposition>   NrF   wdownload_warning_   )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(   rA   rB   zDownloading...zResume:zFrom (uriginal):zFrom (redirected):zFrom:zTo:zContent-LengthB)totalunit
unit_scale)
chunk_sizeg      ?)C
ValueErrorrD   r=   r   getr&   
exceptions
ProxyErrorr+   r*   r,   r-   status_coder(   
startswithr   r   r   r   endswithr.   r0   dirnameosmakedirsr1   r4   itemsr2   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quietr5   speedr6   rB   r?   fuzzyresumerD   
url_originr8   r9   gdrive_file_idis_gdrive_download_linkreser   r:   r;   r<   r4   r   content_dispositionfilename_from_urloutput_is_pathexisting_tmp_filesr%   tmp_filer(   rS   pbart_startchunkelapsed_time_expectedelapsed_times%                                        r   downloadr   Y   s   f $J3$;'?@@	~3::b:AJ%$D, /8Y.O+N+3::n:M
"&
		((3tF(;C *C!79@@! A C ;;~&11+>		/:AQXXZ]++,<=''-v))/vV (. (  qxxz!}--.@A''-v))/vV (. (  qxxz!}--.@A''-v))/vV (. (  !S[[012;;FCn,##)6%%+^6 $* $  ::ckk,78CKK56lC( 0A !% 2 2 41<<(;< F 
 		'1Q/0 !CKK/#:	2388<Ce ~ 1!ii44<<KK-.
 II/1DEHHJqM-55cggsCLL-~"(8(89N&//#''2zz&!KK&"34JJs{{629c: 	ODs||F34"))#((3;;v3F*MN	O (%&!+C d. 7D$3::67dJ )!,HF  ((||F+KK'H
 4 AK..qvvx89hhsGDhHSZZ0)XCJJ7$jszzB&#**='3SZZ0#1CKKv	
 01JE995stDD))+%%%< 	EEGGENCJ' (+dffu(<%#yy{W4"77JJ4|CD	E JJLGGIKK&)
 	

MA ""-- 	4ZZ
 !#**%	@0 0"  
	;<IIhmmCF34E5$'E$t#**5LZZ &*d<
	T  acjj!

	 	

s   )^$ >``;`3`" 0Bc .c >Ac $`A````"	c+Bc		c	dd
5d 
dd d$)F)NNFNNTTNFFN)
__future__r   r2   r_   os.pathpathr.   r   r   r,   rq   rc   rx   r&   rf   rw   r   rz   
expanduserr/   r   r    r=   r    r   r   <module>r      s}    %  	  	  
     
   
s~~c%@4 	



Ir   