]>
jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/npr.py
1 from .common
import InfoExtractor
9 class NprIE(InfoExtractor
):
10 _VALID_URL
= r
'https?://(?:www\.)?npr\.org/(?:sections/[^/]+/)?\d{4}/\d{2}/\d{2}/(?P<id>\d+)'
12 'url': 'https://www.npr.org/sections/allsongs/2015/10/21/449974205/new-music-from-beach-house-chairlift-cmj-discoveries-and-more',
15 'title': 'New Music From Beach House, Chairlift, CMJ Discoveries And More'
19 'url': 'https://www.npr.org/sections/deceptivecadence/2015/10/09/446928052/music-from-the-shadows-ancient-armenian-hymns-and-piano-jazz',
22 'title': "Songs We Love: Tigran Hamasyan, 'Your Mercy is Boundless'"
25 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
29 'title': 'Your Mercy is Boundless (Bazum en Qo gtutyunqd)',
34 # multimedia, not media title
35 'url': 'https://www.npr.org/2017/06/19/533198237/tigers-jaw-tiny-desk-concert',
38 'title': 'Tigers Jaw: Tiny Desk Concert',
41 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
45 'title': 'Tigers Jaw: Tiny Desk Concert',
49 'expected_warnings': ['Failed to download m3u8 information'],
51 # multimedia, no formats, stream
52 'url': 'https://www.npr.org/2020/02/14/805476846/laura-stevenson-tiny-desk-concert',
53 'only_matching': True,
56 def _real_extract(self
, url
):
57 playlist_id
= self
._match
_id
(url
)
59 story
= self
._download
_json
(
60 'http://api.npr.org/query', playlist_id
, query
={
62 'fields': 'audio,multimedia,title',
64 'apiKey': 'MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010',
65 })['list']['story'][0]
66 playlist_title
= story
.get('title', {}).get('$text')
68 KNOWN_FORMATS
= ('threegp', 'm3u8', 'smil', 'mp4', 'mp3')
69 quality
= qualities(KNOWN_FORMATS
)
72 for media
in story
.get('audio', []) + story
.get('multimedia', []):
73 media_id
= media
['id']
76 for format_id
, formats_entry
in media
.get('format', {}).items():
79 if isinstance(formats_entry
, list):
80 formats_entry
= formats_entry
[0]
81 format_url
= formats_entry
.get('$text')
84 if format_id
in KNOWN_FORMATS
:
85 if format_id
== 'm3u8':
86 formats
.extend(self
._extract
_m
3u8_formats
(
87 format_url
, media_id
, 'mp4', 'm3u8_native',
88 m3u8_id
='hls', fatal
=False))
89 elif format_id
== 'smil':
90 smil_formats
= self
._extract
_smil
_formats
(
91 format_url
, media_id
, transform_source
=lambda s
: s
.replace(
92 'rtmp://flash.npr.org/ondemand/', 'https://ondemand.npr.org/'),
94 self
._check
_formats
(smil_formats
, media_id
)
95 formats
.extend(smil_formats
)
99 'format_id': format_id
,
100 'quality': quality(format_id
),
102 for stream_id
, stream_entry
in media
.get('stream', {}).items():
103 if not isinstance(stream_entry
, dict):
105 if stream_id
!= 'hlsUrl':
107 stream_url
= url_or_none(stream_entry
.get('$text'))
110 formats
.extend(self
._extract
_m
3u8_formats
(
111 stream_url
, stream_id
, 'mp4', 'm3u8_native',
112 m3u8_id
='hls', fatal
=False))
113 self
._sort
_formats
(formats
)
117 'title': media
.get('title', {}).get('$text') or playlist_title
,
118 'thumbnail': media
.get('altImageUrl', {}).get('$text'),
119 'duration': int_or_none(media
.get('duration', {}).get('$text')),
123 return self
.playlist_result(entries
, playlist_id
, playlist_title
)