]>
Commit | Line | Data |
---|---|---|
db77c49c | 1 | import datetime |
e5a998f3 | 2 | import json |
db77c49c AG |
3 | import math |
4 | import random | |
29f7c58a | 5 | import time |
6 | import uuid | |
7 | ||
8991844e | 8 | from .common import InfoExtractor |
3d2623a8 | 9 | from ..networking.exceptions import HTTPError |
29f7c58a | 10 | from ..utils import ( |
11 | ExtractorError, | |
12 | int_or_none, | |
4815d35c | 13 | jwt_decode_hs256, |
14 | try_call, | |
135e6b93 | 15 | try_get, |
29f7c58a | 16 | ) |
8991844e SS |
17 | |
18 | ||
19 | class SonyLIVIE(InfoExtractor): | |
135e6b93 A |
20 | _VALID_URL = r'''(?x) |
21 | (?: | |
22 | sonyliv:| | |
23 | https?://(?:www\.)?sonyliv\.com/(?:s(?:how|port)s/[^/]+|movies|clip|trailer|music-videos)/[^/?#&]+- | |
24 | ) | |
25 | (?P<id>\d+) | |
26 | ''' | |
77426a08 | 27 | _TESTS = [{ |
29f7c58a | 28 | 'url': 'https://www.sonyliv.com/shows/bachelors-delight-1700000113/achaari-cheese-toast-1000022678?watch=true', |
8991844e | 29 | 'info_dict': { |
c28cfda8 | 30 | 'title': 'Achaari Cheese Toast', |
29f7c58a | 31 | 'id': '1000022678', |
8991844e | 32 | 'ext': 'mp4', |
29f7c58a | 33 | 'upload_date': '20200411', |
34 | 'description': 'md5:3957fa31d9309bf336ceb3f37ad5b7cb', | |
35 | 'timestamp': 1586632091, | |
36 | 'duration': 185, | |
37 | 'season_number': 1, | |
c28cfda8 | 38 | 'series': 'Bachelors Delight', |
29f7c58a | 39 | 'episode_number': 1, |
40 | 'release_year': 2016, | |
8991844e SS |
41 | }, |
42 | 'params': { | |
43 | 'skip_download': True, | |
44 | }, | |
77426a08 | 45 | }, { |
29f7c58a | 46 | 'url': 'https://www.sonyliv.com/movies/tahalka-1000050121?watch=true', |
47 | 'only_matching': True, | |
48 | }, { | |
49 | 'url': 'https://www.sonyliv.com/clip/jigarbaaz-1000098925', | |
50 | 'only_matching': True, | |
51 | }, { | |
52 | 'url': 'https://www.sonyliv.com/trailer/sandwiched-forever-1000100286?watch=true', | |
53 | 'only_matching': True, | |
54 | }, { | |
55 | 'url': 'https://www.sonyliv.com/sports/india-tour-of-australia-2020-21-1700000286/cricket-hls-day-3-1st-test-aus-vs-ind-19-dec-2020-1000100959?watch=true', | |
56 | 'only_matching': True, | |
57 | }, { | |
58 | 'url': 'https://www.sonyliv.com/music-videos/yeh-un-dinon-ki-baat-hai-1000018779', | |
77426a08 S |
59 | 'only_matching': True, |
60 | }] | |
29f7c58a | 61 | _GEO_COUNTRIES = ['IN'] |
db77c49c AG |
62 | _HEADERS = {} |
63 | _LOGIN_HINT = 'Use "--username <mobile_number>" to login using OTP or "--username token --password <auth_token>" to login using auth token.' | |
64 | _NETRC_MACHINE = 'sonyliv' | |
65 | ||
66 | def _get_device_id(self): | |
67 | e = int(time.time() * 1000) | |
68 | t = list('xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx') | |
69 | for i, c in enumerate(t): | |
70 | n = int((e + 16 * random.random()) % 16) | 0 | |
71 | e = math.floor(e / 16) | |
72 | if c == 'x': | |
73 | t[i] = str(n) | |
74 | elif c == 'y': | |
75 | t[i] = '{:x}'.format(3 & n | 8) | |
76 | return ''.join(t) + '-' + str(int(time.time() * 1000)) | |
77 | ||
52efa4b3 | 78 | def _perform_login(self, username, password): |
79 | self._HEADERS['device_id'] = self._get_device_id() | |
80 | self._HEADERS['content-type'] = 'application/json' | |
81 | ||
4815d35c | 82 | if username.lower() == 'token' and try_call(lambda: jwt_decode_hs256(password)): |
52efa4b3 | 83 | self._HEADERS['authorization'] = password |
4815d35c | 84 | self.report_login() |
85 | return | |
db77c49c AG |
86 | elif len(username) != 10 or not username.isdigit(): |
87 | raise ExtractorError(f'Invalid username/password; {self._LOGIN_HINT}') | |
88 | ||
89 | self.report_login() | |
db77c49c AG |
90 | otp_request_json = self._download_json( |
91 | 'https://apiv2.sonyliv.com/AGL/1.6/A/ENG/WEB/IN/HR/CREATEOTP-V2', | |
e5a998f3 | 92 | None, note='Sending OTP', headers=self._HEADERS, data=json.dumps({ |
93 | 'mobileNumber': username, | |
94 | 'channelPartnerID': 'MSMIND', | |
95 | 'country': 'IN', | |
96 | 'timestamp': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'), | |
97 | 'otpSize': 6, | |
98 | 'loginType': 'REGISTERORSIGNIN', | |
99 | 'isMobileMandatory': True, | |
100 | }).encode()) | |
db77c49c AG |
101 | if otp_request_json['resultCode'] == 'KO': |
102 | raise ExtractorError(otp_request_json['message'], expected=True) | |
e5a998f3 | 103 | |
db77c49c AG |
104 | otp_verify_json = self._download_json( |
105 | 'https://apiv2.sonyliv.com/AGL/2.0/A/ENG/WEB/IN/HR/CONFIRMOTP-V2', | |
e5a998f3 | 106 | None, note='Verifying OTP', headers=self._HEADERS, data=json.dumps({ |
107 | 'channelPartnerID': 'MSMIND', | |
108 | 'mobileNumber': username, | |
109 | 'country': 'IN', | |
110 | 'otp': self._get_tfa_info('OTP'), | |
111 | 'dmaId': 'IN', | |
112 | 'ageConfirmation': True, | |
113 | 'timestamp': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'), | |
114 | 'isMobileMandatory': True, | |
115 | }).encode()) | |
db77c49c AG |
116 | if otp_verify_json['resultCode'] == 'KO': |
117 | raise ExtractorError(otp_request_json['message'], expected=True) | |
52efa4b3 | 118 | self._HEADERS['authorization'] = otp_verify_json['resultObj']['accessToken'] |
77426a08 | 119 | |
29f7c58a | 120 | def _call_api(self, version, path, video_id): |
29f7c58a | 121 | try: |
122 | return self._download_json( | |
123 | 'https://apiv2.sonyliv.com/AGL/%s/A/ENG/WEB/%s' % (version, path), | |
db77c49c | 124 | video_id, headers=self._HEADERS)['resultObj'] |
29f7c58a | 125 | except ExtractorError as e: |
3d2623a8 | 126 | if isinstance(e.cause, HTTPError) and e.cause.status == 406 and self._parse_json( |
127 | e.cause.response.read().decode(), video_id)['message'] == 'Please subscribe to watch this content': | |
db77c49c | 128 | self.raise_login_required(self._LOGIN_HINT, method=None) |
3d2623a8 | 129 | if isinstance(e.cause, HTTPError) and e.cause.status == 403: |
29f7c58a | 130 | message = self._parse_json( |
3d2623a8 | 131 | e.cause.response.read().decode(), video_id)['message'] |
29f7c58a | 132 | if message == 'Geoblocked Country': |
133 | self.raise_geo_restricted(countries=self._GEO_COUNTRIES) | |
134 | raise ExtractorError(message) | |
135 | raise | |
136 | ||
52efa4b3 | 137 | def _initialize_pre_login(self): |
db77c49c | 138 | self._HEADERS['security_token'] = self._call_api('1.4', 'ALL/GETTOKEN', None) |
8991844e SS |
139 | |
140 | def _real_extract(self, url): | |
29f7c58a | 141 | video_id = self._match_id(url) |
142 | content = self._call_api( | |
143 | '1.5', 'IN/CONTENT/VIDEOURL/VOD/' + video_id, video_id) | |
a06916d9 | 144 | if not self.get_param('allow_unplayable_formats') and content.get('isEncrypted'): |
88acdbc2 | 145 | self.report_drm(video_id) |
29f7c58a | 146 | dash_url = content['videoURL'] |
147 | headers = { | |
148 | 'x-playback-session-id': '%s-%d' % (uuid.uuid4().hex, time.time() * 1000) | |
149 | } | |
150 | formats = self._extract_mpd_formats( | |
151 | dash_url, video_id, mpd_id='dash', headers=headers, fatal=False) | |
152 | formats.extend(self._extract_m3u8_formats( | |
153 | dash_url.replace('.mpd', '.m3u8').replace('/DASH/', '/HLS/'), | |
154 | video_id, 'mp4', m3u8_id='hls', headers=headers, fatal=False)) | |
155 | for f in formats: | |
156 | f.setdefault('http_headers', {}).update(headers) | |
29f7c58a | 157 | |
158 | metadata = self._call_api( | |
159 | '1.6', 'IN/DETAIL/' + video_id, video_id)['containers'][0]['metadata'] | |
c28cfda8 | 160 | title = metadata['episodeTitle'] |
09d18ad0 A |
161 | subtitles = {} |
162 | for sub in content.get('subtitle', []): | |
163 | sub_url = sub.get('subtitleUrl') | |
164 | if not sub_url: | |
165 | continue | |
166 | subtitles.setdefault(sub.get('subtitleLanguageName', 'ENG'), []).append({ | |
167 | 'url': sub_url, | |
168 | }) | |
29f7c58a | 169 | return { |
170 | 'id': video_id, | |
171 | 'title': title, | |
172 | 'formats': formats, | |
173 | 'thumbnail': content.get('posterURL'), | |
174 | 'description': metadata.get('longDescription') or metadata.get('shortDescription'), | |
175 | 'timestamp': int_or_none(metadata.get('creationDate'), 1000), | |
176 | 'duration': int_or_none(metadata.get('duration')), | |
177 | 'season_number': int_or_none(metadata.get('season')), | |
c28cfda8 | 178 | 'series': metadata.get('title'), |
29f7c58a | 179 | 'episode_number': int_or_none(metadata.get('episodeNumber')), |
180 | 'release_year': int_or_none(metadata.get('year')), | |
09d18ad0 | 181 | 'subtitles': subtitles, |
29f7c58a | 182 | } |
135e6b93 A |
183 | |
184 | ||
185 | class SonyLIVSeriesIE(InfoExtractor): | |
186 | _VALID_URL = r'https?://(?:www\.)?sonyliv\.com/shows/[^/?#&]+-(?P<id>\d{10})$' | |
187 | _TESTS = [{ | |
188 | 'url': 'https://www.sonyliv.com/shows/adaalat-1700000091', | |
189 | 'playlist_mincount': 456, | |
190 | 'info_dict': { | |
191 | 'id': '1700000091', | |
192 | }, | |
193 | }] | |
194 | _API_SHOW_URL = "https://apiv2.sonyliv.com/AGL/1.9/R/ENG/WEB/IN/DL/DETAIL/{}?kids_safe=false&from=0&to=49" | |
195 | _API_EPISODES_URL = "https://apiv2.sonyliv.com/AGL/1.4/R/ENG/WEB/IN/CONTENT/DETAIL/BUNDLE/{}?from=0&to=1000&orderBy=episodeNumber&sortOrder=asc" | |
196 | _API_SECURITY_URL = 'https://apiv2.sonyliv.com/AGL/1.4/A/ENG/WEB/ALL/GETTOKEN' | |
197 | ||
198 | def _entries(self, show_id): | |
199 | headers = { | |
200 | 'Accept': 'application/json, text/plain, */*', | |
201 | 'Referer': 'https://www.sonyliv.com', | |
202 | } | |
203 | headers['security_token'] = self._download_json( | |
204 | self._API_SECURITY_URL, video_id=show_id, headers=headers, | |
205 | note='Downloading security token')['resultObj'] | |
206 | seasons = try_get( | |
207 | self._download_json(self._API_SHOW_URL.format(show_id), video_id=show_id, headers=headers), | |
208 | lambda x: x['resultObj']['containers'][0]['containers'], list) | |
209 | for season in seasons or []: | |
210 | season_id = season['id'] | |
211 | episodes = try_get( | |
212 | self._download_json(self._API_EPISODES_URL.format(season_id), video_id=season_id, headers=headers), | |
213 | lambda x: x['resultObj']['containers'][0]['containers'], list) | |
214 | for episode in episodes or []: | |
215 | video_id = episode.get('id') | |
216 | yield self.url_result('sonyliv:%s' % video_id, ie=SonyLIVIE.ie_key(), video_id=video_id) | |
217 | ||
218 | def _real_extract(self, url): | |
219 | show_id = self._match_id(url) | |
220 | return self.playlist_result(self._entries(show_id), playlist_id=show_id) |