1 from .common
import InfoExtractor
2 from ..utils
import int_or_none
, qualities
, traverse_obj
, url_or_none
5 class NprIE(InfoExtractor
):
6 _VALID_URL
= r
'https?://(?:www\.)?npr\.org/(?:sections/[^/]+/)?\d{4}/\d{2}/\d{2}/(?P<id>\d+)'
8 'url': 'https://www.npr.org/sections/allsongs/2015/10/21/449974205/new-music-from-beach-house-chairlift-cmj-discoveries-and-more',
11 'title': 'New Music From Beach House, Chairlift, CMJ Discoveries And More',
15 'url': 'https://www.npr.org/sections/deceptivecadence/2015/10/09/446928052/music-from-the-shadows-ancient-armenian-hymns-and-piano-jazz',
18 'title': "Songs We Love: Tigran Hamasyan, 'Your Mercy is Boundless'",
21 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
25 'title': 'Your Mercy is Boundless (Bazum en Qo gtutyunqd)',
30 # multimedia, not media title
31 'url': 'https://www.npr.org/2017/06/19/533198237/tigers-jaw-tiny-desk-concert',
34 'title': 'Tigers Jaw: Tiny Desk Concert',
37 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
41 'title': 'Tigers Jaw: Tiny Desk Concert',
45 'expected_warnings': ['Failed to download m3u8 information'],
47 # multimedia, no formats, stream
48 'url': 'https://www.npr.org/2020/02/14/805476846/laura-stevenson-tiny-desk-concert',
49 'only_matching': True,
51 'url': 'https://www.npr.org/2022/03/15/1084896560/bonobo-tiny-desk-home-concert',
55 'title': 'Bonobo: Tiny Desk (Home) Concert',
57 'thumbnail': r
're:^https?://media.npr.org/assets/img/.*\.jpg$',
61 def _real_extract(self
, url
):
62 playlist_id
= self
._match
_id
(url
)
64 story
= self
._download
_json
(
65 'http://api.npr.org/query', playlist_id
, query
={
67 'fields': 'audio,multimedia,title',
69 'apiKey': 'MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010',
70 })['list']['story'][0]
71 playlist_title
= story
.get('title', {}).get('$text')
73 KNOWN_FORMATS
= ('threegp', 'm3u8', 'smil', 'mp4', 'mp3')
74 quality
= qualities(KNOWN_FORMATS
)
77 for media
in story
.get('audio', []) + story
.get('multimedia', []):
78 media_id
= media
['id']
81 for format_id
, formats_entry
in media
.get('format', {}).items():
84 if isinstance(formats_entry
, list):
85 formats_entry
= formats_entry
[0]
86 format_url
= formats_entry
.get('$text')
89 if format_id
in KNOWN_FORMATS
:
90 if format_id
== 'm3u8':
91 formats
.extend(self
._extract
_m
3u8_formats
(
92 format_url
, media_id
, 'mp4', 'm3u8_native',
93 m3u8_id
='hls', fatal
=False))
94 elif format_id
== 'smil':
95 smil_formats
= self
._extract
_smil
_formats
(
96 format_url
, media_id
, transform_source
=lambda s
: s
.replace(
97 'rtmp://flash.npr.org/ondemand/', 'https://ondemand.npr.org/'),
99 self
._check
_formats
(smil_formats
, media_id
)
100 formats
.extend(smil_formats
)
104 'format_id': format_id
,
105 'quality': quality(format_id
),
107 for stream_id
, stream_entry
in media
.get('stream', {}).items():
108 if not isinstance(stream_entry
, dict):
110 if stream_id
!= 'hlsUrl':
112 stream_url
= url_or_none(stream_entry
.get('$text'))
115 formats
.extend(self
._extract
_m
3u8_formats
(
116 stream_url
, stream_id
, 'mp4', 'm3u8_native',
117 m3u8_id
='hls', fatal
=False))
120 raw_json_ld
= self
._yield
_json
_ld
(self
._download
_webpage
(url
, playlist_id
), playlist_id
, fatal
=False)
121 m3u8_url
= traverse_obj(list(raw_json_ld
), (..., 'subjectOf', ..., 'embedUrl'), get_all
=False)
122 formats
= self
._extract
_m
3u8_formats
(m3u8_url
, media_id
, 'mp4', m3u8_id
='hls', fatal
=False)
126 'title': media
.get('title', {}).get('$text') or playlist_title
,
127 'thumbnail': media
.get('altImageUrl', {}).get('$text'),
128 'duration': int_or_none(media
.get('duration', {}).get('$text')),
132 return self
.playlist_result(entries
, playlist_id
, playlist_title
)