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