]>
Commit | Line | Data |
---|---|---|
8991844e SS |
1 | # coding: utf-8 |
2 | from __future__ import unicode_literals | |
3 | ||
db77c49c AG |
4 | import datetime |
5 | import math | |
6 | import random | |
29f7c58a | 7 | import time |
8 | import uuid | |
9 | ||
8991844e | 10 | from .common import InfoExtractor |
29f7c58a | 11 | from ..compat import compat_HTTPError |
12 | from ..utils import ( | |
13 | ExtractorError, | |
14 | int_or_none, | |
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 | ||
78 | def _login(self, username, password): | |
79 | if username.lower() == 'token' and len(password) > 1198: | |
80 | return password | |
81 | elif len(username) != 10 or not username.isdigit(): | |
82 | raise ExtractorError(f'Invalid username/password; {self._LOGIN_HINT}') | |
83 | ||
84 | self.report_login() | |
85 | data = '''{"mobileNumber":"%s","channelPartnerID":"MSMIND","country":"IN","timestamp":"%s", | |
86 | "otpSize":6,"loginType":"REGISTERORSIGNIN","isMobileMandatory":true} | |
87 | ''' % (username, datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%MZ")) | |
88 | otp_request_json = self._download_json( | |
89 | 'https://apiv2.sonyliv.com/AGL/1.6/A/ENG/WEB/IN/HR/CREATEOTP-V2', | |
90 | None, note='Sending OTP', data=data.encode(), headers=self._HEADERS) | |
91 | if otp_request_json['resultCode'] == 'KO': | |
92 | raise ExtractorError(otp_request_json['message'], expected=True) | |
93 | otp_code = self._get_tfa_info('OTP') | |
94 | data = '''{"channelPartnerID":"MSMIND","mobileNumber":"%s","country":"IN","otp":"%s", | |
95 | "dmaId":"IN","ageConfirmation":true,"timestamp":"%s","isMobileMandatory":true} | |
96 | ''' % (username, otp_code, datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%MZ")) | |
97 | otp_verify_json = self._download_json( | |
98 | 'https://apiv2.sonyliv.com/AGL/2.0/A/ENG/WEB/IN/HR/CONFIRMOTP-V2', | |
99 | None, note='Verifying OTP', data=data.encode(), headers=self._HEADERS) | |
100 | if otp_verify_json['resultCode'] == 'KO': | |
101 | raise ExtractorError(otp_request_json['message'], expected=True) | |
102 | return otp_verify_json['resultObj']['accessToken'] | |
77426a08 | 103 | |
29f7c58a | 104 | def _call_api(self, version, path, video_id): |
29f7c58a | 105 | try: |
106 | return self._download_json( | |
107 | 'https://apiv2.sonyliv.com/AGL/%s/A/ENG/WEB/%s' % (version, path), | |
db77c49c | 108 | video_id, headers=self._HEADERS)['resultObj'] |
29f7c58a | 109 | except ExtractorError as e: |
db77c49c AG |
110 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 406 and self._parse_json( |
111 | e.cause.read().decode(), video_id)['message'] == 'Please subscribe to watch this content': | |
112 | self.raise_login_required(self._LOGIN_HINT, method=None) | |
29f7c58a | 113 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403: |
114 | message = self._parse_json( | |
115 | e.cause.read().decode(), video_id)['message'] | |
116 | if message == 'Geoblocked Country': | |
117 | self.raise_geo_restricted(countries=self._GEO_COUNTRIES) | |
118 | raise ExtractorError(message) | |
119 | raise | |
120 | ||
121 | def _real_initialize(self): | |
db77c49c AG |
122 | self._HEADERS['security_token'] = self._call_api('1.4', 'ALL/GETTOKEN', None) |
123 | username, password = self._get_login_info() | |
124 | if username: | |
125 | self._HEADERS['device_id'] = self._get_device_id() | |
126 | self._HEADERS['content-type'] = 'application/json' | |
127 | self._HEADERS['authorization'] = self._login(username, password) | |
8991844e SS |
128 | |
129 | def _real_extract(self, url): | |
29f7c58a | 130 | video_id = self._match_id(url) |
131 | content = self._call_api( | |
132 | '1.5', 'IN/CONTENT/VIDEOURL/VOD/' + video_id, video_id) | |
a06916d9 | 133 | if not self.get_param('allow_unplayable_formats') and content.get('isEncrypted'): |
88acdbc2 | 134 | self.report_drm(video_id) |
29f7c58a | 135 | dash_url = content['videoURL'] |
136 | headers = { | |
137 | 'x-playback-session-id': '%s-%d' % (uuid.uuid4().hex, time.time() * 1000) | |
138 | } | |
139 | formats = self._extract_mpd_formats( | |
140 | dash_url, video_id, mpd_id='dash', headers=headers, fatal=False) | |
141 | formats.extend(self._extract_m3u8_formats( | |
142 | dash_url.replace('.mpd', '.m3u8').replace('/DASH/', '/HLS/'), | |
143 | video_id, 'mp4', m3u8_id='hls', headers=headers, fatal=False)) | |
144 | for f in formats: | |
145 | f.setdefault('http_headers', {}).update(headers) | |
146 | self._sort_formats(formats) | |
147 | ||
148 | metadata = self._call_api( | |
149 | '1.6', 'IN/DETAIL/' + video_id, video_id)['containers'][0]['metadata'] | |
c28cfda8 | 150 | title = metadata['episodeTitle'] |
09d18ad0 A |
151 | subtitles = {} |
152 | for sub in content.get('subtitle', []): | |
153 | sub_url = sub.get('subtitleUrl') | |
154 | if not sub_url: | |
155 | continue | |
156 | subtitles.setdefault(sub.get('subtitleLanguageName', 'ENG'), []).append({ | |
157 | 'url': sub_url, | |
158 | }) | |
29f7c58a | 159 | return { |
160 | 'id': video_id, | |
161 | 'title': title, | |
162 | 'formats': formats, | |
163 | 'thumbnail': content.get('posterURL'), | |
164 | 'description': metadata.get('longDescription') or metadata.get('shortDescription'), | |
165 | 'timestamp': int_or_none(metadata.get('creationDate'), 1000), | |
166 | 'duration': int_or_none(metadata.get('duration')), | |
167 | 'season_number': int_or_none(metadata.get('season')), | |
c28cfda8 | 168 | 'series': metadata.get('title'), |
29f7c58a | 169 | 'episode_number': int_or_none(metadata.get('episodeNumber')), |
170 | 'release_year': int_or_none(metadata.get('year')), | |
09d18ad0 | 171 | 'subtitles': subtitles, |
29f7c58a | 172 | } |
135e6b93 A |
173 | |
174 | ||
175 | class SonyLIVSeriesIE(InfoExtractor): | |
176 | _VALID_URL = r'https?://(?:www\.)?sonyliv\.com/shows/[^/?#&]+-(?P<id>\d{10})$' | |
177 | _TESTS = [{ | |
178 | 'url': 'https://www.sonyliv.com/shows/adaalat-1700000091', | |
179 | 'playlist_mincount': 456, | |
180 | 'info_dict': { | |
181 | 'id': '1700000091', | |
182 | }, | |
183 | }] | |
184 | _API_SHOW_URL = "https://apiv2.sonyliv.com/AGL/1.9/R/ENG/WEB/IN/DL/DETAIL/{}?kids_safe=false&from=0&to=49" | |
185 | _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" | |
186 | _API_SECURITY_URL = 'https://apiv2.sonyliv.com/AGL/1.4/A/ENG/WEB/ALL/GETTOKEN' | |
187 | ||
188 | def _entries(self, show_id): | |
189 | headers = { | |
190 | 'Accept': 'application/json, text/plain, */*', | |
191 | 'Referer': 'https://www.sonyliv.com', | |
192 | } | |
193 | headers['security_token'] = self._download_json( | |
194 | self._API_SECURITY_URL, video_id=show_id, headers=headers, | |
195 | note='Downloading security token')['resultObj'] | |
196 | seasons = try_get( | |
197 | self._download_json(self._API_SHOW_URL.format(show_id), video_id=show_id, headers=headers), | |
198 | lambda x: x['resultObj']['containers'][0]['containers'], list) | |
199 | for season in seasons or []: | |
200 | season_id = season['id'] | |
201 | episodes = try_get( | |
202 | self._download_json(self._API_EPISODES_URL.format(season_id), video_id=season_id, headers=headers), | |
203 | lambda x: x['resultObj']['containers'][0]['containers'], list) | |
204 | for episode in episodes or []: | |
205 | video_id = episode.get('id') | |
206 | yield self.url_result('sonyliv:%s' % video_id, ie=SonyLIVIE.ie_key(), video_id=video_id) | |
207 | ||
208 | def _real_extract(self, url): | |
209 | show_id = self._match_id(url) | |
210 | return self.playlist_result(self._entries(show_id), playlist_id=show_id) |