o
    
j                     @   s&  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
mZmZmZ d dlmZ 	d$dedee d	ed
edef
ddZ					d%dededee dee ded
ededdfddZd&de
dededefddZd'dedee dedee fddZded ed!efd"d#ZdS )(    N)
expanduser)AnyIterableListOptional)tqdm   Turl
start_byte
block_sizeprogress_barreturnc              	   c   s,   t jj| dd}t j|}t| dd}W d   n1 s$w   Y  ||kr/dS t j| }|r?d||jd< t j|G}t	dd	d
|| d$}d}		 |
|}
|
s\n|
V  |	t|
7 }	|t|
 qTW d   n1 sww   Y  W d   dS W d   dS 1 sw   Y  dS )aD  Stream url by chunk

    Args:
        url (str): Url.
        start_byte (int or None, optional): Start streaming at that point (Default: ``None``).
        block_size (int, optional): Size of chunks to stream (Default: ``32 * 1024``).
        progress_bar (bool, optional): Display a progress bar (Default: ``True``).
    HEADmethodContent-LengthNz	bytes={}-RangeBTi   )unit
unit_scaleunit_divisortotaldisabler   )urllibrequestRequesturlopenintinfogetformatheadersr   readlenupdate)r	   r
   r   r   reqresponseurl_sizeupointerpbar	num_byteschunk r-   E/home/kuhnn/.local/lib/python3.10/site-packages/TTS/utils/download.py
stream_url   s:   
Pr/   sha256Fdownload_folderfilename
hash_value	hash_typeresumec                 C   s  t jj| dd}t j| }|p| ptj| }tj	||}	|r4tj
|	r4d}
tj|	}n|sCtj
|	rCtd|	d}
d}|r~|t|ddkr~t|	d	}t|||rh	 W d   dS W d   n1 srw   Y  td
|	t|	|
}t| ||dD ]}|| qW d   n1 sw   Y  t|	d	 }|rt|||std
|	W d   dS W d   dS 1 sw   Y  dS )aP  Download file to disk.

    Args:
        url (str): Url.
        download_folder (str): Folder to download file.
        filename (str or None, optional): Name of downloaded file. If None, it is inferred from the url
            (Default: ``None``).
        hash_value (str or None, optional): Hash for url (Default: ``None``).
        hash_type (str, optional): Hash type, among "sha256" and "md5" (Default: ``"sha256"``).
        progress_bar (bool, optional): Display a progress bar (Default: ``True``).
        resume (bool, optional): Enable resuming download (Default: ``False``).
    r   r   abz6{} already exists. Delete the file manually and retry.wbNr   r   rbzBThe hash of {} does not match. Delete the file manually and retry.)r
   r   )r   r   r   r   r   get_filenameospathbasenamejoinexistsgetsizeRuntimeErrorr!   r   r    openvalidate_filer/   write)r	   r1   r2   r3   r4   r   r5   r&   req_infofilepathmode
local_sizefile_objfpointerr,   r-   r-   r.   download_url8   s>   "rJ   rH   c                 C   sR   |dkr	t  }n|dkrt  }nt	 | d}|sn|| q| |kS )a8  Validate a given file object with its hash.

    Args:
        file_obj: File object to read from.
        hash_value (str): Hash for url.
        hash_type (str, optional): Hash type, among "sha256" and "md5" (Default: ``"sha256"``).

    Returns:
        bool: return True if its a valid file, else False.
    r0   md5Ti   )hashlibr0   rK   
ValueErrorr#   r%   	hexdigest)rH   r3   r4   	hash_funcr,   r-   r-   r.   rB   m   s   



rB   	from_pathto_path	overwritec                 C   sn  |du r
t j| }zNt| d>}td|  g }|D ](}t j||j}|	 r>|
| t j|r>td| |s>q||| q|W  d   W S 1 sRw   Y  W n
 tjyb   Y nw zFt| d5}td|  | }|D ]}t j||}t j|rtd| |sqw||| qwW d   |W S 1 sw   Y  |W S  tjy   Y tdw )a  Extract archive.
    Args:
        from_path (str): the path of the archive.
        to_path (str or None, optional): the root path of the extraced files (directory of from_path)
            (Default: ``None``)
        overwrite (bool, optional): overwrite existing files (Default: ``False``)

    Returns:
        list: List of paths to extracted files even if not overwritten.
    NrzOpened tar file %s.z%s already extracted.zOpened zip file %s.z2 > [!] only supports tar.gz, tgz, and zip achives.)r:   r;   dirnametarfilerA   loggingr   r=   nameisfileappendr>   extract	ReadErrorzipfileZipFilenamelist
BadZipFileNotImplementedError)rP   rQ   rR   tarfilesfile_	file_pathzfiler-   r-   r.   extract_archive   sP   
&


rf   dataset_pathdataset_nameoutput_pathc              	   C   sz   t j||}zddl}|j  td| d |jj| |dd W dS  ty<   tdt jt	dd	  Y dS w )
aj  Download dataset from kaggle.
    Args:
        dataset_path (str):
        This the kaggle link to the dataset. for example vctk is 'mfekadu/english-multispeaker-corpus-for-voice-cloning'
        dataset_name (str): Name of the folder the dataset will be saved in.
        output_path (str): Path of the location you want the dataset folder to be saved to.
    r   Nz
Downloading z...T)r;   unzipz][!] in order to download kaggle datasets, you need to have a kaggle api token stored in your ~z.kaggle/kaggle.json)
r:   r;   r=   kaggleapiauthenticateprintdataset_download_filesOSErrorr   )rg   rh   ri   	data_pathrl   r-   r-   r.   download_kaggle_dataset   s   

rs   )Nr   T)NNr0   TF)r0   )NF)rL   rV   r:   rU   r   urllib.requestr\   os.pathr   typingr   r   r   r   torch.utils.model_zoor   strr   boolr/   rJ   rB   rf   rs   r-   r-   r-   r.   <module>   s^   
+
5$2