[aenetworks] Add support for lifetimemovieclub.com
[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|lifetimemovieclub)\.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         'url': 'https://www.lifetimemovieclub.com/movies/a-killer-among-us',
67         'only_matching': True
68     }]
69     _DOMAIN_TO_REQUESTOR_ID = {
70         'history.com': 'HISTORY',
71         'aetv.com': 'AETV',
72         'mylifetime.com': 'LIFETIME',
73         'lifetimemovieclub.com': 'LIFETIMEMOVIECLUB',
74         'fyi.tv': 'FYI',
75     }
76
77     def _real_extract(self, url):
78         domain, show_path, movie_display_id = re.match(self._VALID_URL, url).groups()
79         display_id = show_path or movie_display_id
80         webpage = self._download_webpage(url, display_id)
81         if show_path:
82             url_parts = show_path.split('/')
83             url_parts_len = len(url_parts)
84             if url_parts_len == 1:
85                 entries = []
86                 for season_url_path in re.findall(r'(?s)<li[^>]+data-href="(/shows/%s/season-\d+)"' % url_parts[0], webpage):
87                     entries.append(self.url_result(
88                         compat_urlparse.urljoin(url, season_url_path), 'AENetworks'))
89                 return self.playlist_result(
90                     entries, self._html_search_meta('aetn:SeriesId', webpage),
91                     self._html_search_meta('aetn:SeriesTitle', webpage))
92             elif url_parts_len == 2:
93                 entries = []
94                 for episode_item in re.findall(r'(?s)<[^>]+class="[^"]*(?:episode|program)-item[^"]*"[^>]*>', webpage):
95                     episode_attributes = extract_attributes(episode_item)
96                     episode_url = compat_urlparse.urljoin(
97                         url, episode_attributes['data-canonical'])
98                     entries.append(self.url_result(
99                         episode_url, 'AENetworks',
100                         episode_attributes['data-videoid']))
101                 return self.playlist_result(
102                     entries, self._html_search_meta('aetn:SeasonId', webpage))
103
104         query = {
105             'mbr': 'true',
106             'assetTypes': 'high_video_s3'
107         }
108         video_id = self._html_search_meta('aetn:VideoID', webpage)
109         media_url = self._search_regex(
110             r"media_url\s*=\s*'([^']+)'", webpage, 'video url')
111         theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
112             r'https?://link.theplatform.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
113         info = self._parse_theplatform_metadata(theplatform_metadata)
114         if theplatform_metadata.get('AETN$isBehindWall'):
115             requestor_id = self._DOMAIN_TO_REQUESTOR_ID[domain]
116             resource = self._get_mvpd_resource(
117                 requestor_id, theplatform_metadata['title'],
118                 theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
119                 theplatform_metadata['ratings'][0]['rating'])
120             query['auth'] = self._extract_mvpd_auth(
121                 url, video_id, requestor_id, resource)
122         info.update(self._search_json_ld(webpage, video_id, fatal=False))
123         media_url = update_url_query(media_url, query)
124         media_url = self._sign_url(media_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
125         formats, subtitles = self._extract_theplatform_smil(media_url, video_id)
126         self._sort_formats(formats)
127         info.update({
128             'id': video_id,
129             'formats': formats,
130             'subtitles': subtitles,
131         })
132         return info
133
134
135 class HistoryTopicIE(AENetworksBaseIE):
136     IE_NAME = 'history:topic'
137     IE_DESC = 'History.com Topic'
138     _VALID_URL = r'https?://(?:www\.)?history\.com/topics/(?:[^/]+/)?(?P<topic_id>[^/]+)(?:/[^/]+(?:/(?P<video_display_id>[^/?#]+))?)?'
139     _TESTS = [{
140         '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',
141         'info_dict': {
142             'id': '40700995724',
143             'ext': 'mp4',
144             'title': "Bet You Didn't Know: Valentine's Day",
145             'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
146             'timestamp': 1375819729,
147             'upload_date': '20130806',
148             'uploader': 'AENE-NEW',
149         },
150         'params': {
151             # m3u8 download
152             'skip_download': True,
153         },
154         'add_ie': ['ThePlatform'],
155     }, {
156         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/videos',
157         'info_dict':
158         {
159             'id': 'world-war-i-history',
160             'title': 'World War I History',
161         },
162         'playlist_mincount': 23,
163     }, {
164         'url': 'http://www.history.com/topics/world-war-i-history/videos',
165         'only_matching': True,
166     }, {
167         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history',
168         'only_matching': True,
169     }, {
170         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/speeches',
171         'only_matching': True,
172     }]
173
174     def theplatform_url_result(self, theplatform_url, video_id, query):
175         return {
176             '_type': 'url_transparent',
177             'id': video_id,
178             'url': smuggle_url(
179                 update_url_query(theplatform_url, query),
180                 {
181                     'sig': {
182                         'key': self._THEPLATFORM_KEY,
183                         'secret': self._THEPLATFORM_SECRET,
184                     },
185                     'force_smil_url': True
186                 }),
187             'ie_key': 'ThePlatform',
188         }
189
190     def _real_extract(self, url):
191         topic_id, video_display_id = re.match(self._VALID_URL, url).groups()
192         if video_display_id:
193             webpage = self._download_webpage(url, video_display_id)
194             release_url, video_id = re.search(r"_videoPlayer.play\('([^']+)'\s*,\s*'[^']+'\s*,\s*'(\d+)'\)", webpage).groups()
195             release_url = unescapeHTML(release_url)
196
197             return self.theplatform_url_result(
198                 release_url, video_id, {
199                     'mbr': 'true',
200                     'switch': 'hls',
201                     'assetTypes': 'high_video_ak',
202                 })
203         else:
204             webpage = self._download_webpage(url, topic_id)
205             entries = []
206             for episode_item in re.findall(r'<a.+?data-release-url="[^"]+"[^>]*>', webpage):
207                 video_attributes = extract_attributes(episode_item)
208                 entries.append(self.theplatform_url_result(
209                     video_attributes['data-release-url'], video_attributes['data-id'], {
210                         'mbr': 'true',
211                         'switch': 'hls',
212                         'assetTypes': 'high_video_ak',
213                     }))
214             return self.playlist_result(entries, topic_id, get_element_by_attribute('class', 'show-title', webpage))