Merge pull request #12861 from Tithen-Firion/cbsinteractive-fix
[youtube-dl] / youtube_dl / extractor / aenetworks.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .theplatform import ThePlatformIE
6 from ..utils import (
7     smuggle_url,
8     update_url_query,
9     unescapeHTML,
10     extract_attributes,
11     get_element_by_attribute,
12 )
13 from ..compat import (
14     compat_urlparse,
15 )
16
17
18 class AENetworksBaseIE(ThePlatformIE):
19     _THEPLATFORM_KEY = 'crazyjava'
20     _THEPLATFORM_SECRET = 's3cr3t'
21
22
23 class AENetworksIE(AENetworksBaseIE):
24     IE_NAME = 'aenetworks'
25     IE_DESC = 'A+E Networks: A&E, Lifetime, History.com, FYI Network'
26     _VALID_URL = r'''(?x)
27                     https?://
28                         (?:www\.)?
29                         (?P<domain>
30                             (?:history|aetv|mylifetime|lifetimemovieclub)\.com|
31                             fyi\.tv
32                         )/
33                         (?:
34                             shows/(?P<show_path>[^/]+(?:/[^/]+){0,2})|
35                             movies/(?P<movie_display_id>[^/]+)(?:/full-movie)?|
36                             specials/(?P<special_display_id>[^/]+)/full-special
37                         )
38                     '''
39     _TESTS = [{
40         'url': 'http://www.history.com/shows/mountain-men/season-1/episode-1',
41         'md5': 'a97a65f7e823ae10e9244bc5433d5fe6',
42         'info_dict': {
43             'id': '22253814',
44             'ext': 'mp4',
45             'title': 'Winter Is Coming',
46             'description': 'md5:641f424b7a19d8e24f26dea22cf59d74',
47             'timestamp': 1338306241,
48             'upload_date': '20120529',
49             'uploader': 'AENE-NEW',
50         },
51         'add_ie': ['ThePlatform'],
52     }, {
53         'url': 'http://www.history.com/shows/ancient-aliens/season-1',
54         'info_dict': {
55             'id': '71889446852',
56         },
57         'playlist_mincount': 5,
58     }, {
59         'url': 'http://www.mylifetime.com/shows/atlanta-plastic',
60         'info_dict': {
61             'id': 'SERIES4317',
62             'title': 'Atlanta Plastic',
63         },
64         'playlist_mincount': 2,
65     }, {
66         'url': 'http://www.aetv.com/shows/duck-dynasty/season-9/episode-1',
67         'only_matching': True
68     }, {
69         'url': 'http://www.fyi.tv/shows/tiny-house-nation/season-1/episode-8',
70         'only_matching': True
71     }, {
72         'url': 'http://www.mylifetime.com/shows/project-runway-junior/season-1/episode-6',
73         'only_matching': True
74     }, {
75         'url': 'http://www.mylifetime.com/movies/center-stage-on-pointe/full-movie',
76         'only_matching': True
77     }, {
78         'url': 'https://www.lifetimemovieclub.com/movies/a-killer-among-us',
79         'only_matching': True
80     }, {
81         'url': 'http://www.history.com/specials/sniper-into-the-kill-zone/full-special',
82         'only_matching': True
83     }]
84     _DOMAIN_TO_REQUESTOR_ID = {
85         'history.com': 'HISTORY',
86         'aetv.com': 'AETV',
87         'mylifetime.com': 'LIFETIME',
88         'lifetimemovieclub.com': 'LIFETIMEMOVIECLUB',
89         'fyi.tv': 'FYI',
90     }
91
92     def _real_extract(self, url):
93         domain, show_path, movie_display_id, special_display_id = re.match(self._VALID_URL, url).groups()
94         display_id = show_path or movie_display_id or special_display_id
95         webpage = self._download_webpage(url, display_id)
96         if show_path:
97             url_parts = show_path.split('/')
98             url_parts_len = len(url_parts)
99             if url_parts_len == 1:
100                 entries = []
101                 for season_url_path in re.findall(r'(?s)<li[^>]+data-href="(/shows/%s/season-\d+)"' % url_parts[0], webpage):
102                     entries.append(self.url_result(
103                         compat_urlparse.urljoin(url, season_url_path), 'AENetworks'))
104                 if entries:
105                     return self.playlist_result(
106                         entries, self._html_search_meta('aetn:SeriesId', webpage),
107                         self._html_search_meta('aetn:SeriesTitle', webpage))
108                 else:
109                     # single season
110                     url_parts_len = 2
111             if url_parts_len == 2:
112                 entries = []
113                 for episode_item in re.findall(r'(?s)<[^>]+class="[^"]*(?:episode|program)-item[^"]*"[^>]*>', webpage):
114                     episode_attributes = extract_attributes(episode_item)
115                     episode_url = compat_urlparse.urljoin(
116                         url, episode_attributes['data-canonical'])
117                     entries.append(self.url_result(
118                         episode_url, 'AENetworks',
119                         episode_attributes.get('data-videoid') or episode_attributes.get('data-video-id')))
120                 return self.playlist_result(
121                     entries, self._html_search_meta('aetn:SeasonId', webpage))
122
123         query = {
124             'mbr': 'true',
125             'assetTypes': 'high_video_s3'
126         }
127         video_id = self._html_search_meta('aetn:VideoID', webpage)
128         media_url = self._search_regex(
129             [r"media_url\s*=\s*'(?P<url>[^']+)'",
130              r'data-media-url=(?P<url>(?:https?:)?//[^\s>]+)',
131              r'data-media-url=(["\'])(?P<url>(?:(?!\1).)+?)\1'],
132             webpage, 'video url', group='url')
133         theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
134             r'https?://link.theplatform.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
135         info = self._parse_theplatform_metadata(theplatform_metadata)
136         if theplatform_metadata.get('AETN$isBehindWall'):
137             requestor_id = self._DOMAIN_TO_REQUESTOR_ID[domain]
138             resource = self._get_mvpd_resource(
139                 requestor_id, theplatform_metadata['title'],
140                 theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
141                 theplatform_metadata['ratings'][0]['rating'])
142             query['auth'] = self._extract_mvpd_auth(
143                 url, video_id, requestor_id, resource)
144         info.update(self._search_json_ld(webpage, video_id, fatal=False))
145         media_url = update_url_query(media_url, query)
146         media_url = self._sign_url(media_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
147         formats, subtitles = self._extract_theplatform_smil(media_url, video_id)
148         self._sort_formats(formats)
149         info.update({
150             'id': video_id,
151             'formats': formats,
152             'subtitles': subtitles,
153         })
154         return info
155
156
157 class HistoryTopicIE(AENetworksBaseIE):
158     IE_NAME = 'history:topic'
159     IE_DESC = 'History.com Topic'
160     _VALID_URL = r'https?://(?:www\.)?history\.com/topics/(?:[^/]+/)?(?P<topic_id>[^/]+)(?:/[^/]+(?:/(?P<video_display_id>[^/?#]+))?)?'
161     _TESTS = [{
162         'url': 'http://www.history.com/topics/valentines-day/history-of-valentines-day/videos/bet-you-didnt-know-valentines-day?m=528e394da93ae&s=undefined&f=1&free=false',
163         'info_dict': {
164             'id': '40700995724',
165             'ext': 'mp4',
166             'title': "Bet You Didn't Know: Valentine's Day",
167             'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
168             'timestamp': 1375819729,
169             'upload_date': '20130806',
170             'uploader': 'AENE-NEW',
171         },
172         'params': {
173             # m3u8 download
174             'skip_download': True,
175         },
176         'add_ie': ['ThePlatform'],
177     }, {
178         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/videos',
179         'info_dict':
180         {
181             'id': 'world-war-i-history',
182             'title': 'World War I History',
183         },
184         'playlist_mincount': 23,
185     }, {
186         'url': 'http://www.history.com/topics/world-war-i-history/videos',
187         'only_matching': True,
188     }, {
189         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history',
190         'only_matching': True,
191     }, {
192         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/speeches',
193         'only_matching': True,
194     }]
195
196     def theplatform_url_result(self, theplatform_url, video_id, query):
197         return {
198             '_type': 'url_transparent',
199             'id': video_id,
200             'url': smuggle_url(
201                 update_url_query(theplatform_url, query),
202                 {
203                     'sig': {
204                         'key': self._THEPLATFORM_KEY,
205                         'secret': self._THEPLATFORM_SECRET,
206                     },
207                     'force_smil_url': True
208                 }),
209             'ie_key': 'ThePlatform',
210         }
211
212     def _real_extract(self, url):
213         topic_id, video_display_id = re.match(self._VALID_URL, url).groups()
214         if video_display_id:
215             webpage = self._download_webpage(url, video_display_id)
216             release_url, video_id = re.search(r"_videoPlayer.play\('([^']+)'\s*,\s*'[^']+'\s*,\s*'(\d+)'\)", webpage).groups()
217             release_url = unescapeHTML(release_url)
218
219             return self.theplatform_url_result(
220                 release_url, video_id, {
221                     'mbr': 'true',
222                     'switch': 'hls',
223                     'assetTypes': 'high_video_ak',
224                 })
225         else:
226             webpage = self._download_webpage(url, topic_id)
227             entries = []
228             for episode_item in re.findall(r'<a.+?data-release-url="[^"]+"[^>]*>', webpage):
229                 video_attributes = extract_attributes(episode_item)
230                 entries.append(self.theplatform_url_result(
231                     video_attributes['data-release-url'], video_attributes['data-id'], {
232                         'mbr': 'true',
233                         'switch': 'hls',
234                         'assetTypes': 'high_video_ak',
235                     }))
236             return self.playlist_result(entries, topic_id, get_element_by_attribute('class', 'show-title', webpage))