a
    h4?                     @   s   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 ddl	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G dd deZG dd deZG dd deZdS )    N   )AnvatoIE)InfoExtractor   )ExtractorError
clean_htmldetermine_extget_element_by_classint_or_nonemake_archive_idurl_or_noneurlencode_postdata)traverse_objc                   @   s   e Zd ZdZdZdZddee e	
ejdddd	d
dd  dddZi ZdZdZdZdd Zdd ZdddZdd ZdS )	NFLBaseIEa  (?x)
                    https?://
                        (?P<host>
                            (?:www\.)?
                            (?:
                                (?:
                                    nfl|
                                    buffalobills|
                                    miamidolphins|
                                    patriots|
                                    newyorkjets|
                                    baltimoreravens|
                                    bengals|
                                    clevelandbrowns|
                                    steelers|
                                    houstontexans|
                                    colts|
                                    jaguars|
                                    (?:titansonline|tennesseetitans)|
                                    denverbroncos|
                                    (?:kc)?chiefs|
                                    raiders|
                                    chargers|
                                    dallascowboys|
                                    giants|
                                    philadelphiaeagles|
                                    (?:redskins|washingtonfootball)|
                                    chicagobears|
                                    detroitlions|
                                    packers|
                                    vikings|
                                    atlantafalcons|
                                    panthers|
                                    neworleanssaints|
                                    buccaneers|
                                    azcardinals|
                                    (?:stlouis|the)rams|
                                    49ers|
                                    seahawks
                                )\.com|
                                .+?\.clubs\.nfl\.com
                            )
                        )/
                    zp<script[^>]+id="[^"]*video-config-[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}[^"]*"[^>]*>\s*({.+});?\s*</script>z(anvato:GXvEgwyJeWem8KCYXfeoHWknwP48Mboj:Z 4cFUW6DmwJpzT9L7LrG3qRAcABG5s04gZCZuvCL49d9OwfGsRZdesktopZChromeWindowsz10.0)modelversionZosNameZ	osVersion,:
separatorsotherZ	undefined)Z	clientKeyZclientSecretZdeviceIdZ
deviceInfoZnetworkTypeZpeacockUUIDZB3_Qa8TkWpIB8ESCBT8tY2TukbVKgO5F6BJVc7N1oComdwFzI7H2L9NOWdm11i_BY9fNr   c                 C   s   |  d}t|d| j dd fdd hfdd}|s>|   d|vrRtd	d
d| jdd dtdd| jd|ddt|ddd hfdddd	ddid}t|dddd| _t| jd krtd!d
dd S )"Nzhttps://auth-id.nfl.com/glt_c                 S   s
   |  dS )Nr   )
startswith)k_ r   I/mnt/pikpak/tmp/myenv/lib/python3.9/site-packages/yt_dlp/extractor/nfl.py<lambda>\       z-NFLBaseIE._get_account_info.<locals>.<lambda>c                 S   s   | j S N)valuexr   r   r   r   \   r    Fget_allZucidzRequired cookies for the auth-id.nfl.com domain were not found among passed cookies. If using --cookies, these cookies must be exported along with .nfl.com cookies, or else try using --cookies-from-browser insteadT)expectedz/https://auth-id.nfl.com/accounts.getAccountInfozDownloading account infozprofile,dataenZ	js_latestcookiezhttps://www.nfl.com/Zgig_canary_verc                 S   s   | j dd S )N-r   )r"   	partitionr#   r   r   r   r   p   r    Z15170)defaultjson)	includelangZAPIKeyZsdklogin_tokenZauthModeZpageURLZsdkBuildformatContent-Typez!application/x-www-form-urlencoded)notedataheaderssignatureTimestampUIDZUIDSignature)r6   uidZuidSignature   z5Failed to retrieve account info with provided cookies)	Z_get_cookiesr   _API_KEYZraise_login_requiredr   _download_jsonr   _ACCOUNT_INFOlen)selfcookiesr0   accountr   r   r   _get_account_infoY   sN    


zNFLBaseIE._get_account_infoc              	   C   s   | j r | jtt d kr d S | jd| jdr6dnd d ddidtji | j	| jd	d

 d}|d | _ |d | _|d | jd< d S )N   z'https://api.nfl.com/identity/v3/token%sZrefreshTokenz/refresh r2   application/jsonzDownloading access tokenr   r   )r5   r3   r4   ZaccessTokenZ	expiresIn)_TOKEN_TOKEN_EXPIRYinttimer;   r<   getr-   dumps_CLIENT_DATAencode)r>   tokenr   r   r   _get_auth_token}   s    


zNFLBaseIE._get_auth_tokenFc                 C   s   |    | jd| |d| j dddtjd|ddd d	}| j|d
 |ddd\}}||||tt|gdt	|dddt
