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