U
    g<                     @   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 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traverse_objurlencode_postdatac                   @   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g g dZi ZdZdZdZdd Z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other)Z	clientKeyZclientSecretZdeviceIdZ
deviceInfoZnetworkTypeZnflClaimGroupsToAddZnflClaimGroupsToRemoveNr   c                 C   s   | j s2| j||ddpd}| jd|dddp.d| _ | d}t|d| j  d	d
 fdd
 hfdd}|sp|   d|krtddd| jd|dtdd| j 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 )&NF)fatal z*window\.gigyaApiKey\s*=\s*["\'](\w+)["\'];zAPI keyZB3_Qa8TkWpIB8ESCBT8tY2TukbVKgO5F6BJVc7N1oComdwFzI7H2L9NOWdm11i_BY9fzhttps://auth-id.nfl.com/glt_c                 S   s
   |  dS )Nr   )
startswith)k_ r   8/tmp/pip-unpacked-wheel-q5ljy6pj/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   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gig_canary_verc                 S   s   | j dd S )N-r   )r    	partitionr!   r   r   r   r   t   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)r4   uidZuidSignature   z5Failed to retrieve account info with provided cookies)_API_KEY_download_webpage_search_regexZ_get_cookiesr
   Zraise_login_requiredr   _download_jsonr   _ACCOUNT_INFOlen)selfurlslugwebpagecookiesr.   accountr   r   r   _get_account_infoW   sj      
    
zNFLBaseIE._get_account_infoc              	   C   s   | j r | jtt d kr d S | js2| || | jd| jdrHdnd |ddidtj	| j
| jd	d
 d}|d | _ |d | _|d | jd< d S )N   z'https://api.nfl.com/identity/v3/token%sZrefreshTokenz/refreshr   r0   zapplication/jsonzDownloading access tokenr   r   )r3   r1   r2   ZaccessTokenZ	expiresIn)_TOKEN_TOKEN_EXPIRYinttimer<   rD   r;   getr+   dumps_CLIENT_DATAencode)r>   r?   r@   tokenr   r   r   _get_auth_token   s       

zNFLBaseIE._get_auth_tokenc                 C   s  |  ||}|d d }|d}|r@| | j | t|}n|dpP|d }|d}|d }d|i}t|}	|	dkr| ||d	|d
< n||d< |ddkrd|d< |ddk}
d }||dp||d}|r|t|ddg}|||
t|d|d |S )NZplaylistr   ZmcpIDidZentityIdtitler?   m3u8mp4formatsaudioTnoneZvcodecZliveZimageSrcZposterImageZjpg)r?   extdescription)rQ   is_liverX   
thumbnails)	Z_parse_jsonrJ   
url_result_ANVATO_PREFIXr   r   Z_extract_m3u8_formatsupdater   )r>   video_config
display_iditemZmcp_idinfoZmedia_idrQ   Zitem_urlrW   rY   rZ   Z	image_urlr   r   r   _parse_video_config   s:    

 zNFLBaseIE._parse_video_config)__name__
__module____qualname___VALID_URL_BASE_VIDEO_CONFIG_REGEXr\   struuidZuuid4base64	b64encoder+   rK   rM   decoderL   r<   r8   rF   rG   rD   rO   rb   r   r   r   r   r      s2   ,
*r   c                   @   sl   e Zd ZdZejd Zddddddd	d
dddd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899441rS   z;Baker Mayfield's game-changing plays from 3-TD game Week 14z$md5:85e05a3cc163f8c344340f220521136dZ20201215iH_zre:^https?://.*\.jpg$NFLzcount:6   zcount:3)rP   rW   rQ   rX   upload_date	timestamp	thumbnailuploadertagsduration
categories)r?   	info_dictzchttps://www.chiefs.com/listen/patrick-mahomes-travis-kelce-react-to-win-over-dolphins-the-breakdownZ 6886b32c24b463038c760ceb55a34566z$d87e8790-3e14-11eb-8ceb-ff05c2867f99Zmp3zHPatrick Mahomes, Travis Kelce React to Win Over Dolphins | The Breakdownz$md5:12ada8ee70e6762658c30e223e095075)rP   rW   rQ   rX   zHTTP Error 404: Not Found)r?   md5rw   skipzMhttps://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_idr9   rb   r:   rg   )r>   r?   r_   rA   r   r   r   _real_extract   s    
  zNFLIE._real_extractN)	rc   rd   re   IE_NAMEr   rf   
