
    yi(                    b   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mZ ddlmZ ddlmZ ddlmZ  G d d	      Z	 	 	 	 	 	 dd
Z ej(                  dd      Z	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 d	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 ddZy)    )annotationsN   )_get_session)_sanitize_filename)download)DownloadErrorc                  6    e Zd ZdZ	 d	 	 	 	 	 	 	 	 	 ddZddZy)_GoogleDriveFilez"application/vnd.google-apps.folderNc                P    || _         || _        || _        ||| _        y g | _        y N)idnametypechildren)selfr   r   r   r   s        I/home/ubuntu/.local/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   )
r   strr   r   r   r   r   zlist[_GoogleDriveFile] | NonereturnNone)r   bool)__name__
__module____qualname__r   r   r    r   r   r
   r
      sL    6K 37
A
A 
A 	
A
 0
A 

A-r   r
   c                   g }| j                   D ]  }t        |j                        |_        |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.filenameN)	r   r   r   r   appendospjoin_get_directory_structurer   )gdrive_fileprevious_pathdirectory_structurefileis        r   r&   r&   &   s    
 $$ V&		:	>>&&chh}dii.P'QR-dCHH]DII4VW .#**1-.&&-1S'TUV r   GoogleDriveFileToDownloadr   path
local_pathc                   |du | du z  st        d      ||}n| J t        |       }|d}t        |||      \  }}|st        dt        j
                         t        ||||      }t        |j                        |_        |s6t        d	t        j
                         t        d
t        j
                         t        |d      }|st        dt        j
                         |%t        j                         t        j                  z   }|j                  t        j                        r!t        j                  ||j                        }n|}|	s*t        j                   |      st        j"                  |       g }|D ]  \  }}t        j                  ||      }|-|	s*t        j                   |      st        j"                  |       K|	r|j%                  t'        |||             kt        j(                  |      d   r|}n&t        j*                  |      t        j                  z   }t-        d|z   |||||||
      }|j%                  |        |st        dt        j
                         |S )a  Downloads entire folder from URL.

    Parameters
    ----------
    url:
        URL of the Google Drive folder.
        Must be of the format 'https://drive.google.com/drive/folders/{url}'.
    id:
        Google Drive's folder ID.
    output:
        String containing the path of the output folder.
        Defaults to current working directory.
    quiet:
        Suppress terminal output.
    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.
    user_agent:
        User-agent to use in the HTTP request.
    skip_download:
        If True, return the list of files to download without downloading them.
        Defaults to False.
    resume:
        Resume interrupted transfers.
        Completed output files will be skipped.
        Partial tempfiles will be reused, if the transfer is incomplete.
        Default is False.

    Returns
    -------
    files:
        If skip_download is False, list of local file paths downloaded.
        If skip_download is True, list of GoogleDriveFileToDownload that contains
        id, path, and local_path.

    Raises
    ------
    ValueError
        If neither url nor id is specified, or both are specified.
    DownloadError
        If a file in the folder fails to download.

    Example
    -------
    gdown.download_folder(
        "https://drive.google.com/drive/folders/" +
        "1ZXEhzbLRLU1giKKRJkjm8N04cO_JoYE2",
    )
    Nz$Either url or id has to be specifiedzyMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36)proxyuse_cookies
