
    yfC                         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mZmZmZmZ  G d de      Z G d d	e      Z G d
 de      Z G d de      Z G d de      Zy)    N   )InfoExtractor   )ExtractorError
clean_htmldetermine_extextract_attributesfloat_or_noneget_elements_html_by_classint_or_nonemerge_dictsmimetype2extparse_iso8601
remove_endremove_startstr_or_nonetraverse_objurl_or_nonec                   T    e Zd Z ej                  d      ZdZdZdZd Z	d Z
d Zd Zy	)
NYTimesBaseIEz$36dd619a-56dc-595b-9e09-37f4152c7b5da  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuNIzKBOFB77aT/jN/FQ+/QVKWq5V1ka1AYmCR9hstz1pGNPH5ajOU9gAqta0T89iPnhjwla+3oec/Z3kGjxbpv6miQXufHFq3u2RC6HyU458cLat5kVPSOQCe3VVB5NRpOlRuwKHqn0txfxnwSSj8mqzstR997d3gKB//RO9zE16y3PoWlDQXkASngNJEWvL19iob/xwAkfEWCjyRILWFY0JYX3AvLMSbq7wsqOCE5srJpo7rRU32zsByhsp1D5W9OYqqwDmflsgCEQy2vqTsJjrJohuNg+urMXNNZ7Y3naMoqttsGDrWVxtPBafKMI8pM2ReNZBbGQsQXRzQNo7+QIDAQABz/https://samizdat-graphql.nytimes.com/graphql/v2a  query VideoQuery($id: String!) {
  video(id: $id) {
    ... on Video {
      bylines {
        renderedRepresentation
      }
      duration
      firstPublished
      promotionalHeadline
      promotionalMedia {
        ... on Image {
          crops {
            name
            renditions {
              name
              width
              height
              url
            }
          }
        }
      }
      renditions {
        type
        width
        height
        url
        bitrate
      }
      summary
    }
  }
}c                 f   t        j                  | j                  d      }t        j                  ||      }t        | j	                  | j
                  |dt        j                  | j                  dd| idd      j                         dd	d
| j                  ddd      ddt        hf      xs i S )Nvideoz!Downloading JSON from GraphQL APIidznyt://video/)query	variables),:)
separatorszapplication/jsonvhszv3.52.21zhttps://nytimes.com)zContent-TypezNyt-App-TypezNyt-App-Versionz	Nyt-TokenOriginF)dataheadersfatalr!   )uuiduuid5_DNS_NAMESPACEr   _download_json_GRAPHQL_APIjsondumps_GRAPHQL_QUERYencode_TOKENdict)selfmedia_id
video_uuid
media_uuids       :/usr/lib/python3/dist-packages/yt_dlp/extractor/nytimes.py	_call_apizNYTimesBaseIE._call_api>   s    ZZ 3 3W=
ZZ
H5
D//x)LSWS]S],,"l:,$?@_ %T& '-fh 2 %#-![[/9  0 
 %gv6
8 
> <>
	>    c                 H    t        |d ddt        hfdt        hfdfd       S )Nc                     t        |d         S )Nurl)r   _vs     r3   <lambda>z3NYTimesBaseIE._extract_thumbnails.<locals>.<lambda>P   s    +ah2G r5   r8   widthheight)r8   r=   r>   default)r   r   )r/   thumbss     r3   _extract_thumbnailsz!NYTimesBaseIE._extract_thumbnailsO   s;    F%G}-+/J
 % 	 	r5   c                    g }g }i }t        |ddt        hf      D ]T  }|j                  d      }|j                  d      }|r	|dk(  s||v r2|j                  |       t	        |j                  d            xs t        |      }	|	dk(  rC| j                  ||dd	|xs d
d      \  }
}|j                  |
       | j                  ||       |	dk(  r|j                  |||j                  d      xs |j                  d      t        |j                  d            t        |j                  d            t        |ddt        hfd      t        |j                  d      d      xs d |	d       W ||fS )N
