]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/tenplay.py
[ie/youtube] Improve detection of faulty HLS formats (#8646)
[yt-dlp.git] / yt_dlp / extractor / tenplay.py
CommitLineData
74e001af 1import base64
88a99c87
MV
2import functools
3import itertools
4from datetime import datetime
74e001af 5
dd90451f 6from .common import InfoExtractor
3d2623a8 7from ..networking import HEADRequest
88a99c87 8from ..utils import int_or_none, traverse_obj, urlencode_postdata, urljoin
dd90451f
RA
9
10
11class TenPlayIE(InfoExtractor):
c97f5e93 12 _VALID_URL = r'https?://(?:www\.)?10play\.com\.au/(?:[^/]+/)+(?P<id>tpv\d{6}[a-z]{5})'
74e001af 13 _NETRC_MACHINE = '10play'
c97f5e93 14 _TESTS = [{
dc57e74a 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 }, {
74e001af 37 'url': 'https://10play.com.au/todd-sampsons-body-hack/episodes/season-4/episode-7/tpv200921kvngh',
dd90451f 38 'info_dict': {
74e001af 39 'id': '6192880312001',
dd90451f 40 'ext': 'mp4',
74e001af 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'
dd90451f
RA
48 },
49 'params': {
dd90451f
RA
50 'skip_download': True,
51 }
c97f5e93
S
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 }]
29f7c58a 56 _GEO_BYPASS = False
74e001af 57
58 _AUS_AGES = {
59 'G': 0,
60 'PG': 15,
61 'M': 15,
62 'MA': 15,
1bd3639f 63 'MA15+': 15,
74e001af 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 }))
9222c381 80 return 'Bearer ' + data['jwt']['accessToken']
dd90451f
RA
81
82 def _real_extract(self, url):
83 content_id = self._match_id(url)
84 data = self._download_json(
74e001af 85 'https://10play.com.au/api/v1/videos/' + content_id, content_id)
dc57e74a 86 headers = {}
87
88 if data.get('memberGated') is True:
89 _token = self._get_bearer_token(content_id)
90 headers = {'Authorization': _token}
91
74e001af 92 _video_url = self._download_json(
93 data.get('playbackApiEndpoint'), content_id, 'Downloading video JSON',
dc57e74a 94 headers=headers).get('source')
29f7c58a 95 m3u8_url = self._request_webpage(HEADRequest(
3d2623a8 96 _video_url), content_id).url
29f7c58a 97 if '10play-not-in-oz' in m3u8_url:
98 self.raise_geo_restricted(countries=['AU'])
74e001af 99 formats = self._extract_m3u8_formats(m3u8_url, content_id, 'mp4')
dd90451f
RA
100
101 return {
29f7c58a 102 'formats': formats,
dc57e74a 103 'subtitles': {'en': [{'url': data.get('captionUrl')}]} if data.get('captionUrl') else None,
74e001af 104 'id': data.get('altId') or content_id,
dc57e74a 105 'duration': data.get('duration'),
106 'title': data.get('subtitle'),
107 'alt_title': data.get('title'),
74e001af 108 'description': data.get('description'),
1bd3639f 109 'age_limit': self._AUS_AGES.get(data.get('classification')),
dc57e74a 110 'series': data.get('tvShow'),
111 'season': int_or_none(data.get('season')),
112 'episode_number': int_or_none(data.get('episode')),
74e001af 113 'timestamp': data.get('published'),
114 'thumbnail': data.get('imageUrl'),
115 'uploader': 'Channel 10',
29f7c58a 116 'uploader_id': '2199827728001',
dd90451f 117 }
88a99c87
MV
118
119
120class 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))