htfddt
hfddthfddddthifdfS )Nz"https://api.nfl.com/play/v1/asset/Bearer rD   )AuthorizationAcceptr2   T)initliver   r   )r5   r4   Z	accessUrlmp4Zhls)Zm3u8_id)idformats	subtitlesis_live_old_archive_idsmetadataevent)Z	def_titleZfriendlyNameZdef_descriptionduration
thumbnails.url)titledescriptionr\   r]   )rN   r;   rE   r-   rJ   rL   Z#_extract_m3u8_formats_and_subtitlesr   r   r   stranyr
   r   )r>   mcp_idrX   r4   rV   rW   r   r   r   _extract_video   s2    





zNFLBaseIE._extract_videoc                 C   s
  |  ||}t|dthfpd}|d d }|d }rH| j||dS d|dpZ|d i}|d	 }t|}|d
kr| ||d d|d< n||d	< |ddu rd|d< d }	t|ddtd }
r|
t|
ddg}	|i t|dt	hfdt
hfd||	d |S )NrS   FZplaylistr   ZmcpID)rX   rU   ZentityIdr^   m3u8rT   rV   audioTnoneZvcodecZimageSrcZposterImage)expected_typeZjpg)r^   extr_   r`   )r_   r`   )rX   r]   )Z_parse_jsonr   boolrI   rd   r   Z_extract_m3u8_formatsr   updatera   r   )r>   video_config
display_idrX   itemrc   infoZitem_urlri   r]   Z	image_urlr   r   r   _parse_video_config   s8    
zNFLBaseIE._parse_video_config)F)__name__
__module____qualname___VALID_URL_BASE_VIDEO_CONFIG_REGEXZ_ANVATO_PREFIXra   uuiduuid4base64	b64encoder-   rJ   rL   decoderK   r<   r:   rE   rF   rA   rN   rd   rp   r   r   r   r   r      s2   ,
$
r   c                	   @   sf   e Zd ZdZejd Zddddddd	d
gdddddddddddddddddgZdd ZdS )NFLIEznfl.comz)(?:videos?|listen|audio)/(?P<id>[^/#?&]+)zVhttps://www.nfl.com/videos/baker-mayfield-s-game-changing-plays-from-3-td-game-week-14Z899441rT   z;Baker Mayfield's game-changing plays from 3-TD game Week 14z$md5:85e05a3cc163f8c344340f220521136dzre:https?://.+\.jpg   zanvato 899441)rU   ri   r_   r`   	thumbnailr\   rY   )r^   	info_dictzchttps://www.chiefs.com/listen/patrick-mahomes-travis-kelce-react-to-win-over-dolphins-the-breakdownZ 92a517f05bd3eb50fe50244bc621aec8z$8b7c3625-a461-4751-8db4-85f536f2bbd0Zmp3zHPatrick Mahomes, Travis Kelce React to Win Over Dolphins | The Breakdownz$md5:12ada8ee70e6762658c30e223e095075zshttps://static.clubs.nfl.com/image/private/t_editorial_landscape_12_desktop/v1571153441/chiefs/rfljejccnyhhkpkfq855)rU   ri   r_   r`   r}   )r^   md5r~   zMhttps://www.buffalobills.com/video/buffalo-bills-military-recognition-week-14T)r^   Zonly_matchingzjhttps://www.raiders.com/audio/instant-reactions-raiders-week-14-loss-to-indianapolis-colts-espn-jason-fitzc                 C   s.   |  |}| ||}| | | j|d|S )Nzvideo config)	_match_id_download_webpagerp   Z_search_regexru   )r>   r^   rm   webpager   r   r   _real_extract   s    
zNFLIE._real_extractN)	rq   rr   rs   IE_NAMEr   rt   
_VALID_URL_TESTSr   r   r   r   r   r{      s:   
r{   c                   @   s4   e Zd ZdZejd ZddddddZd	d
 ZdS )NFLArticleIEznfl.com:articleznews/(?P<id>[^/#?&]+)zrhttps://www.buffalobills.com/news/the-only-thing-we-ve-earned-is-the-noise-bills-coaches-discuss-handling-rising-ezPthe-only-thing-we-ve-earned-is-the-noise-bills-coaches-discuss-handling-rising-ez_'The only thing we've earned is the noise' | Bills coaches discuss handling rising expectations)rU   r_      )r^   r~   playlist_countc                    sT    | |  fdd}ttdpBddg}|  |S )Nc                  3   s&   t jD ]} |  V  qd S r!   )refindallru   rp   )rl   rm   r>   r   r   r   entries   s    z+NFLArticleIE._real_extract.<locals>.entriesznfl-c-article__titlezog:titleztwitter:title)r   r   r   r	   Z_html_search_metaplaylist_result)r>   r^   r   r_   r   r   r   r      s    
zNFLArticleIE._real_extractN)	rq   rr   rs   r   r   rt   r   Z_TESTr   r   r   r   r   r      s   
	r   c                   @   s   e Zd ZdZdZddddddd	d
ddgg dddddiddddddiddddddidddddddddd d!dgd"gdddd#d$d%giid&d'gZd(d)d*d+d,Zd-d. Zd/d0 Zd1S )2NFLPlusReplayIEznfl.com:plus:replayzHhttps?://(?:www\.)?nfl\.com/plus/games/(?P<slug>[\w-]+)(?:/(?P<id>\d+))?zDhttps://www.nfl.com/plus/games/giants-at-vikings-2022-post-1/1572108Z1572108rT   z$New York Giants at Minnesota VikingszSNew York Giants play the Minnesota Vikings at U.S. Bank Stadium on January 15, 2023NFLZ20230116iH%ci  zGame Highlights)zMinnesota VikingszNew York Giantsz%Minnesota Vikings vs. New York Giantsre:^https?://.*\.jpgrU   ri   r_   r`   ZuploaderZupload_date	timestampr\   
categoriestagsr}   skip_downloadre   )r^   r~   paramsSubscription requiredz<https://www.nfl.com/plus/games/giants-at-vikings-2022-post-1r   rU   zgiants-at-vikings-2022-post-1)r3   r^   r   r~   z<https://www.nfl.com/plus/games/giants-at-patriots-2011-pre-4r   zgiants-at-patriots-2011-pre-4Z950701zGiants @ Patriotsz(Giants at Patriots on September 01, 2011Z20210724i2\`i  zplay-by-playZnflplusreplaytypecondensed_game)r   Zextractor_argsr3   r^   r~   r   z	Full GamezFull Game - SpanishzCondensed GamezAll-22)Z	full_gameZfull_game_spanishr   Zall_22c                 C   s   |    d S r!   rA   r>   r   r   r   _real_initializeH  s    z NFLPlusReplayIE._real_initializec                    s    |dd\}}ddgdv r:tj tjd f|sȈ  ddj i}j	d| |dd	d
i|dd }j	d|dd|i|d t
dkrt dfdddfdd}|rֈ|S  fdd}| |S )NslugrU   r   allrP   rO   z6https://api.nfl.com/football/v2/games/externalId/slug/zDownloading game IDZwithExternalIdstrue)queryr5   z-https://api.nfl.com/content/v1/videos/replayszDownloading replays JSONZgameIdr   itemsc                    s   |d  d kS )NsubTyper   r   r   vrequested_typesr   r   r   ]  r    z/NFLPlusReplayIE._real_extract.<locals>.<lambda>mcpPlaybackIdFr%   c                  3   s0   t  dfddfD ]} | d V  qd S )Nr   c                    s   |d o|d  v S )Nr   r   r   r   r   r   r   r   d  r    z@NFLPlusReplayIE._real_extract.<locals>.entries.<locals>.<lambda>r   )r   rd   )ZreplayZreplaysr   r>   r   r   r   b  s    z.NFLPlusReplayIE._real_extract.<locals>.entries)Z_match_valid_urlgroupZ_configuration_arglist_REPLAY_TYPESkeysr   rN   rE   r;   r=   rd   r   )r>   r^   r   video_idr5   Zgame_idr   r   r   r   r   K  s8    


