]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tenplay.py
[ie/tenplay] Add support for seasons (#7939)
[yt-dlp.git] / yt_dlp / extractor / tenplay.py
1 import base64
2 import functools
3 import itertools
4 from datetime import datetime
5
6 from .common import InfoExtractor
7 from ..networking import HEADRequest
8 from ..utils import int_or_none, traverse_obj, urlencode_postdata, urljoin
9
10
11 class TenPlayIE(InfoExtractor):
12 _VALID_URL = r'https?://(?:www\.)?10play\.com\.au/(?:[^/]+/)+(?P<id>tpv\d{6}[a-z]{5})'
13 _NETRC_MACHINE = '10play'
14 _TESTS = [{
15 'url': 'https://10play.com.au/neighbours/web-extras/season-39/nathan-borg-is-the-first-aussie-actor-with-a-cochlear-implant-to-join-neighbours/tpv210128qupwd',
16 'info_dict': {
17 'id': '6226844312001',
18 'ext': 'mp4',
19 'title': 'Nathan Borg Is The First Aussie Actor With A Cochlear Implant To Join Neighbours',
20 'alt_title': 'Nathan Borg Is The First Aussie Actor With A Cochlear Implant To Join Neighbours',
21 'description': 'md5:a02d0199c901c2dd4c796f1e7dd0de43',
22 'duration': 186,
23 'season': 39,
24 'series': 'Neighbours',
25 'thumbnail': r're:https://.*\.jpg',
26 'uploader': 'Channel 10',
27 'age_limit': 15,
28 'timestamp': 1611810000,
29 'upload_date': '20210128',
30 'uploader_id': '2199827728001',
31 },
32 'params': {
33 'skip_download': True,
34 },
35 'skip': 'Only available in Australia',
36 }, {
37 'url': 'https://10play.com.au/todd-sampsons-body-hack/episodes/season-4/episode-7/tpv200921kvngh',
38 'info_dict': {
39 'id': '6192880312001',
40 'ext': 'mp4',
41 'title': "Todd Sampson's Body Hack - S4 Ep. 2",
42 'description': 'md5:fa278820ad90f08ea187f9458316ac74',
43 'age_limit': 15,
44 'timestamp': 1600770600,
45 'upload_date': '20200922',
46 'uploader': 'Channel 10',
47 'uploader_id': '2199827728001'
48 },
49 'params': {
50 'skip_download': True,
51 }
52 }, {
53 'url': 'https://10play.com.au/how-to-stay-married/web-extras/season-1/terrys-talks-ep-1-embracing-change/tpv190915ylupc',
54 'only_matching': True,
55 }]
56 _GEO_BYPASS = False
57
58 _AUS_AGES = {
59 'G': 0,
60 'PG': 15,
61 'M': 15,
62 'MA': 15,
63 'MA15+': 15,
64 'R': 18,
65 'X': 18
66 }
67
68 def _get_bearer_token(self, video_id):
69 username, password = self._get_login_info()
70 if username is None or password is None:
71 self.raise_login_required('Your 10play account\'s details must be provided with --username and --password.')
72 _timestamp = datetime.now().strftime('%Y%m%d000000')
73 _auth_header = base64.b64encode(_timestamp.encode('ascii')).decode('ascii')
74 data = self._download_json('https://10play.com.au/api/user/auth', video_id, 'Getting bearer token', headers={
75 'X-Network-Ten-Auth': _auth_header,
76 }, data=urlencode_postdata({
77 'email': username,
78 'password': password,
79 }))
80 return 'Bearer ' + data['jwt']['accessToken']
81
82 def _real_extract(self, url):
83 content_id = self._match_id(url)
84 data = self._download_json(
85 'https://10play.com.au/api/v1/videos/' + content_id, content_id)
86 headers = {}
87
88 if data.get('memberGated') is True:
89 _token = self._get_bearer_token(content_id)
90 headers = {'Authorization': _token}
91
92 _video_url = self._download_json(
93 data.get('playbackApiEndpoint'), content_id, 'Downloading video JSON',
94 headers=headers).get('source')
95 m3u8_url = self._request_webpage(HEADRequest(
96 _video_url), content_id).url
97 if '10play-not-in-oz' in m3u8_url:
98 self.raise_geo_restricted(countries=['AU'])
99 formats = self._extract_m3u8_formats(m3u8_url, content_id, 'mp4')
100
101 return {
102 'formats': formats,
103 'subtitles': {'en': [{'url': data.get('captionUrl')}]} if data.get('captionUrl') else None,
104 'id': data.get('altId') or content_id,
105 'duration': data.get('duration'),
106 'title': data.get('subtitle'),
107 'alt_title': data.get('title'),
108 'description': data.get('description'),
109 'age_limit': self._AUS_AGES.get(data.get('classification')),
110 'series': data.get('tvShow'),
111 'season': int_or_none(data.get('season')),
112 'episode_number': int_or_none(data.get('episode')),
113 'timestamp': data.get('published'),
114 'thumbnail': data.get('imageUrl'),
115 'uploader': 'Channel 10',
116 'uploader_id': '2199827728001',
117 }
118
119
120 class TenPlaySeasonIE(InfoExtractor):
121 _VALID_URL = r'https?://(?:www\.)?10play\.com\.au/(?P<show>[^/?#]+)/episodes/(?P<season>[^/?#]+)/?(?:$|[?#])'
122 _TESTS = [{
123 'url': 'https://10play.com.au/masterchef/episodes/season-14',
124 'info_dict': {
125 'title': 'Season 14',
126 'id': 'MjMyOTIy',
127 },
128 'playlist_mincount': 64,
129 }, {
130 'url': 'https://10play.com.au/the-bold-and-the-beautiful-fast-tracked/episodes/season-2022',
131 'info_dict': {
132 'title': 'Season 2022',
133 'id': 'Mjc0OTIw',
134 },
135 'playlist_mincount': 256,
136 }]
137
138 def _entries(self, load_more_url, display_id=None):
139 skip_ids = []
140 for page in itertools.count(1):
141 episodes_carousel = self._download_json(
142 load_more_url, display_id, query={'skipIds[]': skip_ids},
143 note=f'Fetching episodes page {page}')
144
145 episodes_chunk = episodes_carousel['items']
146 skip_ids.extend(ep['id'] for ep in episodes_chunk)
147
148 for ep in episodes_chunk:
149 yield ep['cardLink']
150 if not episodes_carousel['hasMore']:
151 break
152
153 def _real_extract(self, url):
154 show, season = self._match_valid_url(url).group('show', 'season')
155 season_info = self._download_json(
156 f'https://10play.com.au/api/shows/{show}/episodes/{season}', f'{show}/{season}')
157
158 episodes_carousel = traverse_obj(season_info, (
159 'content', 0, 'components', (
160 lambda _, v: v['title'].lower() == 'episodes',
161 (..., {dict}),
162 )), get_all=False) or {}
163
164 playlist_id = episodes_carousel['tpId']
165
166 return self.playlist_from_matches(
167 self._entries(urljoin(url, episodes_carousel['loadMoreUrl']), playlist_id),
168 playlist_id, traverse_obj(season_info, ('content', 0, 'title', {str})),
169 getter=functools.partial(urljoin, url))