[ie/twitter:spaces] Support video spaces (#10789)
[yt-dlp3.git] / yt_dlp / extractor / sevenplus.py
blob6e1fbe7135f2f74a2cb560a5bb6c7dc0de2c6354
1 import json
2 import re
4 from .brightcove import BrightcoveNewBaseIE
5 from ..networking.exceptions import HTTPError
6 from ..utils import (
7 ExtractorError,
8 try_get,
9 update_url_query,
13 class SevenPlusIE(BrightcoveNewBaseIE):
14 IE_NAME = '7plus'
15 _VALID_URL = r'https?://(?:www\.)?7plus\.com\.au/(?P<path>[^?]+\?.*?\bepisode-id=(?P<id>[^&#]+))'
16 _TESTS = [{
17 'url': 'https://7plus.com.au/MTYS?episode-id=MTYS7-003',
18 'info_dict': {
19 'id': 'MTYS7-003',
20 'ext': 'mp4',
21 'title': 'S7 E3 - Wind Surf',
22 'description': 'md5:29c6a69f21accda7601278f81b46483d',
23 'uploader_id': '5303576322001',
24 'upload_date': '20171201',
25 'timestamp': 1512106377,
26 'series': 'Mighty Ships',
27 'season_number': 7,
28 'episode_number': 3,
29 'episode': 'Wind Surf',
31 'params': {
32 'skip_download': True,
34 }, {
35 'url': 'https://7plus.com.au/UUUU?episode-id=AUMS43-001',
36 'only_matching': True,
39 def _real_initialize(self):
40 self.token = None
42 cookies = self._get_cookies('https://7plus.com.au')
43 api_key = next((x for x in cookies if x.startswith('glt_')), '')[4:]
44 if not api_key: # Cookies are signed out, skip login
45 return
47 login_resp = self._download_json(
48 'https://login.7plus.com.au/accounts.getJWT', None, 'Logging in', fatal=False,
49 query={
50 'APIKey': api_key,
51 'sdk': 'js_latest',
52 'login_token': cookies[f'glt_{api_key}'].value,
53 'authMode': 'cookie',
54 'pageURL': 'https://7plus.com.au/',
55 'sdkBuild': '12471',
56 'format': 'json',
57 }) or {}
59 if 'errorMessage' in login_resp:
60 self.report_warning(f'Unable to login: 7plus said: {login_resp["errorMessage"]}')
61 return
62 id_token = login_resp.get('id_token')
63 if not id_token:
64 self.report_warning('Unable to login: Could not extract id token')
65 return
67 token_resp = self._download_json(
68 'https://7plus.com.au/auth/token', None, 'Getting auth token', fatal=False,
69 headers={'Content-Type': 'application/json'}, data=json.dumps({
70 'idToken': id_token,
71 'platformId': 'web',
72 'regSource': '7plus',
73 }).encode()) or {}
74 self.token = token_resp.get('token')
75 if not self.token:
76 self.report_warning('Unable to log in: Could not extract auth token')
78 def _real_extract(self, url):
79 path, episode_id = self._match_valid_url(url).groups()
81 headers = {}
82 if self.token:
83 headers['Authorization'] = f'Bearer {self.token}'
85 try:
86 media = self._download_json(
87 'https://videoservice.swm.digital/playback', episode_id, query={
88 'appId': '7plus',
89 'deviceType': 'web',
90 'platformType': 'web',
91 'accountId': 5303576322001,
92 'referenceId': 'ref:' + episode_id,
93 'deliveryId': 'csai',
94 'videoType': 'vod',
95 }, headers=headers)['media']
96 except ExtractorError as e:
97 if isinstance(e.cause, HTTPError) and e.cause.status == 403:
98 raise ExtractorError(self._parse_json(
99 e.cause.response.read().decode(), episode_id)[0]['error_code'], expected=True)
100 raise
102 for source in media.get('sources', {}):
103 src = source.get('src')
104 if not src:
105 continue
106 source['src'] = update_url_query(src, {'rule': ''})
108 info = self._parse_brightcove_metadata(media, episode_id)
110 content = self._download_json(
111 'https://component-cdn.swm.digital/content/' + path,
112 episode_id, headers={
113 'market-id': 4,
114 }, fatal=False) or {}
115 for item in content.get('items', {}):
116 if item.get('componentData', {}).get('componentType') == 'infoPanel':
117 for src_key, dst_key in [('title', 'title'), ('shortSynopsis', 'description')]:
118 value = item.get(src_key)
119 if value:
120 info[dst_key] = value
121 info['series'] = try_get(
122 item, lambda x: x['seriesLogo']['name'], str)
123 mobj = re.search(r'^S(\d+)\s+E(\d+)\s+-\s+(.+)$', info['title'])
124 if mobj:
125 info.update({
126 'season_number': int(mobj.group(1)),
127 'episode_number': int(mobj.group(2)),
128 'episode': mobj.group(3),
131 return info