renditions.r8   typerA   mimetypem3u8mp4m3u8_nativehlsF)m3u8_idr#   )targetmpdvideoencodingvideo_codecr=   r>   )	file_sizefileSize)Nvalueget_allbitrate  )r8   	format_idvcodecr=   r>   filesizetbrext)
r   r.   getappendr   r   #_extract_m3u8_formats_and_subtitlesextend_merge_subtitlesr   )r/   video_idcontent_media_jsonurlsformats	subtitlesr   	video_urlrW   r[   	m3u8_fmts	m3u8_subss               r3   _extract_formats_and_subtitlesz,NYTimesBaseIE._extract_formats_and_subtitlesV   s   	!"4|S4&6QR 	E		%(I		&)I	X 5d9JKK	"uyy45Qy9QCf}'+'O'Ox%.e (P (=$	9 y)%%i	%B$!*#ii8TEIIm<T(7);<)%))H*=> ,U13D{m5U^c!e&uyy';TBJd
  
!	8 	!!r5   c                 v   | j                  |      }| j                  ||      \  }}||j                  d      |j                  d      t        |j                  d            t	        |j                  d      d      dj                  t        |dd	d
d hf            ||| j                  t        |d            d	S )NpromotionalHeadlinesummaryfirstPublisheddurationrV   scale, bylines.renderedRepresentationc                     t        | d      S )NzBy )r   xs    r3   r<   z.NYTimesBaseIE._extract_video.<locals>.<lambda>   s    \RSUZE[ r5   promotionalMediacrops.rD   .)	r   titledescription	timestamprn   creatorrd   re   
thumbnails)r4   ri   r\   r   r
   joinr   rB   )r/   r0   r!   rd   re   s        r3   _extract_videozNYTimesBaseIE._extract_videox   s    ~~h'!@@4P XX3488I.&txx0@'AB%dhhz&:$Gyyd3 8;[:\5^ "_ `"22T#XY[
 	
r5   N)__name__
__module____qualname__r$   UUIDr&   r-   r(   r+   r4   rB   ri   r    r5   r3   r   r      s=    TYYEFN XFDL ND>" "D
r5   r   c                   F    e Zd ZdZdgZddddddd	d
dddd	ddddgZd Zy)	NYTimesIEzhttps?://(?:(?:www\.)?nytimes\.com/video/(?:[^/]+/)+?|graphics8\.nytimes\.com/bcvideo/\d+(?:\.\d+)?/iframe/embed\.html\?videoId=)(?P<id>\d+)zl<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//graphics8\.nytimes\.com/bcvideo/[^/]+/iframe/embed\.html.+?)\1>zshttp://www.nytimes.com/video/opinion/100000002847155/verbatim-what-is-a-photocopier.html?playlistId=100000001150263 a553aa344014e3723d33893d89d4defc100000002847155rH   z Verbatim: What Is a Photocopier?z$md5:93603dada88ddbda9395632fdc5da260it]S20140428zBrett Weinerz're:https?://\w+\.nyt.com/images/.+\.jpgi  	r   r[   rz   r{   r|   upload_dater}   	thumbnailrn   r8   md5	info_dictzJhttp://www.nytimes.com/video/travel/100000003550828/36-hours-in-dubai.htmlTr8   only_matchingc                 F    | j                  |      }| j                  |      S )N)	_match_idr   )r/   r8   ra   s      r3   _real_extractzNYTimesIE._real_extract   s!    >>#&""8,,r5   N)r   r   r   
_VALID_URL_EMBED_REGEX_TESTSr   r   r5   r3   r   r      s`     aJ D  EL E1#7A#%%C

 \F&-r5   r   c                       e Zd Z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dddddd d!d"dd#d
d$d%id&d'd(d)d*d+d,d-d.d/d0d1d2d3gZd4 Zd5 Zy6)7NYTimesArticleIEzphttps?://(?:www\.)?nytimes\.com/\d{4}/\d{2}/\d{2}/(?!books|podcasts)[^/?#]+/(?:\w+/)?(?P<id>[^./?#]+)(?:\.html)?zhttp://www.nytimes.com/2015/04/14/business/owner-of-gravity-payments-a-credit-card-processor-is-setting-a-new-minimum-wage-70000-a-year.html?_r=0 3eb5ddb1d6f86254fe4f233826778737100000003628438rH   u0   One Company’s New Minimum Wage: $70,000 a Yearz$md5:89ba9ab67ca767bb92bf823d1f138433i-U20150414zMatthew WilliamszPatricia Cohen're:https?://\w+\.nyt.com/images/.*\.jpgg     ]@)
r   r[   rz   r{   r|   r   uploaderr}   r   rn   r   zMhttps://www.nytimes.com/2023/09/29/health/mosquitoes-genetic-engineering.html 2365b3555c8aa7f4dd34ca735ad02e6a100000009110381mp3z<The Gamble: Can Genetically Modified Mosquitoes End Disease?z$md5:9ff8b47acbaf7f3ca8c732f5c815be2ei|Ne20230929z#Stephanie Nolen, Natalija Gormalovai*  r   zThttps://www.nytimes.com/2023/11/29/business/dealbook/kamala-harris-biden-voters.html100000009202270uU   Kamala Harris Defends Biden Policies, but Says ‘More Work’ Needed to Reach Votersz$md5:de4212a7e19bb89e4fb14210ca915f1fige20231129zBy The New York TimeszKatie RogersgMbhX@skip_downloadrG   )r8   r   r   paramszOhttps://www.nytimes.com/2023/12/02/business/air-traffic-controllers-safety.htmlzair-traffic-controllers-safetyzHDrunk and Asleep on the Job: Air Traffic Controllers Pushed to the Brinkz$md5:549e5a5e935bf7d048be53ba3d2c863d20231202zEmily Steel, Sydney Emberi` ke)r   rz   r{   r   r}   r|      r8   r   playlist_countzShttps://www.nytimes.com/2023/12/02/business/media/netflix-squid-game-challenge.htmlTr   c           	      h   t        |dt        hfdddt        hfd dd hfdt        hfffdt        hfd	t        hfd
d      }| j	                  |j                  d      |      \  }}t        |dt        hf      }|s|r|j                  |dd       i || j                  t        |d            ||dS )NsourceIdrr   .rs   rn   c                     t        | d      S )NrV   ro   )r
   ru   s    r3   r<   z>NYTimesArticleIE._extract_content_from_block.<locals>.<lambda>   s    at8T r5   lengthrm   podcastSeries)r   r   rn   r|   seriesFrS   r   fileUrlnone)r8   rX   rw   )r~   rd   re   )	r   strr   r   ri   r\   r   r]   rB   )r/   blockdetailsrd   re   r8   s         r3   _extract_content_from_blockz,NYTimesArticleIE._extract_content_from_block   s    uu%"C)AC5I.T-U VYadocpXqrs*]O<&.'
  "@@TARTYZ59{m"<=3NN3&9:

