]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/aenetworks.py
[extractor, cleanup] Refactor `_download_...` methods
[yt-dlp.git] / yt_dlp / extractor / aenetworks.py
CommitLineData
05c7feec 1from .theplatform import ThePlatformIE
d8873d4d 2from ..utils import (
4f1e02ad 3 ExtractorError,
29f7c58a 4 GeoRestrictedError,
4f1e02ad 5 int_or_none,
d8873d4d 6 update_url_query,
29f7c58a 7 urlencode_postdata,
42362fdb
RA
8)
9
10
05c7feec 11class AENetworksBaseIE(ThePlatformIE):
29f7c58a 12 _BASE_URL_REGEX = r'''(?x)https?://
13 (?:(?:www|play|watch)\.)?
14 (?P<domain>
15 (?:history(?:vault)?|aetv|mylifetime|lifetimemovieclub)\.com|
16 fyi\.tv
17 )/'''
3b34e388
W
18 _THEPLATFORM_KEY = '43jXaGRQud'
19 _THEPLATFORM_SECRET = 'S10BPXHMlb'
29f7c58a 20 _DOMAIN_MAP = {
21 'history.com': ('HISTORY', 'history'),
22 'aetv.com': ('AETV', 'aetv'),
23 'mylifetime.com': ('LIFETIME', 'lifetime'),
24 'lifetimemovieclub.com': ('LIFETIMEMOVIECLUB', 'lmc'),
25 'fyi.tv': ('FYI', 'fyi'),
26 'historyvault.com': (None, 'historyvault'),
27 'biography.com': (None, 'biography'),
28 }
b9c7a973 29
4f1e02ad
RA
30 def _extract_aen_smil(self, smil_url, video_id, auth=None):
31 query = {'mbr': 'true'}
32 if auth:
33 query['auth'] = auth
34 TP_SMIL_QUERY = [{
35 'assetTypes': 'high_video_ak',
36 'switch': 'hls_high_ak'
37 }, {
38 'assetTypes': 'high_video_s3'
39 }, {
40 'assetTypes': 'high_video_s3',
29f7c58a 41 'switch': 'hls_high_fastly',
4f1e02ad
RA
42 }]
43 formats = []
44 subtitles = {}
45 last_e = None
46 for q in TP_SMIL_QUERY:
47 q.update(query)
48 m_url = update_url_query(smil_url, q)
49 m_url = self._sign_url(m_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
50 try:
51 tp_formats, tp_subtitles = self._extract_theplatform_smil(
52 m_url, video_id, 'Downloading %s SMIL data' % (q.get('switch') or q['assetTypes']))
53 except ExtractorError as e:
29f7c58a 54 if isinstance(e, GeoRestrictedError):
55 raise
4f1e02ad
RA
56 last_e = e
57 continue
58 formats.extend(tp_formats)
59 subtitles = self._merge_subtitles(subtitles, tp_subtitles)
60 if last_e and not formats:
61 raise last_e
62 self._sort_formats(formats)
63 return {
64 'id': video_id,
65 'formats': formats,
66 'subtitles': subtitles,
67 }
68
29f7c58a 69 def _extract_aetn_info(self, domain, filter_key, filter_value, url):
70 requestor_id, brand = self._DOMAIN_MAP[domain]
71 result = self._download_json(
72 'https://feeds.video.aetnd.com/api/v2/%s/videos' % brand,
73 filter_value, query={'filter[%s]' % filter_key: filter_value})['results'][0]
74 title = result['title']
75 video_id = result['id']
76 media_url = result['publicUrl']
77 theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
78 r'https?://link\.theplatform\.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
79 info = self._parse_theplatform_metadata(theplatform_metadata)
80 auth = None
81 if theplatform_metadata.get('AETN$isBehindWall'):
82 resource = self._get_mvpd_resource(
83 requestor_id, theplatform_metadata['title'],
84 theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
85 theplatform_metadata['ratings'][0]['rating'])
86 auth = self._extract_mvpd_auth(
87 url, video_id, requestor_id, resource)
88 info.update(self._extract_aen_smil(media_url, video_id, auth))
89 info.update({
90 'title': title,
91 'series': result.get('seriesName'),
92 'season_number': int_or_none(result.get('tvSeasonNumber')),
93 'episode_number': int_or_none(result.get('tvSeasonEpisodeNumber')),
94 })
95 return info
96
b9c7a973 97
42362fdb 98class AENetworksIE(AENetworksBaseIE):
855f90fa 99 IE_NAME = 'aenetworks'
3ad6dabd 100 IE_DESC = 'A+E Networks: A&E, Lifetime, History.com, FYI Network and History Vault'
29f7c58a 101 _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'''(?P<id>
102 shows/[^/]+/season-\d+/episode-\d+|
103 (?:
104 (?:movie|special)s/[^/]+|
105 (?:shows/[^/]+/)?videos
106 )/[^/?#&]+
107 )'''
b9c7a973 108 _TESTS = [{
52767c1b 109 'url': 'http://www.history.com/shows/mountain-men/season-1/episode-1',
110 'info_dict': {
42362fdb 111 'id': '22253814',
52767c1b 112 'ext': 'mp4',
4f1e02ad 113 'title': 'Winter is Coming',
1358b941 114 'description': 'md5:641f424b7a19d8e24f26dea22cf59d74',
79ba9140 115 'timestamp': 1338306241,
116 'upload_date': '20120529',
117 'uploader': 'AENE-NEW',
52767c1b 118 },
4f1e02ad
RA
119 'params': {
120 # m3u8 download
121 'skip_download': True,
122 },
b9c7a973 123 'add_ie': ['ThePlatform'],
29f7c58a 124 'skip': 'This video is only available for users of participating TV providers.',
587dfd44 125 }, {
29f7c58a 126 'url': 'http://www.aetv.com/shows/duck-dynasty/season-9/episode-1',
42362fdb 127 'info_dict': {
29f7c58a 128 'id': '600587331957',
129 'ext': 'mp4',
130 'title': 'Inlawful Entry',
131 'description': 'md5:57c12115a2b384d883fe64ca50529e08',
132 'timestamp': 1452634428,
133 'upload_date': '20160112',
134 'uploader': 'AENE-NEW',
42362fdb 135 },
29f7c58a 136 'params': {
137 # m3u8 download
138 'skip_download': True,
42362fdb 139 },
29f7c58a 140 'add_ie': ['ThePlatform'],
587dfd44 141 }, {
42362fdb 142 'url': 'http://www.fyi.tv/shows/tiny-house-nation/season-1/episode-8',
587dfd44 143 'only_matching': True
144 }, {
42362fdb 145 'url': 'http://www.mylifetime.com/shows/project-runway-junior/season-1/episode-6',
587dfd44 146 'only_matching': True
70157c2c
RA
147 }, {
148 'url': 'http://www.mylifetime.com/movies/center-stage-on-pointe/full-movie',
149 'only_matching': True
459818e2 150 }, {
29f7c58a 151 'url': 'https://watch.lifetimemovieclub.com/movies/10-year-reunion/full-movie',
459818e2 152 'only_matching': True
95728fda
S
153 }, {
154 'url': 'http://www.history.com/specials/sniper-into-the-kill-zone/full-special',
155 'only_matching': True
3ad6dabd 156 }, {
29f7c58a 157 'url': 'https://www.aetv.com/specials/hunting-jonbenets-killer-the-untold-story/preview-hunting-jonbenets-killer-the-untold-story',
158 'only_matching': True
159 }, {
160 'url': 'http://www.history.com/videos/history-of-valentines-day',
3ad6dabd 161 'only_matching': True
4f1e02ad 162 }, {
29f7c58a 163 'url': 'https://play.aetv.com/shows/duck-dynasty/videos/best-of-duck-dynasty-getting-quack-in-shape',
4f1e02ad 164 'only_matching': True
b9c7a973
S
165 }]
166
167 def _real_extract(self, url):
5ad28e7f 168 domain, canonical = self._match_valid_url(url).groups()
29f7c58a 169 return self._extract_aetn_info(domain, 'canonical', '/' + canonical, url)
170
171
172class AENetworksListBaseIE(AENetworksBaseIE):
173 def _call_api(self, resource, slug, brand, fields):
174 return self._download_json(
175 'https://yoga.appsvcs.aetnd.com/graphql',
176 slug, query={'brand': brand}, data=urlencode_postdata({
177 'query': '''{
178 %s(slug: "%s") {
179 %s
180 }
181}''' % (resource, slug, fields),
182 }))['data'][resource]
183
184 def _real_extract(self, url):
5ad28e7f 185 domain, slug = self._match_valid_url(url).groups()
29f7c58a 186 _, brand = self._DOMAIN_MAP[domain]
187 playlist = self._call_api(self._RESOURCE, slug, brand, self._FIELDS)
188 base_url = 'http://watch.%s' % domain
189
190 entries = []
191 for item in (playlist.get(self._ITEMS_KEY) or []):
192 doc = self._get_doc(item)
193 canonical = doc.get('canonical')
194 if not canonical:
195 continue
196 entries.append(self.url_result(
197 base_url + canonical, AENetworksIE.ie_key(), doc.get('id')))
198
199 description = None
200 if self._PLAYLIST_DESCRIPTION_KEY:
201 description = playlist.get(self._PLAYLIST_DESCRIPTION_KEY)
202
203 return self.playlist_result(
204 entries, playlist.get('id'),
205 playlist.get(self._PLAYLIST_TITLE_KEY), description)
206
207
208class AENetworksCollectionIE(AENetworksListBaseIE):
209 IE_NAME = 'aenetworks:collection'
210 _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'(?:[^/]+/)*(?:list|collections)/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
211 _TESTS = [{
212 'url': 'https://watch.historyvault.com/list/america-the-story-of-us',
213 'info_dict': {
214 'id': '282',
215 'title': 'America The Story of Us',
216 },
217 'playlist_mincount': 12,
218 }, {
219 'url': 'https://watch.historyvault.com/shows/america-the-story-of-us-2/season-1/list/america-the-story-of-us',
220 'only_matching': True
221 }, {
222 'url': 'https://www.historyvault.com/collections/mysteryquest',
223 'only_matching': True
224 }]
225 _RESOURCE = 'list'
226 _ITEMS_KEY = 'items'
227 _PLAYLIST_TITLE_KEY = 'display_title'
228 _PLAYLIST_DESCRIPTION_KEY = None
229 _FIELDS = '''id
230 display_title
231 items {
232 ... on ListVideoItem {
233 doc {
234 canonical
235 id
236 }
237 }
238 }'''
239
240 def _get_doc(self, item):
241 return item.get('doc') or {}
242
243
244class AENetworksShowIE(AENetworksListBaseIE):
245 IE_NAME = 'aenetworks:show'
246 _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'shows/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
247 _TESTS = [{
248 'url': 'http://www.history.com/shows/ancient-aliens',
249 'info_dict': {
2181983a 250 'id': 'SERIES1574',
29f7c58a 251 'title': 'Ancient Aliens',
252 'description': 'md5:3f6d74daf2672ff3ae29ed732e37ea7f',
253 },
a820dc72 254 'playlist_mincount': 150,
29f7c58a 255 }]
256 _RESOURCE = 'series'
257 _ITEMS_KEY = 'episodes'
258 _PLAYLIST_TITLE_KEY = 'title'
259 _PLAYLIST_DESCRIPTION_KEY = 'description'
260 _FIELDS = '''description
261 id
262 title
263 episodes {
264 canonical
265 id
266 }'''
267
268 def _get_doc(self, item):
269 return item
b9c7a973 270
b9c7a973 271
42362fdb
RA
272class HistoryTopicIE(AENetworksBaseIE):
273 IE_NAME = 'history:topic'
274 IE_DESC = 'History.com Topic'
4f1e02ad 275 _VALID_URL = r'https?://(?:www\.)?history\.com/topics/[^/]+/(?P<id>[\w+-]+?)-video'
42362fdb 276 _TESTS = [{
4f1e02ad 277 'url': 'https://www.history.com/topics/valentines-day/history-of-valentines-day-video',
42362fdb
RA
278 'info_dict': {
279 'id': '40700995724',
280 'ext': 'mp4',
4f1e02ad 281 'title': "History of Valentine’s Day",
42362fdb
RA
282 'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
283 'timestamp': 1375819729,
284 'upload_date': '20130806',
29f7c58a 285 'uploader': 'AENE-NEW',
42362fdb
RA
286 },
287 'params': {
288 # m3u8 download
289 'skip_download': True,
290 },
291 'add_ie': ['ThePlatform'],
42362fdb
RA
292 }]
293
29f7c58a 294 def _real_extract(self, url):
295 display_id = self._match_id(url)
296 return self.url_result(
297 'http://www.history.com/videos/' + display_id,
298 AENetworksIE.ie_key())
299
300
301class HistoryPlayerIE(AENetworksBaseIE):
302 IE_NAME = 'history:player'
303 _VALID_URL = r'https?://(?:www\.)?(?P<domain>(?:history|biography)\.com)/player/(?P<id>\d+)'
304 _TESTS = []
305
306 def _real_extract(self, url):
5ad28e7f 307 domain, video_id = self._match_valid_url(url).groups()
29f7c58a 308 return self._extract_aetn_info(domain, 'id', video_id, url)
309
310
311class BiographyIE(AENetworksBaseIE):
312 _VALID_URL = r'https?://(?:www\.)?biography\.com/video/(?P<id>[^/?#&]+)'
313 _TESTS = [{
314 'url': 'https://www.biography.com/video/vincent-van-gogh-full-episode-2075049808',
315 'info_dict': {
316 'id': '30322987',
317 'ext': 'mp4',
318 'title': 'Vincent Van Gogh - Full Episode',
319 'description': 'A full biography about the most influential 20th century painter, Vincent Van Gogh.',
320 'timestamp': 1311970571,
321 'upload_date': '20110729',
322 'uploader': 'AENE-NEW',
323 },
324 'params': {
325 # m3u8 download
326 'skip_download': True,
327 },
328 'add_ie': ['ThePlatform'],
329 }]
05c7feec 330
42362fdb 331 def _real_extract(self, url):
4f1e02ad
RA
332 display_id = self._match_id(url)
333 webpage = self._download_webpage(url, display_id)
29f7c58a 334 player_url = self._search_regex(
335 r'<phoenix-iframe[^>]+src="(%s)' % HistoryPlayerIE._VALID_URL,
336 webpage, 'player URL')
337 return self.url_result(player_url, HistoryPlayerIE.ie_key())