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