]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/abc.py
[extractor] Deprecate `_sort_formats`
[yt-dlp.git] / yt_dlp / extractor / abc.py
CommitLineData
2e65e7db 1import hashlib
2import hmac
64ce58db 3import re
2e65e7db 4import time
64ce58db
JMF
5
6from .common import InfoExtractor
58179eb7 7from ..compat import compat_str
17a64763 8from ..utils import (
6839d02c 9 dict_get,
17a64763
YCH
10 ExtractorError,
11 js_to_json,
c0a65687 12 int_or_none,
55d119e2 13 parse_iso8601,
76b01870 14 str_or_none,
58179eb7 15 try_get,
9e6a4180 16 unescapeHTML,
77341dae 17 update_url_query,
17a64763 18)
64ce58db
JMF
19
20
21class ABCIE(InfoExtractor):
22 IE_NAME = 'abc.net.au'
5630d797 23 _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/(?:news|btn)/(?:[^/]+/){1,4}(?P<id>\d{5,})'
64ce58db 24
17a64763 25 _TESTS = [{
732c848c
MK
26 'url': 'http://www.abc.net.au/news/2014-11-05/australia-to-staff-ebola-treatment-centre-in-sierra-leone/5868334',
27 'md5': 'cb3dd03b18455a661071ee1e28344d9f',
64ce58db 28 'info_dict': {
732c848c 29 'id': '5868334',
64ce58db 30 'ext': 'mp4',
732c848c
MK
31 'title': 'Australia to help staff Ebola treatment centre in Sierra Leone',
32 'description': 'md5:809ad29c67a05f54eb41f2a105693a67',
64ce58db 33 },
5c5a3ecf 34 'skip': 'this video has expired',
17a64763
YCH
35 }, {
36 'url': 'http://www.abc.net.au/news/2015-08-17/warren-entsch-introduces-same-sex-marriage-bill/6702326',
76b01870 37 'md5': '4ebd61bdc82d9a8b722f64f1f4b4d121',
17a64763
YCH
38 'info_dict': {
39 'id': 'NvqvPeNZsHU',
40 'ext': 'mp4',
41 'upload_date': '20150816',
42 'uploader': 'ABC News (Australia)',
43 'description': 'Government backbencher Warren Entsch introduces a cross-party sponsored bill to legalise same-sex marriage, saying the bill is designed to promote "an inclusive Australia, not a divided one.". Read more here: http://ab.co/1Mwc6ef',
44 'uploader_id': 'NewsOnABC',
45 'title': 'Marriage Equality: Warren Entsch introduces same sex marriage bill',
46 },
47 'add_ie': ['Youtube'],
5c5a3ecf 48 'skip': 'Not accessible from Travis CI server',
7687b354 49 }, {
50 'url': 'http://www.abc.net.au/news/2015-10-23/nab-lifts-interest-rates-following-westpac-and-cba/6880080',
51 'md5': 'b96eee7c9edf4fc5a358a0252881cc1f',
52 'info_dict': {
53 'id': '6880080',
54 'ext': 'mp3',
55 'title': 'NAB lifts interest rates, following Westpac and CBA',
56 'description': 'md5:f13d8edc81e462fce4a0437c7dc04728',
57 },
d97da29d
JMF
58 }, {
59 'url': 'http://www.abc.net.au/news/2015-10-19/6866214',
60 'only_matching': True,
5630d797
AH
61 }, {
62 'url': 'https://www.abc.net.au/btn/classroom/wwi-centenary/10527914',
63 'info_dict': {
64 'id': '10527914',
65 'ext': 'mp4',
66 'title': 'WWI Centenary',
67 'description': 'md5:c2379ec0ca84072e86b446e536954546',
68 }
76b01870
AH
69 }, {
70 'url': 'https://www.abc.net.au/news/programs/the-world/2020-06-10/black-lives-matter-protests-spawn-support-for/12342074',
71 'info_dict': {
72 'id': '12342074',
73 'ext': 'mp4',
74 'title': 'Black Lives Matter protests spawn support for Papuans in Indonesia',
75 'description': 'md5:2961a17dc53abc558589ccd0fb8edd6f',
76 }
5630d797
AH
77 }, {
78 'url': 'https://www.abc.net.au/btn/newsbreak/btn-newsbreak-20200814/12560476',
79 'info_dict': {
80 'id': 'tDL8Ld4dK_8',
81 'ext': 'mp4',
82 'title': 'Fortnite Banned From Apple and Google App Stores',
83 'description': 'md5:a6df3f36ce8f816b74af4bd6462f5651',
84 'upload_date': '20200813',
85 'uploader': 'Behind the News',
86 'uploader_id': 'behindthenews',
87 }
17a64763 88 }]
64ce58db
JMF
89
90 def _real_extract(self, url):
ed9266db 91 video_id = self._match_id(url)
64ce58db
JMF
92 webpage = self._download_webpage(url, video_id)
93
76b01870
AH
94 mobj = re.search(r'<a\s+href="(?P<url>[^"]+)"\s+data-duration="\d+"\s+title="Download audio directly">', webpage)
95 if mobj:
96 urls_info = mobj.groupdict()
97 youtube = False
98 video = False
99 else:
100 mobj = re.search(r'<a href="(?P<url>http://www\.youtube\.com/watch\?v=[^"]+)"><span><strong>External Link:</strong>',
101 webpage)
5630d797
AH
102 if mobj is None:
103 mobj = re.search(r'<iframe width="100%" src="(?P<url>//www\.youtube-nocookie\.com/embed/[^?"]+)', webpage)
76b01870
AH
104 if mobj:
105 urls_info = mobj.groupdict()
106 youtube = True
107 video = True
17a64763 108
76b01870
AH
109 if mobj is None:
110 mobj = re.search(r'(?P<type>)"sources": (?P<json_data>\[[^\]]+\]),', webpage)
111 if mobj is None:
112 mobj = re.search(
113 r'inline(?P<type>Video|Audio|YouTube)Data\.push\((?P<json_data>[^)]+)\);',
114 webpage)
115 if mobj is None:
116 expired = self._html_search_regex(r'(?s)class="expired-(?:video|audio)".+?<span>(.+?)</span>', webpage, 'expired', None)
117 if expired:
118 raise ExtractorError('%s said: %s' % (self.IE_NAME, expired), expected=True)
119 raise ExtractorError('Unable to extract video urls')
120
121 urls_info = self._parse_json(
122 mobj.group('json_data'), video_id, transform_source=js_to_json)
123 youtube = mobj.group('type') == 'YouTube'
124 video = mobj.group('type') == 'Video' or urls_info[0]['contentType'] == 'video/mp4'
17a64763
YCH
125
126 if not isinstance(urls_info, list):
127 urls_info = [urls_info]
128
76b01870 129 if youtube:
17a64763
YCH
130 return self.playlist_result([
131 self.url_result(url_info['url']) for url_info in urls_info])
132
76b01870
AH
133 formats = []
134 for url_info in urls_info:
135 height = int_or_none(url_info.get('height'))
136 bitrate = int_or_none(url_info.get('bitrate'))
137 width = int_or_none(url_info.get('width'))
138 format_id = None
139 mobj = re.search(r'_(?:(?P<height>\d+)|(?P<bitrate>\d+)k)\.mp4$', url_info['url'])
140 if mobj:
141 height_from_url = mobj.group('height')
142 if height_from_url:
143 height = height or int_or_none(height_from_url)
144 width = width or int_or_none(url_info.get('label'))
145 else:
146 bitrate = bitrate or int_or_none(mobj.group('bitrate'))
147 format_id = str_or_none(url_info.get('label'))
148 formats.append({
149 'url': url_info['url'],
150 'vcodec': url_info.get('codec') if video else 'none',
151 'width': width,
152 'height': height,
153 'tbr': bitrate,
154 'filesize': int_or_none(url_info.get('filesize')),
155 'format_id': format_id
156 })
7687b354 157
64ce58db
JMF
158 return {
159 'id': video_id,
160 'title': self._og_search_title(webpage),
161 'formats': formats,
162 'description': self._og_search_description(webpage),
163 'thumbnail': self._og_search_thumbnail(webpage),
164 }
55d119e2
RA
165
166
167class ABCIViewIE(InfoExtractor):
168 IE_NAME = 'abc.net.au:iview'
e0671819 169 _VALID_URL = r'https?://iview\.abc\.net\.au/(?:[^/]+/)*video/(?P<id>[^/?#]+)'
77341dae 170 _GEO_COUNTRIES = ['AU']
55d119e2 171
bfcda07a 172 # ABC iview programs are normally available for 14 days only.
55d119e2 173 _TESTS = [{
d6aa1db7 174 'url': 'https://iview.abc.net.au/show/gruen/series/11/video/LE1927H001S00',
175 'md5': '67715ce3c78426b11ba167d875ac6abf',
55d119e2 176 'info_dict': {
d6aa1db7 177 'id': 'LE1927H001S00',
55d119e2 178 'ext': 'mp4',
d6aa1db7 179 'title': "Series 11 Ep 1",
180 'series': "Gruen",
181 'description': 'md5:52cc744ad35045baf6aded2ce7287f67',
182 'upload_date': '20190925',
183 'uploader_id': 'abc1',
184 'timestamp': 1569445289,
77341dae
S
185 },
186 'params': {
187 'skip_download': True,
55d119e2
RA
188 },
189 }]
190
191 def _real_extract(self, url):
192 video_id = self._match_id(url)
e0671819
RA
193 video_params = self._download_json(
194 'https://iview.abc.net.au/api/programs/' + video_id, video_id)
195 title = unescapeHTML(video_params.get('title') or video_params['seriesTitle'])
196 stream = next(s for s in video_params['playlist'] if s.get('type') in ('program', 'livestream'))
55d119e2 197
e0671819
RA
198 house_number = video_params.get('episodeHouseNumber') or video_id
199 path = '/auth/hls/sign?ts={0}&hn={1}&d=android-tablet'.format(
77341dae
S
200 int(time.time()), house_number)
201 sig = hmac.new(
e0671819 202 b'android.content.res.Resources',
77341dae
S
203 path.encode('utf-8'), hashlib.sha256).hexdigest()
204 token = self._download_webpage(
205 'http://iview.abc.net.au{0}&sig={1}'.format(path, sig), video_id)
2e65e7db 206
207 def tokenize_url(url, token):
77341dae
S
208 return update_url_query(url, {
209 'hdnea': token,
210 })
211
c15c316b 212 for sd in ('1080', '720', 'sd', 'sd-low'):
77341dae
S
213 sd_url = try_get(
214 stream, lambda x: x['streams']['hls'][sd], compat_str)
215 if not sd_url:
216 continue
217 formats = self._extract_m3u8_formats(
218 tokenize_url(sd_url, token), video_id, 'mp4',
219 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
220 if formats:
221 break
55d119e2
RA
222
223 subtitles = {}
224 src_vtt = stream.get('captions', {}).get('src-vtt')
225 if src_vtt:
226 subtitles['en'] = [{
227 'url': src_vtt,
228 'ext': 'vtt',
229 }]
230
e0671819 231 is_live = video_params.get('livestream') == '1'
e0671819 232
55d119e2
RA
233 return {
234 'id': video_id,
e0671819
RA
235 'title': title,
236 'description': video_params.get('description'),
237 'thumbnail': video_params.get('thumbnail'),
55d119e2
RA
238 'duration': int_or_none(video_params.get('eventDuration')),
239 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
9e6a4180 240 'series': unescapeHTML(video_params.get('seriesTitle')),
55d119e2 241 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
9aca7fe6
S
242 'season_number': int_or_none(self._search_regex(
243 r'\bSeries\s+(\d+)\b', title, 'season number', default=None)),
244 'episode_number': int_or_none(self._search_regex(
245 r'\bEp\s+(\d+)\b', title, 'episode number', default=None)),
246 'episode_id': house_number,
55d119e2
RA
247 'uploader_id': video_params.get('channel'),
248 'formats': formats,
249 'subtitles': subtitles,
e0671819 250 'is_live': is_live,
55d119e2 251 }
6839d02c
PW
252
253
254class ABCIViewShowSeriesIE(InfoExtractor):
255 IE_NAME = 'abc.net.au:iview:showseries'
256 _VALID_URL = r'https?://iview\.abc\.net\.au/show/(?P<id>[^/]+)(?:/series/\d+)?$'
257 _GEO_COUNTRIES = ['AU']
258
259 _TESTS = [{
260 'url': 'https://iview.abc.net.au/show/upper-middle-bogan',
261 'info_dict': {
262 'id': '124870-1',
263 'title': 'Series 1',
264 'description': 'md5:93119346c24a7c322d446d8eece430ff',
265 'series': 'Upper Middle Bogan',
266 'season': 'Series 1',
267 'thumbnail': r're:^https?://cdn\.iview\.abc\.net\.au/thumbs/.*\.jpg$'
268 },
269 'playlist_count': 8,
270 }, {
271 'url': 'https://iview.abc.net.au/show/upper-middle-bogan',
272 'info_dict': {
273 'id': 'CO1108V001S00',
274 'ext': 'mp4',
275 'title': 'Series 1 Ep 1 I\'m A Swan',
276 'description': 'md5:7b676758c1de11a30b79b4d301e8da93',
277 'series': 'Upper Middle Bogan',
278 'uploader_id': 'abc1',
279 'upload_date': '20210630',
280 'timestamp': 1625036400,
281 },
282 'params': {
283 'noplaylist': True,
284 'skip_download': 'm3u8',
285 },
286 }]
287
288 def _real_extract(self, url):
289 show_id = self._match_id(url)
290 webpage = self._download_webpage(url, show_id)
291 webpage_data = self._search_regex(
292 r'window\.__INITIAL_STATE__\s*=\s*[\'"](.+?)[\'"]\s*;',
293 webpage, 'initial state')
294 video_data = self._parse_json(
295 unescapeHTML(webpage_data).encode('utf-8').decode('unicode_escape'), show_id)
296 video_data = video_data['route']['pageData']['_embedded']
297
88f23a18 298 highlight = try_get(video_data, lambda x: x['highlightVideo']['shareUrl'])
f40ee5e9 299 if not self._yes_playlist(show_id, bool(highlight), video_label='highlight video'):
300 return self.url_result(highlight, ie=ABCIViewIE.ie_key())
6839d02c 301
6839d02c
PW
302 series = video_data['selectedSeries']
303 return {
304 '_type': 'playlist',
305 'entries': [self.url_result(episode['shareUrl'])
306 for episode in series['_embedded']['videoEpisodes']],
307 'id': series.get('id'),
308 'title': dict_get(series, ('title', 'displaySubtitle')),
309 'description': series.get('description'),
310 'series': dict_get(series, ('showTitle', 'displayTitle')),
311 'season': dict_get(series, ('title', 'displaySubtitle')),
312 'thumbnail': series.get('thumbnail'),
313 }