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