]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/sevenplus.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / sevenplus.py
CommitLineData
e69585f8 1import json
4b7dd170
RA
2import re
3
f5a9e9df 4from .brightcove import BrightcoveNewBaseIE
3d2623a8 5from ..networking.exceptions import HTTPError
d9e2240f 6from ..utils import (
29f7c58a 7 ExtractorError,
d9e2240f
S
8 try_get,
9 update_url_query,
10)
4b7dd170
RA
11
12
f5a9e9df 13class SevenPlusIE(BrightcoveNewBaseIE):
4b7dd170
RA
14 IE_NAME = '7plus'
15 _VALID_URL = r'https?://(?:www\.)?7plus\.com\.au/(?P<path>[^?]+\?.*?\bepisode-id=(?P<id>[^&#]+))'
16 _TESTS = [{
d9e2240f 17 'url': 'https://7plus.com.au/MTYS?episode-id=MTYS7-003',
4b7dd170 18 'info_dict': {
d9e2240f 19 'id': 'MTYS7-003',
4b7dd170 20 'ext': 'mp4',
d9e2240f
S
21 'title': 'S7 E3 - Wind Surf',
22 'description': 'md5:29c6a69f21accda7601278f81b46483d',
4b7dd170 23 'uploader_id': '5303576322001',
d9e2240f
S
24 'upload_date': '20171201',
25 'timestamp': 1512106377,
26 'series': 'Mighty Ships',
27 'season_number': 7,
28 'episode_number': 3,
29 'episode': 'Wind Surf',
4b7dd170
RA
30 },
31 'params': {
4b7dd170 32 'skip_download': True,
add96eb9 33 },
4b7dd170
RA
34 }, {
35 'url': 'https://7plus.com.au/UUUU?episode-id=AUMS43-001',
36 'only_matching': True,
37 }]
38
e69585f8 39 def _real_initialize(self):
40 self.token = None
41
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
46
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 {}
58
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
66
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',
add96eb9 73 }).encode()) or {}
e69585f8 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')
77
4b7dd170 78 def _real_extract(self, url):
5ad28e7f 79 path, episode_id = self._match_valid_url(url).groups()
4b7dd170 80
e69585f8 81 headers = {}
82 if self.token:
83 headers['Authorization'] = f'Bearer {self.token}'
84
29f7c58a 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',
e69585f8 95 }, headers=headers)['media']
29f7c58a 96 except ExtractorError as e:
3d2623a8 97 if isinstance(e.cause, HTTPError) and e.cause.status == 403:
29f7c58a 98 raise ExtractorError(self._parse_json(
3d2623a8 99 e.cause.response.read().decode(), episode_id)[0]['error_code'], expected=True)
29f7c58a 100 raise
4b7dd170
RA
101
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': ''})
107
108 info = self._parse_brightcove_metadata(media, episode_id)
109
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
d9e2240f 121 info['series'] = try_get(
add96eb9 122 item, lambda x: x['seriesLogo']['name'], str)
d9e2240f
S
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),
129 })
4b7dd170
RA
130
131 return info