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