[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / corus.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .theplatform import ThePlatformFeedIE
7 from ..utils import (
8     dict_get,
9     ExtractorError,
10     float_or_none,
11     int_or_none,
12 )
13
14
15 class CorusIE(ThePlatformFeedIE):
16     _VALID_URL = r'''(?x)
17                     https?://
18                         (?:www\.)?
19                         (?P<domain>
20                             (?:
21                                 globaltv|
22                                 etcanada|
23                                 seriesplus|
24                                 wnetwork|
25                                 ytv
26                             )\.com|
27                             (?:
28                                 hgtv|
29                                 foodnetwork|
30                                 slice|
31                                 history|
32                                 showcase|
33                                 bigbrothercanada|
34                                 abcspark|
35                                 disney(?:channel|lachaine)
36                             )\.ca
37                         )
38                         /(?:[^/]+/)*
39                         (?:
40                             video\.html\?.*?\bv=|
41                             videos?/(?:[^/]+/)*(?:[a-z0-9-]+-)?
42                         )
43                         (?P<id>
44                             [\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}|
45                             (?:[A-Z]{4})?\d{12,20}
46                         )
47                     '''
48     _TESTS = [{
49         'url': 'http://www.hgtv.ca/shows/bryan-inc/videos/movie-night-popcorn-with-bryan-870923331648/',
50         'info_dict': {
51             'id': '870923331648',
52             'ext': 'mp4',
53             'title': 'Movie Night Popcorn with Bryan',
54             'description': 'Bryan whips up homemade popcorn, the old fashion way for Jojo and Lincoln.',
55             'upload_date': '20170206',
56             'timestamp': 1486392197,
57         },
58         'params': {
59             'format': 'bestvideo',
60             'skip_download': True,
61         },
62         'expected_warnings': ['Failed to parse JSON'],
63     }, {
64         'url': 'http://www.foodnetwork.ca/shows/chopped/video/episode/chocolate-obsession/video.html?v=872683587753',
65         'only_matching': True,
66     }, {
67         'url': 'http://etcanada.com/video/873675331955/meet-the-survivor-game-changers-castaways-part-2/',
68         'only_matching': True,
69     }, {
70         'url': 'http://www.history.ca/the-world-without-canada/video/full-episodes/natural-resources/video.html?v=955054659646#video',
71         'only_matching': True,
72     }, {
73         'url': 'http://www.showcase.ca/eyewitness/video/eyewitness++106/video.html?v=955070531919&p=1&s=da#video',
74         'only_matching': True,
75     }, {
76         'url': 'http://www.bigbrothercanada.ca/video/1457812035894/',
77         'only_matching': True
78     }, {
79         'url': 'https://www.bigbrothercanada.ca/video/big-brother-canada-704/1457812035894/',
80         'only_matching': True
81     }, {
82         'url': 'https://www.seriesplus.com/emissions/dre-mary-mort-sur-ordonnance/videos/deux-coeurs-battant/SERP0055626330000200/',
83         'only_matching': True
84     }, {
85         'url': 'https://www.disneychannel.ca/shows/gabby-duran-the-unsittables/video/crybaby-duran-clip/2f557eec-0588-11ea-ae2b-e2c6776b770e/',
86         'only_matching': True
87     }]
88     _GEO_BYPASS = False
89     _SITE_MAP = {
90         'globaltv': 'series',
91         'etcanada': 'series',
92         'foodnetwork': 'food',
93         'bigbrothercanada': 'series',
94         'disneychannel': 'disneyen',
95         'disneylachaine': 'disneyfr',
96     }
97
98     def _real_extract(self, url):
99         domain, video_id = re.match(self._VALID_URL, url).groups()
100         site = domain.split('.')[0]
101         path = self._SITE_MAP.get(site, site)
102         if path != 'series':
103             path = 'migration/' + path
104         video = self._download_json(
105             'https://globalcontent.corusappservices.com/templates/%s/playlist/' % path,
106             video_id, query={'byId': video_id},
107             headers={'Accept': 'application/json'})[0]
108         title = video['title']
109
110         formats = []
111         for source in video.get('sources', []):
112             smil_url = source.get('file')
113             if not smil_url:
114                 continue
115             source_type = source.get('type')
116             note = 'Downloading%s smil file' % (' ' + source_type if source_type else '')
117             resp = self._download_webpage(
118                 smil_url, video_id, note, fatal=False,
119                 headers=self.geo_verification_headers())
120             if not resp:
121                 continue
122             error = self._parse_json(resp, video_id, fatal=False)
123             if error:
124                 if error.get('exception') == 'GeoLocationBlocked':
125                     self.raise_geo_restricted(countries=['CA'])
126                 raise ExtractorError(error['description'])
127             smil = self._parse_xml(resp, video_id, fatal=False)
128             if smil is None:
129                 continue
130             namespace = self._parse_smil_namespace(smil)
131             formats.extend(self._parse_smil_formats(
132                 smil, smil_url, video_id, namespace))
133         if not formats and video.get('drm'):
134             raise ExtractorError('This video is DRM protected.', expected=True)
135         self._sort_formats(formats)
136
137         subtitles = {}
138         for track in video.get('tracks', []):
139             track_url = track.get('file')
140             if not track_url:
141                 continue
142             lang = 'fr' if site in ('disneylachaine', 'seriesplus') else 'en'
143             subtitles.setdefault(lang, []).append({'url': track_url})
144
145         metadata = video.get('metadata') or {}
146         get_number = lambda x: int_or_none(video.get('pl1$' + x) or metadata.get(x + 'Number'))
147
148         return {
149             'id': video_id,
150             'title': title,
151             'formats': formats,
152             'thumbnail': dict_get(video, ('defaultThumbnailUrl', 'thumbnail', 'image')),
153             'description': video.get('description'),
154             'timestamp': int_or_none(video.get('availableDate'), 1000),
155             'subtitles': subtitles,
156             'duration': float_or_none(metadata.get('duration')),
157             'series': dict_get(video, ('show', 'pl1$show')),
158             'season_number': get_number('season'),
159             'episode_number': get_number('episode'),
160         }