]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/abc.py
[spotify] Detect iframe embeds (#3430)
[yt-dlp.git] / yt_dlp / extractor / abc.py
1 import hashlib
2 import hmac
3 import re
4 import time
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9 dict_get,
10 ExtractorError,
11 js_to_json,
12 int_or_none,
13 parse_iso8601,
14 str_or_none,
15 try_get,
16 unescapeHTML,
17 update_url_query,
18 )
19
20
21 class ABCIE(InfoExtractor):
22 IE_NAME = 'abc.net.au'
23 _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/(?:news|btn)/(?:[^/]+/){1,4}(?P<id>\d{5,})'
24
25 _TESTS = [{
26 'url': 'http://www.abc.net.au/news/2014-11-05/australia-to-staff-ebola-treatment-centre-in-sierra-leone/5868334',
27 'md5': 'cb3dd03b18455a661071ee1e28344d9f',
28 'info_dict': {
29 'id': '5868334',
30 'ext': 'mp4',
31 'title': 'Australia to help staff Ebola treatment centre in Sierra Leone',
32 'description': 'md5:809ad29c67a05f54eb41f2a105693a67',
33 },
34 'skip': 'this video has expired',
35 }, {
36 'url': 'http://www.abc.net.au/news/2015-08-17/warren-entsch-introduces-same-sex-marriage-bill/6702326',
37 'md5': '4ebd61bdc82d9a8b722f64f1f4b4d121',
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'],
48 'skip': 'Not accessible from Travis CI server',
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 },
58 }, {
59 'url': 'http://www.abc.net.au/news/2015-10-19/6866214',
60 'only_matching': True,
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 }
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 }
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 }
88 }]
89
90 def _real_extract(self, url):
91 video_id = self._match_id(url)
92 webpage = self._download_webpage(url, video_id)
93
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)
102 if mobj is None:
103 mobj = re.search(r'<iframe width="100%" src="(?P<url>//www\.youtube-nocookie\.com/embed/[^?"]+)', webpage)
104 if mobj:
105 urls_info = mobj.groupdict()
106 youtube = True
107 video = True
108
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'
125
126 if not isinstance(urls_info, list):
127 urls_info = [urls_info]
128
129 if youtube:
130 return self.playlist_result([
131 self.url_result(url_info['url']) for url_info in urls_info])
132
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 })
157
158 self._sort_formats(formats)
159
160 return {
161 'id': video_id,
162 'title': self._og_search_title(webpage),
163 'formats': formats,
164 'description': self._og_search_description(webpage),
165 'thumbnail': self._og_search_thumbnail(webpage),
166 }
167
168
169 class ABCIViewIE(InfoExtractor):
170 IE_NAME = 'abc.net.au:iview'
171 _VALID_URL = r'https?://iview\.abc\.net\.au/(?:[^/]+/)*video/(?P<id>[^/?#]+)'
172 _GEO_COUNTRIES = ['AU']
173
174 # ABC iview programs are normally available for 14 days only.
175 _TESTS = [{
176 'url': 'https://iview.abc.net.au/show/gruen/series/11/video/LE1927H001S00',
177 'md5': '67715ce3c78426b11ba167d875ac6abf',
178 'info_dict': {
179 'id': 'LE1927H001S00',
180 'ext': 'mp4',
181 'title': "Series 11 Ep 1",
182 'series': "Gruen",
183 'description': 'md5:52cc744ad35045baf6aded2ce7287f67',
184 'upload_date': '20190925',
185 'uploader_id': 'abc1',
186 'timestamp': 1569445289,
187 },
188 'params': {
189 'skip_download': True,
190 },
191 }]
192
193 def _real_extract(self, url):
194 video_id = self._match_id(url)
195 video_params = self._download_json(
196 'https://iview.abc.net.au/api/programs/' + video_id, video_id)
197 title = unescapeHTML(video_params.get('title') or video_params['seriesTitle'])
198 stream = next(s for s in video_params['playlist'] if s.get('type') in ('program', 'livestream'))
199
200 house_number = video_params.get('episodeHouseNumber') or video_id
201 path = '/auth/hls/sign?ts={0}&hn={1}&d=android-tablet'.format(
202 int(time.time()), house_number)
203 sig = hmac.new(
204 b'android.content.res.Resources',
205 path.encode('utf-8'), hashlib.sha256).hexdigest()
206 token = self._download_webpage(
207 'http://iview.abc.net.au{0}&sig={1}'.format(path, sig), video_id)
208
209 def tokenize_url(url, token):
210 return update_url_query(url, {
211 'hdnea': token,
212 })
213
214 for sd in ('1080', '720', 'sd', 'sd-low'):
215 sd_url = try_get(
216 stream, lambda x: x['streams']['hls'][sd], compat_str)
217 if not sd_url:
218 continue
219 formats = self._extract_m3u8_formats(
220 tokenize_url(sd_url, token), video_id, 'mp4',
221 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
222 if formats:
223 break
224 self._sort_formats(formats)
225
226 subtitles = {}
227 src_vtt = stream.get('captions', {}).get('src-vtt')
228 if src_vtt:
229 subtitles['en'] = [{
230 'url': src_vtt,
231 'ext': 'vtt',
232 }]
233
234 is_live = video_params.get('livestream') == '1'
235
236 return {
237 'id': video_id,
238 'title': title,
239 'description': video_params.get('description'),
240 'thumbnail': video_params.get('thumbnail'),
241 'duration': int_or_none(video_params.get('eventDuration')),
242 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
243 'series': unescapeHTML(video_params.get('seriesTitle')),
244 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
245 'season_number': int_or_none(self._search_regex(
246 r'\bSeries\s+(\d+)\b', title, 'season number', default=None)),
247 'episode_number': int_or_none(self._search_regex(
248 r'\bEp\s+(\d+)\b', title, 'episode number', default=None)),
249 'episode_id': house_number,
250 'uploader_id': video_params.get('channel'),
251 'formats': formats,
252 'subtitles': subtitles,
253 'is_live': is_live,
254 }
255
256
257 class ABCIViewShowSeriesIE(InfoExtractor):
258 IE_NAME = 'abc.net.au:iview:showseries'
259 _VALID_URL = r'https?://iview\.abc\.net\.au/show/(?P<id>[^/]+)(?:/series/\d+)?$'
260 _GEO_COUNTRIES = ['AU']
261
262 _TESTS = [{
263 'url': 'https://iview.abc.net.au/show/upper-middle-bogan',
264 'info_dict': {
265 'id': '124870-1',
266 'title': 'Series 1',
267 'description': 'md5:93119346c24a7c322d446d8eece430ff',
268 'series': 'Upper Middle Bogan',
269 'season': 'Series 1',
270 'thumbnail': r're:^https?://cdn\.iview\.abc\.net\.au/thumbs/.*\.jpg$'
271 },
272 'playlist_count': 8,
273 }, {
274 'url': 'https://iview.abc.net.au/show/upper-middle-bogan',
275 'info_dict': {
276 'id': 'CO1108V001S00',
277 'ext': 'mp4',
278 'title': 'Series 1 Ep 1 I\'m A Swan',
279 'description': 'md5:7b676758c1de11a30b79b4d301e8da93',
280 'series': 'Upper Middle Bogan',
281 'uploader_id': 'abc1',
282 'upload_date': '20210630',
283 'timestamp': 1625036400,
284 },
285 'params': {
286 'noplaylist': True,
287 'skip_download': 'm3u8',
288 },
289 }]
290
291 def _real_extract(self, url):
292 show_id = self._match_id(url)
293 webpage = self._download_webpage(url, show_id)
294 webpage_data = self._search_regex(
295 r'window\.__INITIAL_STATE__\s*=\s*[\'"](.+?)[\'"]\s*;',
296 webpage, 'initial state')
297 video_data = self._parse_json(
298 unescapeHTML(webpage_data).encode('utf-8').decode('unicode_escape'), show_id)
299 video_data = video_data['route']['pageData']['_embedded']
300
301 highlight = try_get(video_data, lambda x: x['highlightVideo']['shareUrl'])
302 if not self._yes_playlist(show_id, bool(highlight), video_label='highlight video'):
303 return self.url_result(highlight, ie=ABCIViewIE.ie_key())
304
305 series = video_data['selectedSeries']
306 return {
307 '_type': 'playlist',
308 'entries': [self.url_result(episode['shareUrl'])
309 for episode in series['_embedded']['videoEpisodes']],
310 'id': series.get('id'),
311 'title': dict_get(series, ('title', 'displaySubtitle')),
312 'description': series.get('description'),
313 'series': dict_get(series, ('showTitle', 'displayTitle')),
314 'season': dict_get(series, ('title', 'displaySubtitle')),
315 'thumbnail': series.get('thumbnail'),
316 }