o
    ij5                     @   s   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
mZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ G d	d
 d
eZG dd deZdS )    N   )InfoExtractor   )HEADRequest)	HTTPError)ExtractorErrorOnDemandPagedList
clean_htmldetermine_extformat_fieldget_element_by_classget_elements_html_by_classint_or_none
orderedSetparse_countparse_durationparse_iso8601url_or_noneurlencode_postdataurljoin)traverse_objc                   @   s,  e Zd ZdZde dgZdddddd	d
dddddddeddddddddddd
ddddddd ed!d"ddd#d$i d%d&d'dd(d)d*d+d,d-d.d
d/dd0d1d2dd3dd4dd5dd6dd7ed8d9d:d;gd<d=d>d?id@dAdBdCdDdBdCdEdBdCgZdFZdGZdHZ	dIdJ Z
dOdKdLZdMdN Zd?S )P
BitChuteIEzYhttps?://(?:(?:www|old)\.)?bitchute\.com/(?:video|embed|torrent/[^/?#]+)/(?P<id>[^/?#&]+)z,<(?:script|iframe)[^>]+\bsrc=(["\'])(?P<url>)z+https://www.bitchute.com/video/UGlrF9o9b-Q/ 7e427d7ed7af5a75b5855705ec750e2bUGlrF9o9b-Qmp4&This is the first video on #BitChute !$md5:a0337e7b1fe39e32336974af8173a034re:https?://.+/.+\.jpg$BitChute20170103.https://www.bitchute.com/profile/I5NgtHZn9vPj/*https://www.bitchute.com/channel/bitchute/I5NgtHZn9vPj1VBwRfyNcKdXg      0@FkX)idexttitledescription	thumbnailuploaderupload_dateuploader_urlchannelchannel_urluploader_id
channel_id
view_countduration	timestamp)urlmd5	info_dictz+https://www.bitchute.com/video/Yti_j9A-UZ4/ f10e6a8e787766235946d0868703f1d0zYti_j9A-UZ4zIsrael at War | Full Measurez$md5:e60198b89971966d6030d22b3268f08fsharylattkisson20231106z.https://www.bitchute.com/profile/9K0kUWA9zmd9/z"Full Measure with Sharyl Attkissonz1https://www.bitchute.com/channel/sharylattkisson/9K0kUWA9zmd9NpdxoCRv3ZLbg     P@ij3Iez,https://www.bitchute.com/video/2s6B3nZjAk7R/ 05c12397d5354bf24494885b08d24ed1r&   2s6B3nZjAk7Rr'   filesizeiCr(   zMSTYXHEXENHAMMER666 - Election Fraud, Clinton 2020, EU Armies, and Gun Controlr)   z$md5:2029c7c212ccd4b040f52bb2d036ef4er*   r+   r,   20181113r-   r.   r/   r0   r1   r2   r3   g     @tagsbitchuter4   io
[check_formatsN)r5   r6   r7   paramsz+https://www.bitchute.com/embed/lbb5G1hjPhw/Tr5   only_matchingzDhttps://www.bitchute.com/torrent/Zee5BE49045h/szoMrox2JEI.webtorrentz+https://old.bitchute.com/video/UGlrF9o9b-Q/Fz$https://www.bitchute.com/profile/%s/z$https://www.bitchute.com/channel/%s/c                    s   t  fdddD }|D ];}z| jt||d| d}W n ty; } z| | d|j  W Y d }~qd }~ww |t|jdd  S d S )	Nc                 3   s$    | ]}t d d|  V  qdS )z((^https?://)(seed\d+)(?=\.bitchute\.com)z\g<1>N)resub).0host	video_url M/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/bitchute.py	<genexpr>o   s
    
z+BitChuteIE._check_format.<locals>.<genexpr>)z\g<2>seed122seed125seed126seed128seed132seed150seed151seed152seed153seed167seed171seed177seed305seed307	seedp29xbzzb10-7gsop1v78z	Checking )video_idnotez: URL is invalid, skipping: zContent-Length)r5   r?   )	r   _request_webpager   r   	to_screencauser   headersget)selfrL   r_   urlsr5   responseerM   rK   rN   _check_formatn   s"   

zBitChuteIE._check_formatc                 C   s   | dd }z| jd| |d| dd| dt| dddd	W S  tyq } z?t|jtr]|jj	d
kr]d
t|jj  tjhddd dthf}|r]d|v r]| | |r` | |j W Y d }~d S d }~ww )N/r   z"https://api.bitchute.com/api/beta/zDownloading z	 API JSONzUnable to download zapplication/json)AcceptContent-Typedatard   i  z. errorsc                 S   s   |d dkS )NcontextreasonrM   )_vrM   rM   rN   <lambda>   s    z&BitChuteIE._call_api.<locals>.<lambda>messagelocation)
rpartition_download_jsonjsondumpsencoder   
isinstancerc   r   statusjoinr   rh   readdecodeloadsstrraise_geo_restrictedreport_warningmsg)rf   endpointro   
display_idfatalr`   ri   rp   rM   rM   rN   	_call_api   s,   

