]> jfr.im git - yt-dlp.git/blame - youtube_dlc/extractor/abc.py
[skip travis] renaming
[yt-dlp.git] / youtube_dlc / 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,
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'
92519402 23 _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/news/(?:[^/]+/){1,2}(?P<id>\d+)'
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',
37 'md5': 'db2a5369238b51f9811ad815b69dc086',
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,
17a64763 61 }]
64ce58db
JMF
62
63 def _real_extract(self, url):
ed9266db 64 video_id = self._match_id(url)
64ce58db
JMF
65 webpage = self._download_webpage(url, video_id)
66
17a64763 67 mobj = re.search(
7687b354 68 r'inline(?P<type>Video|Audio|YouTube)Data\.push\((?P<json_data>[^)]+)\);',
17a64763
YCH
69 webpage)
70 if mobj is None:
5c5a3ecf 71 expired = self._html_search_regex(r'(?s)class="expired-(?:video|audio)".+?<span>(.+?)</span>', webpage, 'expired', None)
72 if expired:
73 raise ExtractorError('%s said: %s' % (self.IE_NAME, expired), expected=True)
17a64763
YCH
74 raise ExtractorError('Unable to extract video urls')
75
76 urls_info = self._parse_json(
77 mobj.group('json_data'), video_id, transform_source=js_to_json)
78
79 if not isinstance(urls_info, list):
80 urls_info = [urls_info]
81
82 if mobj.group('type') == 'YouTube':
83 return self.playlist_result([
84 self.url_result(url_info['url']) for url_info in urls_info])
85
64ce58db
JMF
86 formats = [{
87 'url': url_info['url'],
7687b354 88 'vcodec': url_info.get('codec') if mobj.group('type') == 'Video' else 'none',
c0a65687
YCH
89 'width': int_or_none(url_info.get('width')),
90 'height': int_or_none(url_info.get('height')),
91 'tbr': int_or_none(url_info.get('bitrate')),
92 'filesize': int_or_none(url_info.get('filesize')),
64ce58db 93 } for url_info in urls_info]
7687b354 94
64ce58db
JMF
95 self._sort_formats(formats)
96
97 return {
98 'id': video_id,
99 'title': self._og_search_title(webpage),
100 'formats': formats,
101 'description': self._og_search_description(webpage),
102 'thumbnail': self._og_search_thumbnail(webpage),
103 }
55d119e2
RA
104
105
106class ABCIViewIE(InfoExtractor):
107 IE_NAME = 'abc.net.au:iview'
e0671819 108 _VALID_URL = r'https?://iview\.abc\.net\.au/(?:[^/]+/)*video/(?P<id>[^/?#]+)'
77341dae 109 _GEO_COUNTRIES = ['AU']
55d119e2 110
bfcda07a 111 # ABC iview programs are normally available for 14 days only.
55d119e2 112 _TESTS = [{
d6aa1db7 113 'url': 'https://iview.abc.net.au/show/gruen/series/11/video/LE1927H001S00',
114 'md5': '67715ce3c78426b11ba167d875ac6abf',
55d119e2 115 'info_dict': {
d6aa1db7 116 'id': 'LE1927H001S00',
55d119e2 117 'ext': 'mp4',
d6aa1db7 118 'title': "Series 11 Ep 1",
119 'series': "Gruen",
120 'description': 'md5:52cc744ad35045baf6aded2ce7287f67',
121 'upload_date': '20190925',
122 'uploader_id': 'abc1',
123 'timestamp': 1569445289,
77341dae
S
124 },
125 'params': {
126 'skip_download': True,
55d119e2
RA
127 },
128 }]
129
130 def _real_extract(self, url):
131 video_id = self._match_id(url)
e0671819
RA
132 video_params = self._download_json(
133 'https://iview.abc.net.au/api/programs/' + video_id, video_id)
134 title = unescapeHTML(video_params.get('title') or video_params['seriesTitle'])
135 stream = next(s for s in video_params['playlist'] if s.get('type') in ('program', 'livestream'))
55d119e2 136
e0671819
RA
137 house_number = video_params.get('episodeHouseNumber') or video_id
138 path = '/auth/hls/sign?ts={0}&hn={1}&d=android-tablet'.format(
77341dae
S
139 int(time.time()), house_number)
140 sig = hmac.new(
e0671819 141 b'android.content.res.Resources',
77341dae
S
142 path.encode('utf-8'), hashlib.sha256).hexdigest()
143 token = self._download_webpage(
144 'http://iview.abc.net.au{0}&sig={1}'.format(path, sig), video_id)
2e65e7db 145
146 def tokenize_url(url, token):
77341dae
S
147 return update_url_query(url, {
148 'hdnea': token,
149 })
150
d6aa1db7 151 for sd in ('720', 'sd', 'sd-low'):
77341dae
S
152 sd_url = try_get(
153 stream, lambda x: x['streams']['hls'][sd], compat_str)
154 if not sd_url:
155 continue
156 formats = self._extract_m3u8_formats(
157 tokenize_url(sd_url, token), video_id, 'mp4',
158 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
159 if formats:
160 break
55d119e2
RA
161 self._sort_formats(formats)
162
163 subtitles = {}
164 src_vtt = stream.get('captions', {}).get('src-vtt')
165 if src_vtt:
166 subtitles['en'] = [{
167 'url': src_vtt,
168 'ext': 'vtt',
169 }]
170
e0671819
RA
171 is_live = video_params.get('livestream') == '1'
172 if is_live:
173 title = self._live_title(title)
174
55d119e2
RA
175 return {
176 'id': video_id,
e0671819
RA
177 'title': title,
178 'description': video_params.get('description'),
179 'thumbnail': video_params.get('thumbnail'),
55d119e2
RA
180 'duration': int_or_none(video_params.get('eventDuration')),
181 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
9e6a4180 182 'series': unescapeHTML(video_params.get('seriesTitle')),
55d119e2 183 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
9aca7fe6
S
184 'season_number': int_or_none(self._search_regex(
185 r'\bSeries\s+(\d+)\b', title, 'season number', default=None)),
186 'episode_number': int_or_none(self._search_regex(
187 r'\bEp\s+(\d+)\b', title, 'episode number', default=None)),
188 'episode_id': house_number,
55d119e2
RA
189 'uploader_id': video_params.get('channel'),
190 'formats': formats,
191 'subtitles': subtitles,
e0671819 192 'is_live': is_live,
55d119e2 193 }