U
    ~g                     @   sX   d dl Z d dlZd dlZd dlmZ d dlZejdejd ee	Z
G dd dZdS )    N)YouTubez4%(asctime)s - %(name)s - %(levelname)s - %(message)s)formatlevelc                   @   s<   e Zd ZdddZdd ZdddZdd	d
ZdddZdS )VideoDownloader	downloadsc                 C   s   || _ tj|dd d S )NT)exist_ok)download_pathosmakedirs)selfr    r   */var/www/html/dlvid/improved_downloader.py__init__   s    zVideoDownloader.__init__c                 C   sP   d|ksd|krdS d|ks,d|ks,d|kr0dS d|kr<d	S d
|krHdS dS dS )z#Determine the platform from the URLzyoutube.comzyoutu.beyoutubezfacebook.comzfb.comzfb.watchZfacebookzinstagram.comZ	instagramz
tiktok.comZtiktokZgenericNr   )r   urlr   r   r   get_platform   s    zVideoDownloader.get_platformFc           
   
   C   s.  zt |}|r|jjdd }|s@td|  dddW S |j| jd}tj	
|\}}|d }t|| d||jd	d
W S |j }|std|  dddW S |j| jd}d||jdd
W S W nX tk
r( }	 z8tdt|	  td|  | || W Y S d}	~	X Y nX dS )zEDownload video from YouTube using pytube with improved error handlingT)Z
only_audiozNo audio stream found for FzNo audio stream foundsuccesserror)Zoutput_pathz.mp3mp3r   	file_pathtitler   zNo video stream found for zNo video stream foundmp4z,Error downloading from YouTube with pytube: zFalling back to yt-dlp for N)r   streamsfilterfirstloggerr   downloadr   r	   pathsplitextrenamer   Zget_highest_resolution	Exceptionstrinfodownload_with_ytdlp)
r   r   
audio_onlyZytstreamout_filebase_Zmp3_fileer   r   r   download_youtube!   s,    
z VideoDownloader.download_youtubec              
      sF  z|  |}| dtd   tj| j }|d ddddddddd	}|rv|d	d
dddgd d}n|ddi d}t	|Z}t
d| d |j|dd}|st
d|  dddW  5 Q R  W S d|kr*|d st
d|  dddW  5 Q R  W S |d d }|s<|dd}|dd}	| d| }
tj|
s҇ fd d!t| jD }|rtj| j|d }
t
d"|
  n(t
d#|
  dd$dW  5 Q R  W S d|
|	|d%W  5 Q R  W S Q R X W nJ tk
r@ } z*t
d&t|  dt|d W Y S d'}~X Y nX d'S )(zJDownload video using yt-dlp for all platforms with improved error handlingr*      z.%(ext)sFT
      )	ZouttmplquietZno_warningsverboseZignoreerrorsZnocheckcertificateZ
geo_bypassretriesZsocket_timeoutzbestaudio/bestZFFmpegExtractAudior   Z192)keyZpreferredcodecZpreferredquality)r   Zpostprocessorsr   Zbestr   zAttempting to download z with yt-dlp)r   zNo information extracted for z#Failed to extract video informationr   entriesz(Empty playlist or all videos failed for z#Empty playlist or all videos failedr   extr   Video.c                    s   g | ]}|  r|qS r   )
startswith).0ffilenamer   r   
<listcomp>}   s     
 z7VideoDownloader.download_with_ytdlp.<locals>.<listcomp>zFound alternative file: zDownloaded file not found at zDownloaded file not foundr   zError downloading with yt-dlp: N)r   r	   urandomhexr   joinr   updateyt_dlpZ	YoutubeDLr   r$   Zextract_infor   getexistslistdirr"   r#   )r   r   r&   platformZoutput_templateZydl_optsZ	final_extZydlr$   r   r   Zpossible_filesr+   r   r;   r   r%   A   st    
 

z#VideoDownloader.download_with_ytdlpc                 C   sp   t d| d|  | |}t d|  | ||}|d sl|dkrl|slt d|  | ||}|S )zWMain download method that routes to appropriate downloader with improved error handlingzStarting download for z, audio_only=zDetected platform: r   r   z!yt-dlp failed, trying pytube for )r   r$   r   r%   r,   )r   r   r&   rF   resultr   r   r   r      s    
zVideoDownloader.downloadN)r   )F)F)F)__name__
__module____qualname__r   r   r,   r%   r   r   r   r   r   r      s
   

 
Nr   )r	   rerB   Zpytuber   loggingbasicConfigINFO	getLoggerrH   r   r   r   r   r   r   <module>   s   