user_agentzRetrieving folder contents)r*   sess	folder_idquietverifyr!   z$Retrieving folder contents completedzBuilding directory structure )r(   z&Building directory structure completedr-   r   zhttps://drive.google.com/uc?id=)urloutputr7   r1   speedr2   r8   resumezDownload completed)
ValueError_extract_folder_idr   printsysstderr%_download_and_parse_google_drive_linkr   r   r&   osgetcwdr$   sependswithr%   existsmakedirsr#   r,   splitextdirnamer   )r:   r   r;   r7   r1   r<   r2   r8   r3   skip_downloadr=   r6   r5   _r'   r)   root_dirfilesr.   r/   download_outputs                        r   download_folderrQ   <   s   J $J3$;'?@@	~	&s+	 Q
KJWGD!*<7	K *;3C3CDK43::F,3::>2;bQ6SZZH~sww&sww88FK$4$45H!5
HE' %DXXh-
: J)?J'LL)RdzR ||J'*","%++j"9CGG"C!5:&'	J LL$=%> "4Lr   c                    t         j                  j                  |       j                  j	                  d      j                  d      d   S )N/)urllibparseurlparser.   rstripsplit)r:   s    r   r?   r?      s8    <<  %**11#6<<SA"EEr   c                    t         j                  j                  d|i      }d| }| j                  ||      }|j                  dk7  rt        d| d|j                   d      t        j                  |j                  d	      }|j                  |j                  j                  t        d
| d      |j                  j                  }g }|j                  d      D ]'  }	|	j                  dd      }
t        |
t              s't        j                  d|
      }|r8|j!                  d      }|	j#                  d      }|j%                  ||df       xt        j                  d|
      }|r8|j!                  d      }|	j#                  d      }|j%                  ||df       t        j                  d|
      }|s|j!                  d      }|	j#                  d      }|j%                  ||t&        j(                  f       * ||fS )Nr   z,https://drive.google.com/embeddedfolderview?)r8      z2Failed to retrieve folder contents for folder ID: z (status code z). 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.zhtml.parser)featuresz/Failed to parse folder contents for folder ID: z&. The page structure may have changed.a)r   hrefr9   z3https://drive\.google\.com/file/d/([-\w]{25,})/view)patternstringr   T)stripzapplication/octet-streamz-https://docs\.google\.com/\w+/d/([-\w]{25,})/z5https://drive\.google\.com/drive/folders/([-\w]{25,}))rU   rV   	urlencodegetstatus_coder   bs4BeautifulSouptexttitler`   find_all
isinstancer   rematchgroupget_textr#   r
   r   )r5   r6   r8   paramsr:   ressoupfolder_namer   a_tagr^   
file_matchfile_id	file_name
docs_matchfolder_matchchild_folder_id
child_names                     r   _parse_embedded_folder_viewr{      s   
 \\##T9$56F8
AC
((3v(
&C
#@ LOO, -UU
 	
 SXX>DzzTZZ..6=i[ I3 3
 	
 **##K+-HC( "yy$$$XXJ

  &&q)GT2IOOWi1KLM XXD

  &&q)GT2IOOWi1KLMxxL
 *003Od3JOO_j:J:V:VWXE"H ""r   c                |   t        | ||      \  }}t        ||t        j                        }|D ]  \  }}}	|	t        j                  k7  r7|st        d||       |j                  j                  t        |||	             Q|st        d||       t        | |||      }
|j                  j                  |
        |S )N)r5   r6   r8   )r   r   r   zProcessing filezRetrieving folderr4   )r{   r
   r   r@   r   r#   rC   )r5   r6   r7   r8   rr   r   r'   child_idrz   
child_typechilds              r   rC   rC     s     8YvK #))K -5 +(*j)555%
   '' ## #
 6	
 	##E*;+< r   )r'   r
   r(   r   r   zlist[tuple[str | None, str]])NNNFNNTTNFF)r:   
str | Noner   r   r;   r   r7   r   r1   r   r<   zfloat | Noner2   r   r8   
bool | strr3   r   rL   r   r=   r   r   z+list[str] | list[GoogleDriveFileToDownload])r:   r   r   r   )T)r5   requests.Sessionr6   r   r8   r   r   z&tuple[str, list[tuple[str, str, str]]])FT)
r5   r   r6   r   r7   r   r8   r   r   r
   )
__future__r   collectionsrD   os.pathr.   r$   rk   rA   urllib.parserU   re   requestsr   r   r   
exceptionsr   r
   r&   
namedtupler,   rQ   r?   r{   rC   r   r   r   <module>r      s   "  	  	 
  
  " (  %- -&!25!" 3K22!=  !O	OO O 	O
 O O O O O O O 1OdF ?#
?#?# ?# ,	?#J 	.
.. . 	.
 .r   