Merge branch 'fstirlitz-filmon'
[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'https?://(?:www\.)?(?P<domain>(?:history|aetv|mylifetime)\.com|fyi\.tv)/(?:shows/(?P<show_path>[^/]+(?:/[^/]+){0,2})|movies/(?P<movie_display_id>[^/]+)/full-movie)'
27     _TESTS = [{
28         'url': 'http://www.history.com/shows/mountain-men/season-1/episode-1',
29         'md5': 'a97a65f7e823ae10e9244bc5433d5fe6',
30         'info_dict': {
31             'id': '22253814',
32             'ext': 'mp4',
33             'title': 'Winter Is Coming',
34             'description': 'md5:641f424b7a19d8e24f26dea22cf59d74',
35             'timestamp': 1338306241,
36             'upload_date': '20120529',
37             'uploader': 'AENE-NEW',
38         },
39         'add_ie': ['ThePlatform'],
40     }, {
41         'url': 'http://www.history.com/shows/ancient-aliens/season-1',
42         'info_dict': {
43             'id': '71889446852',
44         },
45         'playlist_mincount': 5,
46     }, {
47         'url': 'http://www.mylifetime.com/shows/atlanta-plastic',
48         'info_dict': {
49             'id': 'SERIES4317',
50             'title': 'Atlanta Plastic',
51         },
52         'playlist_mincount': 2,
53     }, {
54         'url': 'http://www.aetv.com/shows/duck-dynasty/season-9/episode-1',
55         'only_matching': True
56     }, {
57         'url': 'http://www.fyi.tv/shows/tiny-house-nation/season-1/episode-8',
58         'only_matching': True
59     }, {
60         'url': 'http://www.mylifetime.com/shows/project-runway-junior/season-1/episode-6',
61         'only_matching': True
62     }, {
63         'url': 'http://www.mylifetime.com/movies/center-stage-on-pointe/full-movie',
64         'only_matching': True
65     }]
66     _DOMAIN_TO_REQUESTOR_ID = {
67         'history.com': 'HISTORY',
68         'aetv.com': 'AETV',
69         'mylifetime.com': 'LIFETIME',
70         'fyi.tv': 'FYI',
71     }
72
73     def _real_extract(self, url):
74         domain, show_path, movie_display_id = re.match(self._VALID_URL, url).groups()
75         display_id = show_path or movie_display_id
76         webpage = self._download_webpage(url, display_id)
77         if show_path:
78             url_parts = show_path.split('/')
79             url_parts_len = len(url_parts)
80             if url_parts_len == 1:
81                 entries = []
82                 for season_url_path in re.findall(r'(?s)<li[^>]+data-href="(/shows/%s/season-\d+)"' % url_parts[0], webpage):
83                     entries.append(self.url_result(
84                         compat_urlparse.urljoin(url, season_url_path), 'AENetworks'))
85                 return self.playlist_result(
86                     entries, self._html_search_meta('aetn:SeriesId', webpage),
87                     self._html_search_meta('aetn:SeriesTitle', webpage))
88             elif url_parts_len == 2:
89                 entries = []
90                 for episode_item in re.findall(r'(?s)<[^>]+class="[^"]*(?:episode|program)-item[^"]*"[^>]*>', webpage):
91                     episode_attributes = extract_attributes(episode_item)
92                     episode_url = compat_urlparse.urljoin(
93                         url, episode_attributes['data-canonical'])
94                     entries.append(self.url_result(
95                         episode_url, 'AENetworks',
96                         episode_attributes['data-videoid']))
97                 return self.playlist_result(
98                     entries, self._html_search_meta('aetn:SeasonId', webpage))
99
100         query = {
101             'mbr': 'true',
102             'assetTypes': 'high_video_s3'
103         }
104         video_id = self._html_search_meta('aetn:VideoID', webpage)
105         media_url = self._search_regex(
106             r"media_url\s*=\s*'([^']+)'", webpage, 'video url')
107         theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
108             r'https?://link.theplatform.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
109         info = self._parse_theplatform_metadata(theplatform_metadata)
110         if theplatform_metadata.get('AETN$isBehindWall'):
111             requestor_id = self._DOMAIN_TO_REQUESTOR_ID[domain]
112             resource = self._get_mvpd_resource(
113                 requestor_id, theplatform_metadata['title'],
114                 theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
115                 theplatform_metadata['ratings'][0]['rating'])
116             query['auth'] = self._extract_mvpd_auth(
117                 url, video_id, requestor_id, resource)
118         info.update(self._search_json_ld(webpage, video_id, fatal=False))
119         media_url = update_url_query(media_url, query)
120         media_url = self._sign_url(media_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
121         formats, subtitles = self._extract_theplatform_smil(media_url, video_id)
122         self._sort_formats(formats)
123         info.update({
124             'id': video_id,
125             'formats': formats,
126             'subtitles': subtitles,
127         })
128         return info
129
130
131 class HistoryTopicIE(AENetworksBaseIE):
132     IE_NAME = 'history:topic'
133     IE_DESC = 'History.com Topic'
134     _VALID_URL = r'https?://(?:www\.)?history\.com/topics/(?:[^/]+/)?(?P<topic_id>[^/]+)(?:/[^/]+(?:/(?P<video_display_id>[^/?#]+))?)?'
135     _TESTS = [{
136         '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',
137         'info_dict': {
138             'id': '40700995724',
139             'ext': 'mp4',
140             'title': "Bet You Didn't Know: Valentine's Day",
141             'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
142             'timestamp': 1375819729,
143             'upload_date': '20130806',
144             'uploader': 'AENE-NEW',
145         },
146         'params': {
147             # m3u8 download
148             'skip_download': True,
149         },
150         'add_ie': ['ThePlatform'],
151     }, {
152         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/videos',
153         'info_dict':
154         {
155             'id': 'world-war-i-history',
156             'title': 'World War I History',
157         },
158         'playlist_mincount': 23,
159     }, {
160         'url': 'http://www.history.com/topics/world-war-i-history/videos',
161         'only_matching': True,
162     }, {
163         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history',
164         'only_matching': True,
165     }, {
166         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/speeches',
167         'only_matching': True,
168     }]
169
170     def theplatform_url_result(self, theplatform_url, video_id, query):
171         return {
172             '_type': 'url_transparent',
173             'id': video_id,
174             'url': smuggle_url(
175                 update_url_query(theplatform_url, query),
176                 {
177                     'sig': {
178                         'key': self._THEPLATFORM_KEY,
179                         'secret': self._THEPLATFORM_SECRET,
180                     },
181                     'force_smil_url': True
182                 }),
183             'ie_key': 'ThePlatform',
184         }
185
186     def _real_extract(self, url):
187         topic_id, video_display_id = re.match(self._VALID_URL, url).groups()
188         if video_display_id:
189             webpage = self._download_webpage(url, video_display_id)
190             release_url, video_id = re.search(r"_videoPlayer.play\('([^']+)'\s*,\s*'[^']+'\s*,\s*'(\d+)'\)", webpage).groups()
191             release_url = unescapeHTML(release_url)
192
193             return self.theplatform_url_result(
194                 release_url, video_id, {
195                     'mbr': 'true',
196                     'switch': 'hls',
197                     'assetTypes': 'high_video_ak',
198                 })
199         else:
200             webpage = self._download_webpage(url, topic_id)
201             entries = []
202             for episode_item in re.findall(r'<a.+?data-release-url="[^"]+"[^>]*>', webpage):
203                 video_attributes = extract_attributes(episode_item)
204                 entries.append(self.theplatform_url_result(
205                     video_attributes['data-release-url'], video_attributes['data-id'], {
206                         'mbr': 'true',
207                         'switch': 'hls',
208                         'assetTypes': 'high_video_ak',
209                     }))
210             return self.playlist_result(entries, topic_id, get_element_by_attribute('class', 'show-title', webpage))