a
    hI                     @   sd   d dl Z d dl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 G dd deZdS )    N   )InfoExtractor   )ExtractorErrorfloat_or_noneint_or_noneparse_qstraverse_objtruncate_stringtry_getunescapeHTMLupdate_url_queryurl_or_noneurlencode_postdatac                   @   s,  e Zd ZdZdZdddddddd	d
dddeeedddddiddddddddddddddeeedddd d!d"dd#d$d%dd&d'd(d)d*eeed+d,dddidd-d.d/d0d1d2d3d4eeedd5d6d7d8d9d:d;dd<d=d=d>dd?d@dAeeeddBdCd dDdEdFddGdHdHdIdJdKdLdMddeeedNd dOdPdQddRdSdSdTdUdAdVdWdeeedXdYdZd[d\dd]d^d^d_d`dadbdcdeeedddeidfddiddgdhddidjdjdkdldmdndodeeedddeidfdddpddqdrddsdtdtdudveeed+dwdxdydzd{d|d}dd~ddd~ddd~ddd~ddd~ddd~ddd~gZdd Zdd Zdd Z	dd Z
dS )RedditIEZredditzbhttps?://(?:\w+\.)?reddit(?:media)?\.com/(?P<slug>(?:(?:r|user)/[^/]+/)?comments/(?P<id>[^/?#&]+))zHhttps://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/Zzv89llsvexdzmp4Z6rrwyjzThat small heart attack.zre:^https?://.*\.(?:jpg|png)zcount:4iЅYZ20170805ZAntw87   r   Zvideos)idext
display_idtitle	alt_title	thumbnail
thumbnails	timestampupload_dateuploaderduration
like_countdislike_countcomment_count	age_limit
channel_idskip_downloadT)url	info_dictparamsz\https://www.reddit.com/r/aww/comments/90bu6w/heat_index_was_110_degrees_so_we_offered_him_a/Z 8b5902cfda3006bf90faea7adf765a49Zgyh95hiqc0b11Z90bu6wzHHeat index was 110 degrees so we offered him a cold drink. He went fo...z_Heat index was 110 degrees so we offered him a cold drink. He went for a full body soak insteadzcount:7i>Q[Z20180720ZFootLoosePickleJuice   Zaww)r$   md5r%   zehttps://www.reddit.com/user/creepyt0es/comments/nip71r/i_plan_to_make_more_stickers_and_prints_check/Zzasobba6wp071Znip71rzHI plan to make more stickers and prints! Check them out on my Etsy! O...zpI plan to make more stickers and prints! Check them out on my Etsy! Or get them through my Patreon. Links below.zcount:5i%Q`Z20210522Z
creepyt0es      Zu_creepyt0eszchttps://www.reddit.com/r/KamenRider/comments/wzqkxp/finale_kamen_rider_revice_episode_50_family_to/r   ZwzqkxpzH[Finale] Kamen Rider Revice Episode 50 "Family to the End, Until the ...zb[Finale] Kamen Rider Revice Episode 50 "Family to the End, Until the Day We Meet Again" Discussionz$md5:5b7deb328062b164b15704c5fd67c335ZTheTwelveYearOldZ
KamenRiderg  AZ20220828)r   r   r   descriptionr   r"   r    r   r   r!   r   r   )r$   Zplaylist_countr%   zAhttps://www.reddit.com/r/dumbfuckers_club/comments/zjjw82/cringe/Z 746180895c7b75a9d6b05341f507699aZa1oneun6pa5a1Zzjjw82ZCringezOtaku-senpai69420Z20221212ic   Zdumbfuckers_club)r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   z'https://www.reddit.com/comments/124pp33Z 15eec9d828adcef4468b741a7e45a395Zantsenjc2jqa1Z124pp33z"Harmless prank of some old friendsZDudezilaZContagiousLaughter   Z20230328i"d)r   r   r   r   r   r   r"   r   r   r   r   r!   r    r   r   z@https://old.reddit.com/r/GenZedong/comments/12fujy3/based_hasan/Z 3156ea69e3c1f1b6259683c5abd36e71Z8bwtclfggpsa1Z12fujy3zBased Hasan?ZKingNigelXLIIZ	GenZedongZ20230408i´1d)r   r   r   r   r   r   r"   r   r   r   r!   r    r   r   z=Requires account that has opted-in to the GenZedong subreddit)r$   r(   r%   skipz]https://www.reddit.com/r/Unexpected/comments/1cl9h0u/the_insurance_claim_will_be_interesting/Za2mdj5d57qyc1Z1cl9h0uz'The insurance claim will be interestingZdarrenpauliZ
Unexpected5   Z20240506inO8fenz
mincount:1)r   r   r   r   r   r   r"   r   r   r   r!   r    r   r   	subtitleszbhttps://www.reddit.com/r/soccer/comments/1cxwzso/tottenham_1_0_newcastle_united_james_maddison_31/Zxbmj4t3igy1d1Z1cxwzsoz7Tottenham [1] - 0 Newcastle United - James Maddison 31'Z
WoodstoviaZsoccer   Z20240522i&Mf)r#   ZwritesubtitleszBhttps://old.reddit.com/r/ketamine/comments/degtjo/when_the_k_hits/Zgqsbxts133r31ZdegtjozWhen the K hitsz	[deleted]Zketamine"   zre:https?://.+/.+\.(?:jpg|png)g  @fAZ20191007)r   r   r   r   r   r   r"   r    r   r   r!   r   r   r   r   )r$   r%   z/https://www.reddit.com/r/videos/comments/6rrwyj)r$   Zonly_matchingzAhttps://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/zAhttps://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/z]https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/z^https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/z`https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/zlhttps://www.redditmedia.com/r/serbia/comments/pu9wbx/ako_vu%C4%8Di%C4%87_izgubi_izbore_ja_%C4%87u_da_crknem/c              	   C   s   |  dd dd }|r"tddd| j d| d td||d	d
ddd}dt|d}|rrtd| ddnt|d	ddthfstdd S )Nz6https://www.reddit.com/api/requires_captcha/login.jsonzChecking login requirementrequiredz(Reddit is requiring captcha before loginTexpectedz!https://www.reddit.com/api/login/z
login-mainjson)opuserpasswdZapi_typez
Logging inzLogin request failed)dataZnoteZerrnotez; )r7   errors.r   z!Unable to login, Reddit API says r;   cookiez'Unable to login, no cookie was returned)_download_jsonr   r   joinr	   str)selfusernamepasswordZcaptchaloginr<    rE   L/mnt/pikpak/tmp/myenv/lib/python3.9/site-packages/yt_dlp/extractor/reddit.py_perform_login  s,    zRedditIE._perform_loginc              	   C   s`   |  ddd t| dddtjjhtjhthfp6i }d|d< |  ddtj	t
| d S )	N
reddit.comZover181https://www.reddit.com/_optionsvalueTZpref_gated_sr_optin)Z_set_cookier	   _get_cookiesurllibparseunquoter7   loadsdictquotedumps)rA   optionsrE   rE   rF   _real_initialize  s    
zRedditIE._real_initializec                 C   s.   d| d}| j ||ddr*dd|igiS d S )Nhttps://v.redd.it/z/wh_ben_en.vttr1   )itemr0   r$   )Z_is_valid_url)rA   video_idZcaption_urlrE   rE   rF   _get_subtitles(  s    zRedditIE._get_subtitlesc                    s  |  |dd\}}z| jd| d|dd}W nZ ty } zBt|jtjrv| d	drltdd	d
| 
d  W Y d }~n
d }~0 0 t|ddkr|	d}|dkr| 
d n"|dkr| 
d ntd| |d d d d d }|d }g fdd}t|dd pg D ]N}	t|	ts4q ||		d |		d}
t|
tr |
D ]}|| q\q ddd	|	dd t|d!td"d#hfd!thfd$thtfd%thfd&thfd'thfd(thfd)thfd*thfd+	}tj|}d,|jv rd-| d-|jv rg }t|d.td/D ]}|	dr&|	d0d1krJq&g }|	d2r||| jt|d2 |d3d4d5d6 |	d7r|| jt|d7 |d8d5d9 |r&||d ||d:| q&|r| j||fi |S | jd;d	|d< i |d|iS t|d=d5d>  rX fd?d@dAD }|}| j dB dC dD|dE}|d pZdF| dG}|dH ppdF| dI}tt!|dJdJddKd hfi}t"||}t dC t 	dLt 	dMt 	dNdOdPd3dQdRdS	g}| j#||d3d4d5d6\}}|| | j$||d8d5d9\}}|| | j%||dT i |||||pD| &|t 	dUdVS |jdWkr| jdXd	|d< i ||j'd-dH |dYS i ||dZ|d[S )\Nslugr   rJ   z/.jsoni  )Zexpected_statusZreddit_sessionz2Your IP address is unable to access the Reddit APITr5   z"Account authentication is requirederrorreasonZquarantinedz?Quarantined subreddit; an account that has opted in is requiredprivatez@Private subreddit; an account that has been approved is requiredz(HTTP Error 403 Forbidden; reason given: r   r;   childrenr$   c                    sX   t | tsd S t| d}|s$d S  t|t| dt| dddid d S )Nr$   widthheightAcceptz*/*)r$   r`   ra   Zhttp_headers)
isinstancerR   r   getappendr   r   )srcZthumbnail_url)r   rE   rF   add_thumbnailI  s    
z-RedditIE._real_extract.<locals>.add_thumbnailc                 S   s   | d d S )NpreviewZimagesrE   xrE   rE   rF   <lambda>V      z(RedditIE._real_extract.<locals>.<lambda>sourceresolutionsr*   )TFZover_18)r   r!   r   H   )leftZselftextZcreated_utcauthorZ	subredditZupsZdownsZnum_comments)	r   r   r+   r   r   r"   r   r   r    rH   /)Zmedia_metadata.)expected_typeeZRedditVideoZhlsUrlr   ZhlsF)Zm3u8_idfatalZdashUrlZdash)Zmpd_idru   )r   r   formatszNo media found)r6   rY   ))N)Zcrosspost_parent_list.)Zsecure_mediamediareddit_video)get_allc                    s   g | ] t  fd dqS )c                    s   t |   S )N)r   ri   yrE   rF   rk     rl   z3RedditIE._real_extract.<locals>.<listcomp>.<lambda>)r   ).0)rx   rz   rF   
<listcomp>  s   z*RedditIE._real_extract.<locals>.<listcomp>)Zdash_urlZhls_urlz%https?://v\.redd\.it/(?P<id>[^/?#&]+)Zfallback_urlrY   )defaultrW   z/DASHPlaylist.mpdr   z/HLSPlaylist.m3u8fc                 S   s   | rd | dgS dS )N,ZsubsAllz
hd,subsAll)r?   ri   rE   rE   rF   rk     rl   ra   r`   Zbitrate_kbpsnoneZh264fallbackzDASH video, mp4_dash)	r$   ra   r`   ZtbrZacodecZvcodecr   Z	format_idZformat_note)targetr   )r   r   rv   r1   r   z	v.redd.itzThis video is processing)r   r   Zurl_transparent)r   _typer$   )(Z_match_valid_urlgroupr>   r   rc   causer7   JSONDecodeErrorrM   rd   Zraise_login_requiredr	   r   rR   listr
   r@   filterr   r   rN   rO   urlparsenetlocpathextendZ_extract_m3u8_formatsr   Z_extract_mpd_formatsre   Zplaylist_resultZraise_no_formatsZ_search_regexr   r   Z#_extract_m3u8_formats_and_subtitlesZ"_extract_mpd_formats_and_subtitlesZ_merge_subtitlesZextract_subtitlessplit)rA   r$   r[   rY   r;   rt   r]   Z	video_urlrg   imagern   
resolutioninfo
parsed_urlentriesrw   rv   Zplaylist_urlsr   Zdash_playlist_urlZhls_playlist_urlqsZhls_fmtsr1   Z	dash_fmtsZ	dash_subsrE   )rx   r   rF   _real_extract.  s
   




 








	zRedditIE._real_extractN)__name__
__module____qualname__Z_NETRC_MACHINEZ
_VALID_URLintZ_TESTSrG   rV   rZ   r   rE   rE   rE   rF   r      s    v	r   )r7   urllib.parserN   commonr   utilsr   r   r   r   r	   r
   r   r   r   r   r   r   rE   rE   rE   rF   <module>   s   4