
    |hD%                         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	e      Zd
 Z	 	 	 ddZd Z	 	 	 	 	 	 	 	 	 ddZy)    )print_functionN   )_get_session)download)indent2   c                       e Zd ZdZddZd Zy)_GoogleDriveFilez"application/vnd.google-apps.folderNc                 P    || _         || _        || _        ||| _        y g | _        y N)idnametypechildren)selfr   r   r   r   s        T/var/www/html/test/engine/venv/lib/python3.12/site-packages/gdown/download_folder.py__init__z_GoogleDriveFile.__init__   s)    		$,$8b    c                 4    | j                   | j                  k(  S r   )r   TYPE_FOLDER)r   s    r   	is_folderz_GoogleDriveFile.is_folder!   s    yyD,,,,r   r   )__name__
__module____qualname__r   r   r    r   r   r
   r
      s    6KA-r   r
   c                 f   t        j                  |d      }d}|j                  d      D ]l  }|j                         }d|v st	        j
                  d      j                  |      }	 t        t        j                  |dd            j                  d      } n |t        d	      t        j                         5  t        j                  d
t                |j#                  d      j%                  d      }ddd       t'        j(                        }|d   g n|d   }	d}
|j*                  j,                  d   j/                  |
      }t1        |      dk\  r|
j3                  |dd       }n1t        dj5                  |j*                  j,                  d               t7        | j/                  d      d   |t6        j8                        }|	D cg c].  }|d   |d   j#                  d      j%                  d      |d   f0 }}||fS # t        $ r t        d      w xY w# 1 sw Y   ,xY wc c}w )zBExtracts information about the current page file and its children.zhtml.parser)featuresNscript
_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)categoryzutf-8unicode_escaper   z -    z-file/folder name cannot be extracted from: {}/r   r   r   raw_unicode_escape   )bs4BeautifulSoupselectdecode_contentsrecompilefinditernext	itertoolsislicegroupStopIterationRuntimeErrorwarningscatch_warningsfilterwarningsDeprecationWarningencodedecodejsonloadstitlecontentssplitlenjoinformatr
   r   )urlcontentfolder_soupencoded_datar   
inner_html
regex_iterdecoded
folder_arrfolder_contentssepsplittedr   gdrive_fileeid_name_type_iters                   r   _parse_google_drive_filerR   %   sG    ##GmDK L$$X. ++-
:%$:;DDJ#$$ZD9 %(  #& &
 	
 
	 	 	" H3EF%%g.556FGH G$J&qM1bz!}O
C  ))!,2237H
8}xx"&;BB!!**1-
 	
 #99S>"))K ! 
1qt{{/077@!A$G 
 )))W ! "@ H H0s   (/H	;<H!3H.	H!H+c                    d}d|v r|dz  }n|dz  }	 | j                  ||      }|j                  d
k7  ry	t        ||j                        \  }}	|	D ]  \  }
}}|t        j                  k7  r?|st	        d|
|       |j                  j                  t        |
||             |s|dfc S Y|st	        d|
|       t        | d|
z   ||      \  }}|s|dfc S |j                  j                  |        t!        |j                        t"        k(  }|sG|rEdj%                  dj'                  |      dj'                  t"              dddg      }t)        |      ||fS # t        j                  j                  $ rK}t	        d| j
                  t        j                         t	        |t        j                         Y d}~y	d}~ww xY w)z0Get folder structure of Google Drive folder URL.T?z&hl=enz?hl=en)verifyz"An error has occurred using proxy:fileN)FN   )rD   rE   zProcessing filer&   zRetrieving folderz'https://drive.google.com/drive/folders/)sessrD   quietremaining_ok z!The gdrive folder with url: {url})rD   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_coderR   textr
   r   r   append%_download_and_parse_google_drive_linkrA   MAX_NUMBER_FILESrB   rC   r5   )rY   rD   rZ   r[   rU   return_coderesrP   rO   rQ   child_id
child_name
child_typechildhas_at_least_max_fileserr_msgs                   r   ri   ri   g   s    K czxxhhs6h* #%=&"K"
 -> !+(*j)555%
   '' ## "D((#
 C9HD%	
U $$##E*C!+D !!5!56:JJ2((3::s:C,338H3I=*<
 7####{ )) 0$,,SZZ	
 	acjj!s   E G:AG  Gc                    g }| j                   D ]  }|j                  j                  t        j                  d      |_        |j                         rr|j                  dt        j                  ||j                        f       t        |t        j                  ||j                              D ]  }|j                  |        |j                   r|j                  |j                  t        j                  ||j                        f        |S )zEConverts a Google Drive folder structure into a local directory list._N)
r   r   replaceosprM   r   rh   rB   _get_directory_structurer   )rO   previous_pathdirectory_structurerW   is        r   rw   rw      s     $$ II%%cggs3	>>&&sxxtyy9: .chh}dii8 . $**1-. &&#((=$))<= r   c	           
         |du | du z  st        d      |dj                  |      } t        ||      }	|st        dt        j
                         	 t        |	| |||      \  }
}|
s|
S |s6t        dt        j
                         t        dt        j
                         |%t        j                         t        j                   z   }|j#                  t        j                         r!t        j                  ||j$                        }n|}t'        ||      }t        j(                  |      st        j*                  |       |st        d       g }|D ]y  \  }}|+t        j(                  |      st        j*                  |       3t-        d|z   ||||||      }||st        dt        j
                          y|j/                  |       { |st        dt        j
                         |S # t        $ r{}t        d	t        j
                         d
j                  t        j                  t        |                  }t        |d      }t        d
|d
t        j
                         Y d}~yd}~ww xY w)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 listrV   )rZ   r[   rU   z#Failed to retrieve folder contents:
	z Retrieving folder list completedzBuilding directory structurez&Building directory structure completedzhttps://drive.google.com/uc?id=)rD   outputrZ   r|   speedr}   rU   zDownload ended unsuccessfullyzDownload completed)
ValueErrorrC   r   rb   rd   re   ri   r5   rB   textwrapwrapstrr   osgetcwdrv   rM   endswithr   rw   existsmakedirsr   rh   )rD   r   r   rZ   r|   r   r}   r[   rU   rY   rk   rO   rP   errorroot_folderry   	filenamesfile_id	file_pathfilenames                       r   download_folderr      s   ` $J3$;'?@@	~;BBbBIe=D&SZZ8#H%$
 [ 0szzB,3::>~sww&swwhhv{'7'782;L::k"
K 67I1 #?::i(I&1G;#
 5CJJG")#* "4a  3#**E		(--A/0ud#dE4cjj1s   G. .	I27A1I--I2)FFT)	NNNFNNTFT)
__future__r   r1   r<   r   os.pathpathrv   r-   rd   r   r6   r)   r_   r   r   r   rj   objectr
   rR   ri   rw   r   r   r   r   <module>r      s    &   	  	 
   
  "   
-v 
-?*J P$f, 	


qr   