logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

sonyliv.py (10678B)


  1. import datetime as dt
  2. import itertools
  3. import json
  4. import math
  5. import random
  6. import time
  7. import uuid
  8. from .common import InfoExtractor
  9. from ..networking.exceptions import HTTPError
  10. from ..utils import (
  11. ExtractorError,
  12. int_or_none,
  13. jwt_decode_hs256,
  14. try_call,
  15. )
  16. from ..utils.traversal import traverse_obj
  17. class SonyLIVIE(InfoExtractor):
  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. '''
  25. _TESTS = [{
  26. 'url': 'https://www.sonyliv.com/shows/bachelors-delight-1700000113/achaari-cheese-toast-1000022678?watch=true',
  27. 'info_dict': {
  28. 'title': 'Achaari Cheese Toast',
  29. 'id': '1000022678',
  30. 'ext': 'mp4',
  31. 'upload_date': '20200411',
  32. 'description': 'md5:3957fa31d9309bf336ceb3f37ad5b7cb',
  33. 'timestamp': 1586632091,
  34. 'duration': 185,
  35. 'season_number': 1,
  36. 'series': 'Bachelors Delight',
  37. 'episode_number': 1,
  38. 'release_year': 2016,
  39. },
  40. 'params': {
  41. 'skip_download': True,
  42. },
  43. }, {
  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',
  57. 'only_matching': True,
  58. }]
  59. _GEO_COUNTRIES = ['IN']
  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. def _get_device_id(self):
  64. e = int(time.time() * 1000)
  65. t = list('xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx')
  66. for i, c in enumerate(t):
  67. n = int((e + 16 * random.random()) % 16) | 0
  68. e = math.floor(e / 16)
  69. if c == 'x':
  70. t[i] = str(n)
  71. elif c == 'y':
  72. t[i] = f'{3 & n | 8:x}'
  73. return ''.join(t) + '-' + str(int(time.time() * 1000))
  74. def _perform_login(self, username, password):
  75. self._HEADERS['device_id'] = self._get_device_id()
  76. self._HEADERS['content-type'] = 'application/json'
  77. if username.lower() == 'token' and try_call(lambda: jwt_decode_hs256(password)):
  78. self._HEADERS['authorization'] = password
  79. self.report_login()
  80. return
  81. elif len(username) != 10 or not username.isdigit():
  82. raise ExtractorError(f'Invalid username/password; {self._LOGIN_HINT}')
  83. self.report_login()
  84. otp_request_json = self._download_json(
  85. 'https://apiv2.sonyliv.com/AGL/1.6/A/ENG/WEB/IN/HR/CREATEOTP-V2',
  86. None, note='Sending OTP', headers=self._HEADERS, data=json.dumps({
  87. 'mobileNumber': username,
  88. 'channelPartnerID': 'MSMIND',
  89. 'country': 'IN',
  90. 'timestamp': dt.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),
  91. 'otpSize': 6,
  92. 'loginType': 'REGISTERORSIGNIN',
  93. 'isMobileMandatory': True,
  94. }).encode())
  95. if otp_request_json['resultCode'] == 'KO':
  96. raise ExtractorError(otp_request_json['message'], expected=True)
  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', headers=self._HEADERS, data=json.dumps({
  100. 'channelPartnerID': 'MSMIND',
  101. 'mobileNumber': username,
  102. 'country': 'IN',
  103. 'otp': self._get_tfa_info('OTP'),
  104. 'dmaId': 'IN',
  105. 'ageConfirmation': True,
  106. 'timestamp': dt.datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%MZ'),
  107. 'isMobileMandatory': True,
  108. }).encode())
  109. if otp_verify_json['resultCode'] == 'KO':
  110. raise ExtractorError(otp_request_json['message'], expected=True)
  111. self._HEADERS['authorization'] = otp_verify_json['resultObj']['accessToken']
  112. def _call_api(self, version, path, video_id):
  113. try:
  114. return self._download_json(
  115. f'https://apiv2.sonyliv.com/AGL/{version}/A/ENG/WEB/{path}',
  116. video_id, headers=self._HEADERS)['resultObj']
  117. except ExtractorError as e:
  118. if isinstance(e.cause, HTTPError) and e.cause.status == 406 and self._parse_json(
  119. e.cause.response.read().decode(), video_id)['message'] == 'Please subscribe to watch this content':
  120. self.raise_login_required(self._LOGIN_HINT, method=None)
  121. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  122. message = self._parse_json(
  123. e.cause.response.read().decode(), video_id)['message']
  124. if message == 'Geoblocked Country':
  125. self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
  126. raise ExtractorError(message)
  127. raise
  128. def _initialize_pre_login(self):
  129. self._HEADERS['security_token'] = self._call_api('1.4', 'ALL/GETTOKEN', None)
  130. def _real_extract(self, url):
  131. video_id = self._match_id(url)
  132. content = self._call_api(
  133. '1.5', 'IN/CONTENT/VIDEOURL/VOD/' + video_id, video_id)
  134. if not self.get_param('allow_unplayable_formats') and content.get('isEncrypted'):
  135. self.report_drm(video_id)
  136. dash_url = content['videoURL']
  137. headers = {
  138. 'x-playback-session-id': '%s-%d' % (uuid.uuid4().hex, time.time() * 1000),
  139. }
  140. formats = self._extract_mpd_formats(
  141. dash_url, video_id, mpd_id='dash', headers=headers, fatal=False)
  142. formats.extend(self._extract_m3u8_formats(
  143. dash_url.replace('.mpd', '.m3u8').replace('/DASH/', '/HLS/'),
  144. video_id, 'mp4', m3u8_id='hls', headers=headers, fatal=False))
  145. for f in formats:
  146. f.setdefault('http_headers', {}).update(headers)
  147. metadata = self._call_api(
  148. '1.6', 'IN/DETAIL/' + video_id, video_id)['containers'][0]['metadata']
  149. title = metadata['episodeTitle']
  150. subtitles = {}
  151. for sub in content.get('subtitle', []):
  152. sub_url = sub.get('subtitleUrl')
  153. if not sub_url:
  154. continue
  155. subtitles.setdefault(sub.get('subtitleLanguageName', 'ENG'), []).append({
  156. 'url': sub_url,
  157. })
  158. return {
  159. 'id': video_id,
  160. 'title': title,
  161. 'formats': formats,
  162. 'thumbnail': content.get('posterURL'),
  163. 'description': metadata.get('longDescription') or metadata.get('shortDescription'),
  164. 'timestamp': int_or_none(metadata.get('creationDate'), 1000),
  165. 'duration': int_or_none(metadata.get('duration')),
  166. 'season_number': int_or_none(metadata.get('season')),
  167. 'series': metadata.get('title'),
  168. 'episode_number': int_or_none(metadata.get('episodeNumber')),
  169. 'release_year': int_or_none(metadata.get('year')),
  170. 'subtitles': subtitles,
  171. }
  172. class SonyLIVSeriesIE(InfoExtractor):
  173. _VALID_URL = r'https?://(?:www\.)?sonyliv\.com/shows/[^/?#&]+-(?P<id>\d{10})/?(?:$|[?#])'
  174. _TESTS = [{
  175. 'url': 'https://www.sonyliv.com/shows/adaalat-1700000091',
  176. 'playlist_mincount': 452,
  177. 'info_dict': {
  178. 'id': '1700000091',
  179. },
  180. }, {
  181. 'url': 'https://www.sonyliv.com/shows/beyhadh-1700000007/',
  182. 'playlist_mincount': 358,
  183. 'info_dict': {
  184. 'id': '1700000007',
  185. },
  186. }]
  187. _API_BASE = 'https://apiv2.sonyliv.com/AGL'
  188. _SORT_ORDERS = ('asc', 'desc')
  189. def _entries(self, show_id, sort_order):
  190. headers = {
  191. 'Accept': 'application/json, text/plain, */*',
  192. 'Referer': 'https://www.sonyliv.com',
  193. }
  194. headers['security_token'] = self._download_json(
  195. f'{self._API_BASE}/1.4/A/ENG/WEB/ALL/GETTOKEN', show_id,
  196. 'Downloading security token', headers=headers)['resultObj']
  197. seasons = traverse_obj(self._download_json(
  198. f'{self._API_BASE}/1.9/R/ENG/WEB/IN/DL/DETAIL/{show_id}', show_id,
  199. 'Downloading series JSON', headers=headers, query={
  200. 'kids_safe': 'false',
  201. 'from': '0',
  202. 'to': '49',
  203. }), ('resultObj', 'containers', 0, 'containers', lambda _, v: int_or_none(v['id'])))
  204. if sort_order == 'desc':
  205. seasons = reversed(seasons)
  206. for season in seasons:
  207. season_id = str(season['id'])
  208. note = traverse_obj(season, ('metadata', 'title', {str})) or 'season'
  209. cursor = 0
  210. for page_num in itertools.count(1):
  211. episodes = traverse_obj(self._download_json(
  212. f'{self._API_BASE}/1.4/R/ENG/WEB/IN/CONTENT/DETAIL/BUNDLE/{season_id}',
  213. season_id, f'Downloading {note} page {page_num} JSON', headers=headers, query={
  214. 'from': str(cursor),
  215. 'to': str(cursor + 99),
  216. 'orderBy': 'episodeNumber',
  217. 'sortOrder': sort_order,
  218. }), ('resultObj', 'containers', 0, 'containers', lambda _, v: int_or_none(v['id'])))
  219. if not episodes:
  220. break
  221. for episode in episodes:
  222. video_id = str(episode['id'])
  223. yield self.url_result(f'sonyliv:{video_id}', SonyLIVIE, video_id)
  224. cursor += 100
  225. def _real_extract(self, url):
  226. show_id = self._match_id(url)
  227. sort_order = self._configuration_arg('sort_order', [self._SORT_ORDERS[0]])[0]
  228. if sort_order not in self._SORT_ORDERS:
  229. raise ValueError(
  230. f'Invalid sort order "{sort_order}". Allowed values are: {", ".join(self._SORT_ORDERS)}')
  231. return self.playlist_result(self._entries(show_id, sort_order), playlist_id=show_id)