o
    eZ                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZ zd dlmZ d dlmZ W n ey=   ed	w d
ededefddZdeeef defddZdS )    N)md5)Path)copyfileobj)Union)VIDEO_ASSETSVideoAssets)get)tqdmz|
Please install requests and tqdm to download assets 
or install supervision with assets 
pip install supervision[assets] 

filenameoriginal_md5_hashreturnc                 C   s\   t j| sdS t| d}| }t| }W d   ||kS 1 s%w   Y  ||kS )a/  
    Check if the MD5 hash of a file matches the original hash.

    Parameters:
        filename (str): The path to the file to be checked as a string.
        original_md5_hash (str): The original MD5 hash to compare against.

    Returns:
        bool: True if the hashes match, False otherwise.
    FrbN)ospathexistsopenreadr   	hexdigest)r
   r   filefile_contentscomputed_md5_hash r   L/var/www/myenv/lib/python3.10/site-packages/supervision/assets/downloader.pyis_md5_hash_matching   s   
r   
asset_namec              	   C   sv  t | tr| jn| }t| s|tv rtd| d tt| d ddd}|  t	|j
dd}t|  }|jjddd tj|jd|d	d
d+}|d}t|| W d   n1 sew   Y  W d   |S W d   |S 1 s}w   Y  |S t| rt|t| d std t| t|S t| d |S ddd tD }td| d)a  
    Download a specified asset if it doesn't already exist or is corrupted.

    Parameters:
        asset_name (Union[VideoAssets, str]): The name or type of the asset to be
            downloaded.

    Returns:
        str: The filename of the downloaded asset.

    Example:
        ```python
        >>> from supervision.assets import download_assets, VideoAssets

        >>> download_assets(VideoAssets.VEHICLES)
        "vehicles.mp4"
        ```
    zDownloading z	 assets 
r   T)streamallow_redirectszContent-Length)parentsexist_okr    z#a351fb)totaldesccolourwbN   z#File corrupted. Re-downloading... 
z asset download complete. 
z, c                 s   s    | ]}|j V  qd S )N)value).0assetr   r   r   	<genexpr>Y   s    z"download_assets.<locals>.<genexpr>z2Invalid asset. It should be one of the following: .)
isinstancer   r%   r   r   r   printr   raise_for_statusintheaders
expanduserresolveparentmkdirr	   wrapattrrawr   r   r   r   removedownload_assetsjoin
ValueError)r   r
   response	file_sizefolder_pathraw_respr   valid_assetsr   r   r   r6   +   sB   



r6   )r   hashlibr   pathlibr   shutilr   typingr   supervision.assets.listr   r   requestsr   	tqdm.autor	   ImportErrorr8   strboolr   r6   r   r   r   r   <module>   s     
