]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/npr.py
Update changelog.
[youtubedl] / youtube_dl / extractor / npr.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5 int_or_none,
6 qualities,
7 url_or_none,
8 )
9
10
11 class NprIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?npr\.org/(?:sections/[^/]+/)?\d{4}/\d{2}/\d{2}/(?P<id>\d+)'
13 _TESTS = [{
14 'url': 'https://www.npr.org/sections/allsongs/2015/10/21/449974205/new-music-from-beach-house-chairlift-cmj-discoveries-and-more',
15 'info_dict': {
16 'id': '449974205',
17 'title': 'New Music From Beach House, Chairlift, CMJ Discoveries And More'
18 },
19 'playlist_count': 7,
20 }, {
21 'url': 'https://www.npr.org/sections/deceptivecadence/2015/10/09/446928052/music-from-the-shadows-ancient-armenian-hymns-and-piano-jazz',
22 'info_dict': {
23 'id': '446928052',
24 'title': "Songs We Love: Tigran Hamasyan, 'Your Mercy is Boundless'"
25 },
26 'playlist': [{
27 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
28 'info_dict': {
29 'id': '446929930',
30 'ext': 'mp3',
31 'title': 'Your Mercy is Boundless (Bazum en Qo gtutyunqd)',
32 'duration': 402,
33 },
34 }],
35 }, {
36 # mutlimedia, not media title
37 'url': 'https://www.npr.org/2017/06/19/533198237/tigers-jaw-tiny-desk-concert',
38 'info_dict': {
39 'id': '533198237',
40 'title': 'Tigers Jaw: Tiny Desk Concert',
41 },
42 'playlist': [{
43 'md5': '12fa60cb2d3ed932f53609d4aeceabf1',
44 'info_dict': {
45 'id': '533201718',
46 'ext': 'mp4',
47 'title': 'Tigers Jaw: Tiny Desk Concert',
48 'duration': 402,
49 },
50 }],
51 'expected_warnings': ['Failed to download m3u8 information'],
52 }, {
53 # multimedia, no formats, stream
54 'url': 'https://www.npr.org/2020/02/14/805476846/laura-stevenson-tiny-desk-concert',
55 'only_matching': True,
56 }]
57
58 def _real_extract(self, url):
59 playlist_id = self._match_id(url)
60
61 story = self._download_json(
62 'http://api.npr.org/query', playlist_id, query={
63 'id': playlist_id,
64 'fields': 'audio,multimedia,title',
65 'format': 'json',
66 'apiKey': 'MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010',
67 })['list']['story'][0]
68 playlist_title = story.get('title', {}).get('$text')
69
70 KNOWN_FORMATS = ('threegp', 'm3u8', 'smil', 'mp4', 'mp3')
71 quality = qualities(KNOWN_FORMATS)
72
73 entries = []
74 for media in story.get('audio', []) + story.get('multimedia', []):
75 media_id = media['id']
76
77 formats = []
78 for format_id, formats_entry in media.get('format', {}).items():
79 if not formats_entry:
80 continue
81 if isinstance(formats_entry, list):
82 formats_entry = formats_entry[0]
83 format_url = formats_entry.get('$text')
84 if not format_url:
85 continue
86 if format_id in KNOWN_FORMATS:
87 if format_id == 'm3u8':
88 formats.extend(self._extract_m3u8_formats(
89 format_url, media_id, 'mp4', 'm3u8_native',
90 m3u8_id='hls', fatal=False))
91 elif format_id == 'smil':
92 smil_formats = self._extract_smil_formats(
93 format_url, media_id, transform_source=lambda s: s.replace(
94 'rtmp://flash.npr.org/ondemand/', 'https://ondemand.npr.org/'))
95 self._check_formats(smil_formats, media_id)
96 formats.extend(smil_formats)
97 else:
98 formats.append({
99 'url': format_url,
100 'format_id': format_id,
101 'quality': quality(format_id),
102 })
103 for stream_id, stream_entry in media.get('stream', {}).items():
104 if not isinstance(stream_entry, dict):
105 continue
106 if stream_id != 'hlsUrl':
107 continue
108 stream_url = url_or_none(stream_entry.get('$text'))
109 if not stream_url:
110 continue
111 formats.extend(self._extract_m3u8_formats(
112 stream_url, stream_id, 'mp4', 'm3u8_native',
113 m3u8_id='hls', fatal=False))
114 self._sort_formats(formats)
115
116 entries.append({
117 'id': media_id,
118 'title': media.get('title', {}).get('$text') or playlist_title,
119 'thumbnail': media.get('altImageUrl', {}).get('$text'),
120 'duration': int_or_none(media.get('duration', {}).get('$text')),
121 'formats': formats,
122 })
123
124 return self.playlist_result(entries, playlist_id, playlist_title)