]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/amcnetworks.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / amcnetworks.py
1 import re
2
3 from .theplatform import ThePlatformIE
4 from ..utils import (
5 int_or_none,
6 parse_age_limit,
7 try_get,
8 update_url_query,
9 )
10
11
12 class AMCNetworksIE(ThePlatformIE): # XXX: Do not subclass from concrete IE
13 _VALID_URL = r'https?://(?:www\.)?(?P<site>amc|bbcamerica|ifc|(?:we|sundance)tv)\.com/(?P<id>(?:movies|shows(?:/[^/]+)+)/[^/?#&]+)'
14 _TESTS = [{
15 'url': 'https://www.bbcamerica.com/shows/the-graham-norton-show/videos/tina-feys-adorable-airline-themed-family-dinner--51631',
16 'info_dict': {
17 'id': '4Lq1dzOnZGt0',
18 'ext': 'mp4',
19 'title': "The Graham Norton Show - Season 28 - Tina Fey's Adorable Airline-Themed Family Dinner",
20 'description': "It turns out child stewardesses are very generous with the wine! All-new episodes of 'The Graham Norton Show' premiere Fridays at 11/10c on BBC America.",
21 'upload_date': '20201120',
22 'timestamp': 1605904350,
23 'uploader': 'AMCN',
24 },
25 'params': {
26 # m3u8 download
27 'skip_download': True,
28 },
29 'skip': '404 Not Found',
30 }, {
31 'url': 'http://www.bbcamerica.com/shows/the-hunt/full-episodes/season-1/episode-01-the-hardest-challenge',
32 'only_matching': True,
33 }, {
34 'url': 'http://www.amc.com/shows/preacher/full-episodes/season-01/episode-00/pilot',
35 'only_matching': True,
36 }, {
37 'url': 'http://www.wetv.com/shows/million-dollar-matchmaker/season-01/episode-06-the-dumped-dj-and-shallow-hal',
38 'only_matching': True,
39 }, {
40 'url': 'http://www.ifc.com/movies/chaos',
41 'only_matching': True,
42 }, {
43 'url': 'http://www.bbcamerica.com/shows/doctor-who/full-episodes/the-power-of-the-daleks/episode-01-episode-1-color-version',
44 'only_matching': True,
45 }, {
46 'url': 'http://www.wetv.com/shows/mama-june-from-not-to-hot/full-episode/season-01/thin-tervention',
47 'only_matching': True,
48 }, {
49 'url': 'http://www.wetv.com/shows/la-hair/videos/season-05/episode-09-episode-9-2/episode-9-sneak-peek-3',
50 'only_matching': True,
51 }, {
52 'url': 'https://www.sundancetv.com/shows/riviera/full-episodes/season-1/episode-01-episode-1',
53 'only_matching': True,
54 }]
55 _REQUESTOR_ID_MAP = {
56 'amc': 'AMC',
57 'bbcamerica': 'BBCA',
58 'ifc': 'IFC',
59 'sundancetv': 'SUNDANCE',
60 'wetv': 'WETV',
61 }
62
63 def _real_extract(self, url):
64 site, display_id = self._match_valid_url(url).groups()
65 requestor_id = self._REQUESTOR_ID_MAP[site]
66 page_data = self._download_json(
67 f'https://content-delivery-gw.svc.ds.amcn.com/api/v2/content/amcn/{requestor_id.lower()}/url/{display_id}',
68 display_id)['data']
69 properties = page_data.get('properties') or {}
70 query = {
71 'mbr': 'true',
72 'manifest': 'm3u',
73 }
74
75 video_player_count = 0
76 try:
77 for v in page_data['children']:
78 if v.get('type') == 'video-player':
79 release_pid = v['properties']['currentVideo']['meta']['releasePid']
80 tp_path = 'M_UwQC/' + release_pid
81 media_url = 'https://link.theplatform.com/s/' + tp_path
82 video_player_count += 1
83 except KeyError:
84 pass
85 if video_player_count > 1:
86 self.report_warning(
87 f'The JSON data has {video_player_count} video players. Only one will be extracted')
88
89 # Fall back to videoPid if releasePid not found.
90 # TODO: Fall back to videoPid if releasePid manifest uses DRM.
91 if not video_player_count:
92 tp_path = 'M_UwQC/media/' + properties['videoPid']
93 media_url = 'https://link.theplatform.com/s/' + tp_path
94
95 theplatform_metadata = self._download_theplatform_metadata(tp_path, display_id)
96 info = self._parse_theplatform_metadata(theplatform_metadata)
97 video_id = theplatform_metadata['pid']
98 title = theplatform_metadata['title']
99 rating = try_get(
100 theplatform_metadata, lambda x: x['ratings'][0]['rating'])
101 video_category = properties.get('videoCategory')
102 if video_category and video_category.endswith('-Auth'):
103 resource = self._get_mvpd_resource(
104 requestor_id, title, video_id, rating)
105 query['auth'] = self._extract_mvpd_auth(
106 url, video_id, requestor_id, resource)
107 media_url = update_url_query(media_url, query)
108 formats, subtitles = self._extract_theplatform_smil(
109 media_url, video_id)
110
111 thumbnails = []
112 thumbnail_urls = [properties.get('imageDesktop')]
113 if 'thumbnail' in info:
114 thumbnail_urls.append(info.pop('thumbnail'))
115 for thumbnail_url in thumbnail_urls:
116 if not thumbnail_url:
117 continue
118 mobj = re.search(r'(\d+)x(\d+)', thumbnail_url)
119 thumbnails.append({
120 'url': thumbnail_url,
121 'width': int(mobj.group(1)) if mobj else None,
122 'height': int(mobj.group(2)) if mobj else None,
123 })
124
125 info.update({
126 'age_limit': parse_age_limit(rating),
127 'formats': formats,
128 'id': video_id,
129 'subtitles': subtitles,
130 'thumbnails': thumbnails,
131 })
132 ns_keys = theplatform_metadata.get('$xmlns', {}).keys()
133 if ns_keys:
134 ns = next(iter(ns_keys))
135 episode = theplatform_metadata.get(ns + '$episodeTitle') or None
136 episode_number = int_or_none(
137 theplatform_metadata.get(ns + '$episode'))
138 season_number = int_or_none(
139 theplatform_metadata.get(ns + '$season'))
140 series = theplatform_metadata.get(ns + '$show') or None
141 info.update({
142 'episode': episode,
143 'episode_number': episode_number,
144 'season_number': season_number,
145 'series': series,
146 })
147 return info