_VALID_URL_TESTSr{   r   r   r   r   rm      sB   
!rm   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)rP   rQ      )r?   rw   playlist_countc                 C   sl   |  |}| ||}g }t| j|D ]}|| || q(ttd|p\| 	ddg|}| 
|||S )Nznfl-c-article__titlezog:titleztwitter:title)rz   r9   refindallrg   appendrb   r   r	   Z_html_search_metaplaylist_result)r>   r?   r_   rA   entriesr^   rQ   r   r   r   r{      s    
  zNFLArticleIE._real_extractN)	rc   rd   re   r|   r   rf   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dddg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/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1572108rS   z$New York Giants at Minnesota VikingszSNew York Giants play the Minnesota Vikings at U.S. Bank Stadium on January 15, 2023rn   Z20230116iH%ci  zGame HighlightszMinnesota VikingszNew York Giantsz%Minnesota Vikings vs. New York Giantsre:^https?://.*\.jpgrP   rW   rQ   rX   rs   rp   rq   ru   rv   rt   rr   skip_downloadrR   )r?   rw   paramsSubscription requiredz<https://www.nfl.com/plus/games/giants-at-vikings-2022-post-1r   rP   zgiants-at-vikings-2022-post-1)r1   r?   r   rw   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r1   r?   rw   r   z	Full GamezFull Game - SpanishzCondensed GamezAll-22)Z	full_gameZfull_game_spanishr   Zall_22c                    s   |dd\}}ddgdk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j | t|S  fdd}| |S )Nr@   rP   r   allAuthorizationBearer z6https://api.nfl.com/football/v2/games/externalId/slug/zDownloading game IDZwithExternalIdstrue)queryr3   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   H  r   z/NFLPlusReplayIE._real_extract.<locals>.<lambda>mcpPlaybackIdFr#   c                  3   sB   t  dfddfD ]&} | d }j | t|V  qd S )Nr   c                    s   |d o|d  kS )Nr   r   r   r   r   r   r   r   O  r   z@NFLPlusReplayIE._real_extract.<locals>.entries.<locals>.<lambda>r   )r
   r[   r\   r   )Zreplayvideo_idZreplaysr   r>   r   r   r   M  s     z.NFLPlusReplayIE._real_extract.<locals>.entries)Z_match_valid_urlgroupZ_configuration_arglist_REPLAY_TYPESkeysr
   rO   rF   r;   r=   r[   r\   r   r   )r>   r?   r@   r   r3   Zgame_idr   r   r   r   r{   6  sH           
 zNFLPlusReplayIE._real_extractN)rc   rd   re   r|   r}   r~   r   r{   r   r   r   r   r      sp     5r   c                   @   sL   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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1576832rS   zConference Championshipsz$md5:944f7fab56f7a37430bf8473f5473857rn   Z20230127i((ci  ZAnalysisz6Cincinnati Bengals at Kansas City Chiefs (2022-POST-3)r   r   r   rR   r   c                 C   sT   |  |}| || | jd| |dd| j idd }| | j | t|S )Nz/https://api.nfl.com/content/v1/videos/episodes/r   r   )r3   r   )rz   rO   r;   rF   r[   r\   r   )r>   r?   r@   r   r   r   r   r{   m  s    
  
zNFLPlusEpisodeIE._real_extractN)rc   rd   re   r|   r}   r~   r{   r   r   r   r   r   W  s&   r   )rj   r+   r   rI   ri   Zanvator   commonr   utilsr   r   r   r	   r
   r   r   rm   r   r   r   r   r   r   r   <module>   s    
 #+_