Merge pull request #14225 from Tithen-Firion/openload-phantomjs-method
[youtube-dl] / youtube_dl / extractor / abc.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8     ExtractorError,
9     js_to_json,
10     int_or_none,
11     parse_iso8601,
12     try_get,
13 )
14
15
16 class ABCIE(InfoExtractor):
17     IE_NAME = 'abc.net.au'
18     _VALID_URL = r'https?://(?:www\.)?abc\.net\.au/news/(?:[^/]+/){1,2}(?P<id>\d+)'
19
20     _TESTS = [{
21         'url': 'http://www.abc.net.au/news/2014-11-05/australia-to-staff-ebola-treatment-centre-in-sierra-leone/5868334',
22         'md5': 'cb3dd03b18455a661071ee1e28344d9f',
23         'info_dict': {
24             'id': '5868334',
25             'ext': 'mp4',
26             'title': 'Australia to help staff Ebola treatment centre in Sierra Leone',
27             'description': 'md5:809ad29c67a05f54eb41f2a105693a67',
28         },
29         'skip': 'this video has expired',
30     }, {
31         'url': 'http://www.abc.net.au/news/2015-08-17/warren-entsch-introduces-same-sex-marriage-bill/6702326',
32         'md5': 'db2a5369238b51f9811ad815b69dc086',
33         'info_dict': {
34             'id': 'NvqvPeNZsHU',
35             'ext': 'mp4',
36             'upload_date': '20150816',
37             'uploader': 'ABC News (Australia)',
38             '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',
39             'uploader_id': 'NewsOnABC',
40             'title': 'Marriage Equality: Warren Entsch introduces same sex marriage bill',
41         },
42         'add_ie': ['Youtube'],
43         'skip': 'Not accessible from Travis CI server',
44     }, {
45         'url': 'http://www.abc.net.au/news/2015-10-23/nab-lifts-interest-rates-following-westpac-and-cba/6880080',
46         'md5': 'b96eee7c9edf4fc5a358a0252881cc1f',
47         'info_dict': {
48             'id': '6880080',
49             'ext': 'mp3',
50             'title': 'NAB lifts interest rates, following Westpac and CBA',
51             'description': 'md5:f13d8edc81e462fce4a0437c7dc04728',
52         },
53     }, {
54         'url': 'http://www.abc.net.au/news/2015-10-19/6866214',
55         'only_matching': True,
56     }]
57
58     def _real_extract(self, url):
59         video_id = self._match_id(url)
60         webpage = self._download_webpage(url, video_id)
61
62         mobj = re.search(
63             r'inline(?P<type>Video|Audio|YouTube)Data\.push\((?P<json_data>[^)]+)\);',
64             webpage)
65         if mobj is None:
66             expired = self._html_search_regex(r'(?s)class="expired-(?:video|audio)".+?<span>(.+?)</span>', webpage, 'expired', None)
67             if expired:
68                 raise ExtractorError('%s said: %s' % (self.IE_NAME, expired), expected=True)
69             raise ExtractorError('Unable to extract video urls')
70
71         urls_info = self._parse_json(
72             mobj.group('json_data'), video_id, transform_source=js_to_json)
73
74         if not isinstance(urls_info, list):
75             urls_info = [urls_info]
76
77         if mobj.group('type') == 'YouTube':
78             return self.playlist_result([
79                 self.url_result(url_info['url']) for url_info in urls_info])
80
81         formats = [{
82             'url': url_info['url'],
83             'vcodec': url_info.get('codec') if mobj.group('type') == 'Video' else 'none',
84             'width': int_or_none(url_info.get('width')),
85             'height': int_or_none(url_info.get('height')),
86             'tbr': int_or_none(url_info.get('bitrate')),
87             'filesize': int_or_none(url_info.get('filesize')),
88         } for url_info in urls_info]
89
90         self._sort_formats(formats)
91
92         return {
93             'id': video_id,
94             'title': self._og_search_title(webpage),
95             'formats': formats,
96             'description': self._og_search_description(webpage),
97             'thumbnail': self._og_search_thumbnail(webpage),
98         }
99
100
101 class ABCIViewIE(InfoExtractor):
102     IE_NAME = 'abc.net.au:iview'
103     _VALID_URL = r'https?://iview\.abc\.net\.au/programs/[^/]+/(?P<id>[^/?#]+)'
104
105     # ABC iview programs are normally available for 14 days only.
106     _TESTS = [{
107         'url': 'http://iview.abc.net.au/programs/diaries-of-a-broken-mind/ZX9735A001S00',
108         'md5': 'cde42d728b3b7c2b32b1b94b4a548afc',
109         'info_dict': {
110             'id': 'ZX9735A001S00',
111             'ext': 'mp4',
112             'title': 'Diaries Of A Broken Mind',
113             'description': 'md5:7de3903874b7a1be279fe6b68718fc9e',
114             'upload_date': '20161010',
115             'uploader_id': 'abc2',
116             'timestamp': 1476064920,
117         },
118         'skip': 'Video gone',
119     }]
120
121     def _real_extract(self, url):
122         video_id = self._match_id(url)
123         webpage = self._download_webpage(url, video_id)
124         video_params = self._parse_json(self._search_regex(
125             r'videoParams\s*=\s*({.+?});', webpage, 'video params'), video_id)
126         title = video_params.get('title') or video_params['seriesTitle']
127         stream = next(s for s in video_params['playlist'] if s.get('type') == 'program')
128
129         format_urls = [
130             try_get(stream, lambda x: x['hds-unmetered'], compat_str)]
131
132         # May have higher quality video
133         sd_url = try_get(
134             stream, lambda x: x['streams']['hds']['sd'], compat_str)
135         if sd_url:
136             format_urls.append(sd_url.replace('metered', 'um'))
137
138         formats = []
139         for format_url in format_urls:
140             if format_url:
141                 formats.extend(
142                     self._extract_akamai_formats(format_url, video_id))
143         self._sort_formats(formats)
144
145         subtitles = {}
146         src_vtt = stream.get('captions', {}).get('src-vtt')
147         if src_vtt:
148             subtitles['en'] = [{
149                 'url': src_vtt,
150                 'ext': 'vtt',
151             }]
152
153         return {
154             'id': video_id,
155             'title': title,
156             'description': self._html_search_meta(['og:description', 'twitter:description'], webpage),
157             'thumbnail': self._html_search_meta(['og:image', 'twitter:image:src'], webpage),
158             'duration': int_or_none(video_params.get('eventDuration')),
159             'timestamp': parse_iso8601(video_params.get('pubDate'), ' '),
160             'series': video_params.get('seriesTitle'),
161             'series_id': video_params.get('seriesHouseNumber') or video_id[:7],
162             'episode_number': int_or_none(self._html_search_meta('episodeNumber', webpage, default=None)),
163             'episode': self._html_search_meta('episode_title', webpage, default=None),
164             'uploader_id': video_params.get('channel'),
165             'formats': formats,
166             'subtitles': subtitles,
167         }