22<L4N O"
 	
r5   c                    | j                  |      }| j                  ||      }| j                  d|d|d       d   d   d   }t        |dd	d
dd f      }|st	        d      t        | j                  |      d      t        |dd	d
dd	d
dt        hfd      xs | j                  ddg|      t        |dt        hf      dj                  t        |d            | j                  t        |d            d}g }|D ],  }|j                  t        | j                  |      |             . t        |      dkD  r | j                   ||fi |S d|i|d   S )Nzwindow\.__preloadedData\s*=zmedia detailsc                 &    | j                  dd      S )N	undefinednull)replaceru   s    r3   r<   z0NYTimesArticleIE._real_extract.<locals>.<lambda>  s    qyyf'E r5   )transform_sourceinitialDatar!   articlesprinkledBodycontent.)	ledeMediaNc                     |d   dv S )N
__typename)VideoAudior   r9   s     r3   r<   z0NYTimesArticleIE._real_extract.<locals>.<lambda>
  s    <,>> r5   z/Unable to extract any media blocks from webpagez - The New York Timesrl   textFrS   og:descriptiontwitter:descriptionrm   rq   )rr   .creators.displayName)rx   
assetCrops.rD   .)rz   r{   r|   r}   r~   r   r   r   )r   _download_webpage_search_jsonr   r   r   _html_extract_titler   _html_search_metar   r   rB   r]   r   r   lenplaylist_result)	r/   r8   page_idwebpageart_jsonblockscommon_infoentriesr   s	            r3   r   zNYTimesArticleIE._real_extract  s   ..%((g6$$*G_gE % GGTVV\^^gi hY-@>)@ A  !RSS   8 8 ACZ['CIsFUXTY3[ m"&"8"8:JLa9bdk"l%h1AM?0STyyX'WXZ22<T4V W

  	^ENN;t'G'G'NP[\]	^ w<!'4''HKHH '
aj
 	
r5   N)r   r   r   r   r   r   r   r   r5   r3   r   r      s     EJ c1#GA#%*'C
" _1#SA#%<C

	  f1#lA#%/%C
 V
( a2_A%2#
  ey?FB
.#
r5   r   c                   b    e Zd ZdZ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gZd Zy)NYTimesCookingIENYTimesCookingGuidez4https?://cooking\.nytimes\.com/guides/(?P<id>[\w-]+)z:https://cooking.nytimes.com/guides/13-how-to-cook-a-turkeyz13-how-to-cook-a-turkeyzHow to Cook a Turkeyz$md5:726cfd3f9b161bdf5c279879e8050ca0)r   rz   r{   r   r   z@https://cooking.nytimes.com/guides/50-how-to-make-mac-and-cheese 64415805fe0b8640fce6b0b9def5989a100000005835845rH   zHow to Make Mac and Cheesez$md5:b8f2f33ec1fb7523b21367147c9594f1i`Z20180405gQ#@zAlison Romanr   )	r   r[   rz   r{   r|   r   rn   r}   r   r   z9https://cooking.nytimes.com/guides/20-how-to-frost-a-cakez20-how-to-frost-a-cakezHow to Frost a Cakez$md5:a31fe3b98a8ce7b98aae097730c269cd   )r8   r   r   r   c           	         | j                  |      }| j                  ||      }| j                  ddg|      }| j                  ddg|      }| j                  d|d      }t	        t        d|      dt        hd	f      }|rB|j                  |       | j                  |D cg c]  }| j                  |       c}|||      S i | j                  |      ||| j                  d
|dd       dS c c}w )Nzog:titleztwitter:titler   r   z#data-video-player-id="(\d+)"></div>z
lead videoz
video-item.zdata-video-idz*<span itemprop="author">([^<]+)</span></p>authorr?   )rz   r{   r}   )
r   r   r   _search_regexr   r   r	   r]   r   r   )	r/   r8   r   r   rz   r{   lead_video_id	media_idsr0   s	            r3   r   zNYTimesCookingIE._real_extractL  s#   ..%((g6&&
O'DgN,,.>@U-VX_`**2G\K &|W=FXEY[j?km	 ]+''?HI8$$X.I7TY[fh h
!!-0
&))=wZ^ * `	
 	
 Js   C-N)r   r   r   IE_NAMEr   r   r   r   r5   r3   r   r   '  s    #GHJK++A

  R1#1A#%%C

	  K1**A

 	/ FD
r5   r   c                   n    e Zd Z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dddddddgZd Zy )!NYTimesCookingRecipeIEz2https?://cooking\.nytimes\.com/recipes/(?P<id>\d+)z?https://cooking.nytimes.com/recipes/1017817-cranberry-curd-tart 579e83bbe8e61e9de67f80edba8a78a81017817rH   zCranberry Curd Tartz$md5:ad77a3fc321db636256d4343c5742152iKV20151118zDavid Tanisr   )r   r[   rz   r{   r|   r   r}   r   r   zKhttps://cooking.nytimes.com/recipes/1024781-neapolitan-checkerboard-cookies 58df35998241dcf0620e99e646331b421024781zNeapolitan Checkerboard Cookiesz$md5:ba12394c585ababea951cb6d2fcc6631ige20231130zSue Liz:https://cooking.nytimes.com/recipes/1019516-overnight-oats 2fe7965a3adc899913b8e25ada3608231019516i +\z$md5:8856ce10239161bd2596ac335b9f9bfb20190102zOvernight OatszGenevieve Ko)r   r[   r|   r{   r   rz   r}   r   c                    | j                  |      }| j                  ||      }| j                  ||      d   d   d   }| j                  |d   |dd      \  }}i t	        |dt
        hfd	t        hfd
t        hfdt        hfddt        hfd      ||t	        |ddddt        hf      D cg c]  }d|i c}dS c c}w )Nprops	pagePropsrecipevideoSrcrH   rJ   )rK   r   rz   topnotepublishedAtcontentAttribution
cardByline)r   rz   r{   r|   r}   imagery   .r8   )rd   re   r~   )
r   r   _search_nextjs_datar^   r   r   r   r   r   r   )r/   r8   r   r   recipe_datard   re   	thumb_urls           r3   r   z$NYTimesCookingRecipeIE._real_extract  s   ..%((g6..w@I+VW_`!EE
#WeU F D
;k]+!C5) )J<8+k];0,F) 
 "?Kgw#}M@O P)E9- P
 	
Ps   +B<
N)r   r   r   r   r   r   r   r5   r3   r   r   e  s    FJP1*A#%$C	
 ]16A#%C	
 L1#A%%%C	
5'FR
r5   r   )r)   r$   commonr   utilsr   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r5   r3   <module>r      sj      !    &p
M p
f- -8}
} }
@;
} ;
|?
] ?
r5   