zBitChuteIE._call_apic           
      C   s  |  |}d|i}| d||d }g }t|dkr(|| j||dddd n| d	d
ur=| || }r<|| n|d|i |sN| jdd|d | jd||d
d}d }t	|ddt
hf }	ro| jdd|	i|d
d}i t	|dt
hfdt
hfdthfddt
hfddt
hfddtdhfdt
hfdt| jdhtfdthfdthfddt
htttfdthfdd d! hfd"t	|dt
hfdt
hfd#t| jdhtfd$t
hfdt
hfdt| jdhtfd%||d&S )'Nr_   zvideo/media	media_urlm3u8r   hlsT)m3u8_idliverC   Fr5   ziVideo is unavailable. Please make sure this video is playable in the browser before reporting this issue.)expectedr_   video)r   r.   r1   
video_namer)   thumbnail_urlchannel_namer/   zhttps://www.bitchute.com/
profile_id)templatedate_publishedr3   hashtags.r2   state_idc                 S   s   | dkS )Nr   rM   )xrM   rM   rN   ru      s    z*BitChuteIE._real_extract.<locals>.<lambda>)r(   r)   r*   r.   r1   r/   r0   r-   r4   r3   rA   r2   is_liveurl_slugprofile_name)r.   r1   r/   r+   r0   r-   )r&   formats)	_match_idr   r
   extend_extract_m3u8_formats	get_paramrj   appendraise_no_formatsr   r   r   r   r   _UPLOADER_URL_TMPLfilterr   r   allr   _CHANNEL_URL_TMPL)
rf   r5   r_   ro   r   r   fmtr   r.   r1   rM   rM   rN   _real_extract   sf   



zBitChuteIE._real_extract)T)__name__
__module____qualname__
_VALID_URL_EMBED_REGEXint_TESTS_GEO_BYPASSr   r   rj   r   r   rM   rM   rM   rN   r      s    	
K
r   c                   @   s   e Zd ZdZdddddddd	d
ddddddddedddddgddddddddddddd dd!gZd"Zd#Zd$d%d&d'd(d)d*d'd+Ze	d,d- Z
d.d/ Zd0d1 Zd2S )3BitChuteChannelIEzThttps?://(?:(?:www|old)\.)?bitchute\.com/(?P<type>channel|playlist)/(?P<id>[^/?#&]+)r"   rB   r   z$md5:2134c37d64fc3a4846787c402956adac)r&   r(   r)   r   r   r   r   r   r   r    r!      r#   r$   r%   )r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r3   r2   r0   r1   r4   )r6   r7   Tz-1)skip_downloadplaylist_items)r5   r7   playlistrD   z/https://www.bitchute.com/playlist/wV9Imujxasw9/   wV9Imujxasw9z+Bruce MacDonald and "The Light of Darkness"z$md5:747724ef404eebdfc04277714f81863ez404 Not Found)r5   playlist_mincountr7   skipz/https://old.bitchute.com/playlist/wV9Imujxasw9/rE   @zyG6tQcGPE5swyAEFLqKUwMuMMuF6IO2DZ6ZDQjGfsL0e4dcTLwqkTTul05Jdve7   zchannel-videos-containerzchannel-videos-titlezchannel-videos-text)	containerr(   r)   zplaylist-videor(   r)   )r.   r   c                 C   s   d| d|  dS )Nzhttps://old.bitchute.com/rk   rM   )playlist_idplaylist_typerM   rM   rN   	_make_url  s   zBitChuteChannelIE._make_urlc           	      c   s    |  ||}| j| d|d| t| jd|| j dd|dd| j dd	}|d
s1d S | j| }t|d |dD ]9}| jd|dd d}|sNq@| j	d| t
|dtt|d |tt|d |ttd|tttd|dV  q@d S )Nzextend/zDownloading page  )csrfmiddlewaretokennameoffsetz0application/x-www-form-urlencoded; charset=UTF-8XMLHttpRequestz
csrftoken=)rm   RefererzX-Requested-WithCookiern   successr   htmlz&<a\s[^>]*\bhref=["\']/video/([^"\'/]+)zvideo iddefaultzhttps://www.bitchute.com/video/Tr(   r)   zvideo-durationzvideo-views)url_transparentr(   r)   r3   r2   )r   ry   r   _TOKEN	PAGE_SIZEre   HTML_CLASS_NAMESr   _search_regex
url_resultr   r	   r   r   r   )	rf   r   r   page_numplaylist_urlro   classes
video_htmlr_   rM   rM   rN   _fetch_page  s@   



zBitChuteChannelIE._fetch_pagec                 C   s~   |  |dd\}}| | |||}t| j||}| jt|| j	|| j
|d d| jd|d dt| jd|dd ddS )Ntyper&   r   )r)   zog:descriptionztwitter:descriptionz<span>(\d+)\s+videos?</span>zplaylist count)r(   r)   playlist_count)_match_valid_urlgroup_download_webpager   	functoolspartialr   playlist_resultr   r   _html_extract_title_html_search_metar   _html_search_regex)rf   r5   r   r   webpage	page_funcrM   rM   rN   r   0  s   zBitChuteChannelIE._real_extractN)r   r   r   r   r   r   r   r   r   staticmethodr   r   r   rM   rM   rM   rN   r      sr    "
/
r   )r   rz   rG   commonr   
networkingr   networking.exceptionsr   utilsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   utils.traversalr   r   r   rM   rM   rM   rN   <module>   s    D 3