]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/abc.py
New upstream version 2017.12.31
[youtubedl] / youtube_dl / extractor / abc.py
1 from __future__ import unicode_literals
2
3 import hashlib
4 import hmac
5 import re
6 import time
7
8 from .common import InfoExtractor
9 from ..compat import compat_str
10 from ..utils import (
11 ExtractorError,
12 js_to_json,
13 int_or_none,
14 parse_iso8601,
15 try_get,
16 update_url_query,
17 )
18
19
20 class ABCIE(InfoExtractor):
21 IE_NAME = 'abc.net.au'
22 _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/news/(?:[^/]+/){1,2}(?P<id>\d+)'
23
24 _TESTS = [{
25 'url': 'http://www.abc.net.au/news/2014-11-05/australia-to-staff-ebola-treatment-centre-in-sierra-leone/5868334',
26 'md5': 'cb3dd03b18455a661071ee1e28344d9f',
27 'info_dict': {
28 'id': '5868334',
29 'ext': 'mp4',
30 'title': 'Australia to help staff Ebola treatment centre in Sierra Leone',
31 'description': 'md5:809ad29c67a05f54eb41f2a105693a67',
32 },
33 'skip': 'this video has expired',
34 }, {
35 'url': 'http://www.abc.net.au/news/2015-08-17/warren-entsch-introduces-same-sex-marriage-bill/6702326',
36 'md5': 'db2a5369238b51f9811ad815b69dc086',
37 'info_dict': {
38 'id': 'NvqvPeNZsHU',
39 'ext': 'mp4',
40 'upload_date': '20150816',
41 'uploader': 'ABC News (Australia)',
42 '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',
43 'uploader_id': 'NewsOnABC',
44 'title': 'Marriage Equality: Warren Entsch introduces same sex marriage bill',
45 },
46 'add_ie': ['Youtube'],
47 'skip': 'Not accessible from Travis CI server',
48 }, {
49 'url': 'http://www.abc.net.au/news/2015-10-23/nab-lifts-interest-rates-following-westpac-and-cba/6880080',
50 'md5': 'b96eee7c9edf4fc5a358a0252881cc1f',
51 'info_dict': {
52 'id': '6880080',
53 'ext': 'mp3',
54 'title': 'NAB lifts interest rates, following Westpac and CBA',
55 'description': 'md5:f13d8edc81e462fce4a0437c7dc04728',
56 },
57 }, {
58 'url': 'http://www.abc.net.au/news/2015-10-19/6866214',
59 'only_matching': True,
60 }]
61
62 def _real_extract(self, url):
63 video_id = self._match_id(url)
64 webpage = self._download_webpage(url, video_id)
65
66 mobj = re.search(
67 r'inline(?P<type>Video|Audio|YouTube)Data\.push\((?P<json_data>[^)]+)\);',
68 webpage)
69 if mobj is None:
70 expired = self._html_search_regex(r'(?s)class="expired-(?:video|audio)".+?<span>(.+?)</span>', webpage, 'expired', None)
71 if expired:
72 raise ExtractorError('%s said: %s' % (self.IE_NAME, expired), expected=True)
73 raise ExtractorError('Unable to extract video urls')
74
75 urls_info = self._parse_json(
76 mobj.group('json_data'), video_id, transform_source=js_to_json)
77
78 if not isinstance(urls_info, list):
79 urls_info = [urls_info]
80
81 if mobj.group('type') == 'YouTube':
82 return self.playlist_result([
83 self.url_result(url_info['url']) for url_info in urls_info])
84
85 formats = [{
86 'url': url_info['url'],
87 'vcodec': url_info.get('codec') if mobj.group('type') == 'Video' else 'none',
88 'width': int_or_none(url_info.get('width')),
89 'height': int_or_none(url_info.get('height')),
90 'tbr': int_or_none(url_info.get('bitrate')),
91 'filesize': int_or_none(url_info.get('filesize')),
92 } for url_info in urls_info]
93
94 self._sort_formats(formats)
95
96 return {
97 'id': video_id,
98 'title': self._og_search_title(webpage),
99 'formats': formats,
100 'description': self._og_search_description(webpage),
101 'thumbnail': self._og_search_thumbnail(webpage),
102 }
103
104
105 class ABCIViewIE(InfoExtractor):
106 IE_NAME = 'abc.net.au:iview'
107 _VALID_URL = r'https?://iview\.abc\.net\.au/programs/[^/]+/(?P<id>[^/?#]+)'
108 _GEO_COUNTRIES = ['AU']
109
110 # ABC iview programs are normally available for 14 days only.
111 _TESTS = [{
112 'url': 'http://iview.abc.net.au/programs/call-the-midwife/ZW0898A003S00',
113 'md5': 'cde42d728b3b7c2b32b1b94b4a548afc',
114 'info_dict': {
115 'id': 'ZW0898A003S00',
116 'ext': 'mp4',
117 'title': 'Series 5 Ep 3',
118 'description': 'md5:e0ef7d4f92055b86c4f33611f180ed79',
119 'upload_date': '20171228',
120 'uploader_id': 'abc1',
121 'timestamp': 1514499187,
122 },
123 'params': {
124 'skip_download': True,
125 },
126 }]
127
128 def _real_extract(self, url):
129 video_id = self._match_id(url)
130 webpage = self._download_webpage(url, video_id)
131 video_params = self._parse_json(self._search_regex(
132 r'videoParams\s*=\s*({.+?});', webpage, 'video params'), video_id)
133 title = video_params.get('title') or video_params['seriesTitle']
134 stream = next(s for s in video_params['playlist'] if s.get('type') == 'program')
135
136 house_number = video_params.get('episodeHouseNumber')
137 path = '/auth/hls/sign?ts={0}&hn={1}&d=android-mobile'.format(
138 int(time.time()), house_number)
139 sig = hmac.new(
140 'android.content.res.Resources'.encode('utf-8'),
141 path.encode('utf-8'), hashlib.sha256).hexdigest()
142 token = self._download_webpage(
143 'http://iview.abc.net.au{0}&sig={1}'.format(path, sig), video_id)
144
145 def tokenize_url(url, token):
146 return update_url_query(url, {
147 'hdnea': token,
148 })
149
150 for sd in ('sd', 'sd-low'):
151 sd_url = try_get(
152 stream, lambda x: x['streams']['hls'][sd], compat_str)
153 if not sd_url:
154 continue
155 formats = self._extract_m3u8_formats(
156 tokenize_url(sd_url, token), video_id, 'mp4',
157 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
158 if formats:
159 break
160 self._sort_formats(formats)
161
162 subtitles = {}
163 src_vtt = stream.get('captions', {}).get('src-vtt')
164 if src_vtt:
165 subtitles['en'] = [{
166 'url': src_vtt,
167 'ext': 'vtt',
168 }]
169
170 return {
171 'id': video_id,
172 'title': title,
173 'description': self._html_search_meta(['og:description', 'twitter:description'], webpage),
174 'thumbnail': self._html_search_meta(['og:image', 'twitter:image:src'], webpage),
175 'duration': int_or_none(video_params.get('eventDuration')),
176 'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
177 'series': video_params.get('seriesTitle'),
178 'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
179 'episode_number': int_or_none(self._html_search_meta('episodeNumber', webpage, default=None)),
180 'episode': self._html_search_meta('episode_title', webpage, default=None),
181 'uploader_id': video_params.get('channel'),
182 'formats': formats,
183 'subtitles': subtitles,
184 }