zNFLPlusReplayIE._real_extractN)	rq   rr   rs   r   r   r   r   r   r   r   r   r   r   r   
  sn   5r   c                   @   sT   e Zd ZdZdZddddddd	d
dddgdgddddidgZdd Zdd ZdS )NFLPlusEpisodeIEznfl.com:plus:episodez8https?://(?:www\.)?nfl\.com/plus/episodes/(?P<id>[\w-]+)r   zLhttps://www.nfl.com/plus/episodes/kurt-s-qb-insider-conference-championshipsZ1576832rT   zConference Championshipsz$md5:944f7fab56f7a37430bf8473f5473857r   Z20230127i((ci  ZAnalysisz6Cincinnati Bengals at Kansas City Chiefs (2022-POST-3)r   r   r   re   r   c                 C   s   |    d S r!   r   r   r   r   r   r     s    z!NFLPlusEpisodeIE._real_initializec                 C   sB   |  |}|   | jd| |dd| j idd }| |S )Nz/https://api.nfl.com/content/v1/videos/episodes/rP   rO   )r5   r   )r   rN   r;   rE   rd   )r>   r^   r   r   r   r   r   r     s    

zNFLPlusEpisodeIE._real_extractN)rq   rr   rs   r   r   r   r   r   r   r   r   r   r   k  s(   r   )rx   r-   r   rH   rv   Zanvator   commonr   utilsr   r   r   r	   r
   r   r   r   Zutils.traversalr   r   r{   r   r   r   r   r   r   r   <module>   s   (
 3'a