]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/abc.py
[ABC] Fix extraction
[yt-dlp.git] / youtube_dl / 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'
92519402 24 _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/news/(?:[^/]+/){1,2}(?P<id>\d+)'
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,
76b01870
AH
62 }, {
63 'url': 'https://www.abc.net.au/news/programs/the-world/2020-06-10/black-lives-matter-protests-spawn-support-for/12342074',
64 'info_dict': {
65 'id': '12342074',
66 'ext': 'mp4',
67 'title': 'Black Lives Matter protests spawn support for Papuans in Indonesia',
68 'description': 'md5:2961a17dc53abc558589ccd0fb8edd6f',
69 }
17a64763 70 }]
64ce58db
JMF
71
72 def _real_extract(self, url):
ed9266db 73 video_id = self._match_id(url)
64ce58db
JMF
74 webpage = self._download_webpage(url, video_id)
75
76b01870
AH
76 mobj = re.search(r'<a\s+href="(?P<url>[^"]+)"\s+data-duration="\d+"\s+title="Download audio directly">', webpage)
77 if mobj:
78 urls_info = mobj.groupdict()
79 youtube = False
80 video = False
81 else:
82 mobj = re.search(r'<a href="(?P<url>http://www\.youtube\.com/watch\?v=[^"]+)"><span><strong>External Link:</strong>',
83 webpage)
84 if mobj:
85 urls_info = mobj.groupdict()
86 youtube = True
87 video = True
17a64763 88
76b01870
AH
89 if mobj is None:
90 mobj = re.search(r'(?P<type>)"sources": (?P<json_data>\[[^\]]+\]),', webpage)
91 if mobj is None:
92 mobj = re.search(
93 r'inline(?P<type>Video|Audio|YouTube)Data\.push\((?P<json_data>[^)]+)\);',
94 webpage)
95 if mobj is None:
96 expired = self._html_search_regex(r'(?s)class="expired-(?:video|audio)".+?<span>(.+?)</span>', webpage, 'expired', None)
97 if expired:
98 raise ExtractorError('%s said: %s' % (self.IE_NAME, expired), expected=True)
99 raise ExtractorError('Unable to extract video urls')
100
101 urls_info = self._parse_json(
102 mobj.group('json_data'), video_id, transform_source=js_to_json)
103 youtube = mobj.group('type') == 'YouTube'
104 video = mobj.group('type') == 'Video' or urls_info[0]['contentType'] == 'video/mp4'
17a64763
YCH
105
106 if not isinstance(urls_info, list):
107 urls_info = [urls_info]
108
76b01870 109 if youtube:
17a64763
YCH
110 return self.playlist_result([
111 self.url_result(url_info['url']) for url_info in urls_info])
112
76b01870
AH
113 formats = []
114 for url_info in urls_info:
115 height = int_or_none(url_info.get('height'))
116 bitrate = int_or_none(url_info.get('bitrate'))
117 width = int_or_none(url_info.get('width'))
118 format_id = None
119 mobj = re.search(r'_(?:(?P<height>\d+)|(?P<bitrate>\d+)k)\.mp4$', url_info['url'])
120 if mobj:
121 height_from_url = mobj.group('height')
122 if height_from_url:
123 height = height or int_or_none(height_from_url)
124 width = width or int_or_none(url_info.get('label'))
125 else:
126 bitrate = bitrate or int_or_none(mobj.group('bitrate'))
127 format_id = str_or_none(url_info.get('label'))
128 formats.append({
129 'url': url_info['url'],
130 'vcodec': url_info.get('codec') if video else 'none',
131 'width': width,
132 'height': height,
133 'tbr': bitrate,
134 'filesize': int_or_none(url_info.get('filesize')),
135 'format_id': format_id
136 })
7687b354 137
64ce58db
JMF
138 self._sort_formats(formats)
139
140 return {
141 'id': video_id,
142 'title': self._og_search_title(webpage),
143 'formats': formats,
144 'description': self._og_search_description(webpage),
145 'thumbnail': self._og_search_thumbnail(webpage),
146 }
55d119e2
RA
147
148
149class ABCIViewIE(InfoExtractor):
150 IE_NAME = 'abc.net.au:iview'
e0671819 151 _VALID_URL = r'https?://iview\.abc\.net\.au/(?:[^/]+/)*video/(?P<id>[^/?#]+)'
77341dae 152 _GEO_COUNTRIES = ['AU']
55d119e2 153
bfcda07a 154 # ABC iview programs are normally available for 14 days only.
55d119e2 155 _TESTS = [{
d6aa1db7 156 'url': 'https://iview.abc.net.au/show/gruen/series/11/video/LE1927H001S00',
157 'md5': '67715ce3c78426b11ba167d875ac6abf',
55d119e2 158 'info_dict': {
d6aa1db7 159 'id': 'LE1927H001S00',
55d119e2 160 'ext': 'mp4',
d6aa1db7 161 'title': "Series 11 Ep 1",
162 'series': "Gruen",
163 'description': 'md5:52cc744ad35045baf6aded2ce7287f67',
164 'upload_date': '20190925',
165 'uploader_id': 'abc1',
166 'timestamp': 1569445289,
77341dae
S
167 },
168 'params': {
169 'skip_download': True,
55d119e2
RA
170 },
171 }]
172
173 def _real_extract(self, url):
174 video_id = self._match_id(url)
e0671819
RA
175 video_params = self._download_json(
176 'https://iview.abc.net.au/api/programs/' + video_id, video_id)
177 title = unescapeHTML(video_params.get('title') or video_params['seriesTitle'])
178 stream = next(s for s in video_params['playlist'] if s.get('type') in ('program', 'livestream'))
55d119e2 179
e0671819
RA
180 house_number = video_params.get('episodeHouseNumber') or video_id
181 path = '/auth/hls/sign?ts={0}&hn={1}&d=android-tablet'.format(
77341dae
S
182 int(time.time()), house_number)
183 sig = hmac.new(
e0671819 184 b'android.content.res.Resources',
77341dae
S
185 path.encode('utf-8'), hashlib.sha256).hexdigest()
186 token = self._download_webpage(
187 'http://iview.abc.net.au{0}&sig={1}'.format(path, sig), video_id)
2e65e7db 188
189 def tokenize_url(url, token):
77341dae
S
190 return update_url_query(url, {
191 'hdnea': token,
192 })
193
d6aa1db7 194 for sd in ('720', 'sd', 'sd-low'):
77341dae
S
195 sd_url = try_get(
196 stream, lambda x: x['streams']['hls'][sd], compat_str)
197 if not sd_url:
198 continue
199 formats = self._extract_m3u8_formats(
200 tokenize_url(sd_url, token), video_id, 'mp4',
201 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
202 if formats:
203 break
55d119e2
RA
204 self._sort_formats(formats)
205
206 subtitles = {}
207 src_vtt = stream.get('captions', {}).get('src-vtt')
208 if src_vtt:
209 subtitles['en'] = [{
210 'url': src_vtt,
211 'ext': 'vtt',
212 }]
213
e0671819
RA
214 is_live = video_params.get('livestream') == '1'
215 if is_live:
216 title = self._live_title(title)
217
55d119e2
RA
218 return {
219 'id': video_id,
e0671819
RA
220 'title': title,
221 'description': video_params.get('description'),
222 'thumbnail': video_params.get('thumbnail'),
55d119e2
RA
223 'duration': int_or_none(video_params.get('eventDuration')),
224 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
9e6a4180 225 'series': unescapeHTML(video_params.get('seriesTitle')),
55d119e2 226 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
9aca7fe6
S
227 'season_number': int_or_none(self._search_regex(
228 r'\bSeries\s+(\d+)\b', title, 'season number', default=None)),
229 'episode_number': int_or_none(self._search_regex(
230 r'\bEp\s+(\d+)\b', title, 'episode number', default=None)),
231 'episode_id': house_number,
55d119e2
RA
232 'uploader_id': video_params.get('channel'),
233 'formats': formats,
234 'subtitles': subtitles,
e0671819 235 'is_live': is_live,
55d119e2 236 }