[youtube] fix hd720 format position
[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_ak',
126             'switch': 'hls_high_ak',
127         }
128         video_id = self._html_search_meta('aetn:VideoID', webpage)
129         media_url = self._search_regex(
130             [r"media_url\s*=\s*'(?P<url>[^']+)'",
131              r'data-media-url=(?P<url>(?:https?:)?//[^\s>]+)',
132              r'data-media-url=(["\'])(?P<url>(?:(?!\1).)+?)\1'],
133             webpage, 'video url', group='url')
134         theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
135             r'https?://link\.theplatform\.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
136         info = self._parse_theplatform_metadata(theplatform_metadata)
137         if theplatform_metadata.get('AETN$isBehindWall'):
138             requestor_id = self._DOMAIN_TO_REQUESTOR_ID[domain]
139             resource = self._get_mvpd_resource(
140                 requestor_id, theplatform_metadata['title'],
141                 theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
142                 theplatform_metadata['ratings'][0]['rating'])
143             query['auth'] = self._extract_mvpd_auth(
144                 url, video_id, requestor_id, resource)
145         info.update(self._search_json_ld(webpage, video_id, fatal=False))
146         media_url = update_url_query(media_url, query)
147         media_url = self._sign_url(media_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
148         formats, subtitles = self._extract_theplatform_smil(media_url, video_id)
149         self._sort_formats(formats)
150         info.update({
151             'id': video_id,
152             'formats': formats,
153             'subtitles': subtitles,
154         })
155         return info
156
157
158 class HistoryTopicIE(AENetworksBaseIE):
159     IE_NAME = 'history:topic'
160     IE_DESC = 'History.com Topic'
161     _VALID_URL = r'https?://(?:www\.)?history\.com/topics/(?:[^/]+/)?(?P<topic_id>[^/]+)(?:/[^/]+(?:/(?P<video_display_id>[^/?#]+))?)?'
162     _TESTS = [{
163         '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',
164         'info_dict': {
165             'id': '40700995724',
166             'ext': 'mp4',
167             'title': "Bet You Didn't Know: Valentine's Day",
168             'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
169             'timestamp': 1375819729,
170             'upload_date': '20130806',
171             'uploader': 'AENE-NEW',
172         },
173         'params': {
174             # m3u8 download
175             'skip_download': True,
176         },
177         'add_ie': ['ThePlatform'],
178     }, {
179         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/videos',
180         'info_dict':
181         {
182             'id': 'world-war-i-history',
183             'title': 'World War I History',
184         },
185         'playlist_mincount': 23,
186     }, {
187         'url': 'http://www.history.com/topics/world-war-i-history/videos',
188         'only_matching': True,
189     }, {
190         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history',
191         'only_matching': True,
192     }, {
193         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/speeches',
194         'only_matching': True,
195     }]
196
197     def theplatform_url_result(self, theplatform_url, video_id, query):
198         return {
199             '_type': 'url_transparent',
200             'id': video_id,
201             'url': smuggle_url(
202                 update_url_query(theplatform_url, query),
203                 {
204                     'sig': {
205                         'key': self._THEPLATFORM_KEY,
206                         'secret': self._THEPLATFORM_SECRET,
207                     },
208                     'force_smil_url': True
209                 }),
210             'ie_key': 'ThePlatform',
211         }
212
213     def _real_extract(self, url):
214         topic_id, video_display_id = re.match(self._VALID_URL, url).groups()
215         if video_display_id:
216             webpage = self._download_webpage(url, video_display_id)
217             release_url, video_id = re.search(r"_videoPlayer.play\('([^']+)'\s*,\s*'[^']+'\s*,\s*'(\d+)'\)", webpage).groups()
218             release_url = unescapeHTML(release_url)
219
220             return self.theplatform_url_result(
221                 release_url, video_id, {
222                     'mbr': 'true',
223                     'switch': 'hls',
224                     'assetTypes': 'high_video_ak',
225                 })
226         else:
227             webpage = self._download_webpage(url, topic_id)
228             entries = []
229             for episode_item in re.findall(r'<a.+?data-release-url="[^"]+"[^>]*>', webpage):
230                 video_attributes = extract_attributes(episode_item)
231                 entries.append(self.theplatform_url_result(
232                     video_attributes['data-release-url'], video_attributes['data-id'], {
233                         'mbr': 'true',
234                         'switch': 'hls',
235                         'assetTypes': 'high_video_ak',
236                     }))
237             return self.playlist_result(entries, topic_id, get_element_by_attribute('class', 'show-title', webpage))