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