zee5.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import json
  2. import time
  3. import uuid
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. jwt_decode_hs256,
  9. parse_age_limit,
  10. str_or_none,
  11. try_call,
  12. try_get,
  13. unified_strdate,
  14. unified_timestamp,
  15. url_or_none,
  16. )
  17. class Zee5IE(InfoExtractor):
  18. _VALID_URL = r'''(?x)
  19. (?:
  20. zee5:|
  21. https?://(?:www\.)?zee5\.com/(?:[^#?]+/)?
  22. (?:
  23. (?:tv-shows|kids|web-series|zee5originals)(?:/[^#/?]+){3}
  24. |(?:movies|kids|videos|news|music-videos)/(?!kids-shows)[^#/?]+
  25. )/(?P<display_id>[^#/?]+)/
  26. )
  27. (?P<id>[^#/?]+)/?(?:$|[?#])
  28. '''
  29. _TESTS = [{
  30. 'url': 'https://www.zee5.com/movies/details/adavari-matalaku-ardhale-verule/0-0-movie_1143162669',
  31. 'info_dict': {
  32. 'id': '0-0-movie_1143162669',
  33. 'ext': 'mp4',
  34. 'display_id': 'adavari-matalaku-ardhale-verule',
  35. 'title': 'Adavari Matalaku Ardhale Verule',
  36. 'duration': 9360,
  37. 'description': str,
  38. 'alt_title': 'Adavari Matalaku Ardhale Verule',
  39. 'uploader': 'Zee Entertainment Enterprises Ltd',
  40. 'release_date': '20070427',
  41. 'upload_date': '20070427',
  42. 'timestamp': 1177632000,
  43. 'thumbnail': r're:^https?://.*\.jpg$',
  44. 'episode_number': 0,
  45. 'episode': 'Episode 0',
  46. 'tags': list,
  47. },
  48. 'params': {
  49. 'format': 'bv',
  50. },
  51. }, {
  52. 'url': 'https://www.zee5.com/kids/kids-shows/bandbudh-aur-budbak/0-6-1899/yoga-se-hoga-bandbudh-aur-budbak/0-1-239839',
  53. 'info_dict': {
  54. 'id': '0-1-239839',
  55. 'ext': 'mp4',
  56. 'display_id': 'yoga-se-hoga-bandbudh-aur-budbak',
  57. 'title': 'Yoga Se Hoga-Bandbudh aur Budbak',
  58. 'duration': 659,
  59. 'description': str,
  60. 'alt_title': 'Yoga Se Hoga-Bandbudh aur Budbak',
  61. 'uploader': 'Zee Entertainment Enterprises Ltd',
  62. 'release_date': '20150101',
  63. 'upload_date': '20150101',
  64. 'timestamp': 1420070400,
  65. 'thumbnail': r're:^https?://.*\.jpg$',
  66. 'series': 'Bandbudh Aur Budbak',
  67. 'season_number': 1,
  68. 'episode_number': 1,
  69. 'episode': 'Episode 1',
  70. 'season': 'Season 1',
  71. 'tags': list,
  72. },
  73. 'params': {
  74. 'format': 'bv',
  75. },
  76. }, {
  77. 'url': 'https://www.zee5.com/hi/tv-shows/details/kundali-bhagya/0-6-366/kundali-bhagya-march-08-2021/0-1-manual_7g9jv1os7730?country=IN',
  78. 'only_matching': True,
  79. }, {
  80. 'url': 'https://www.zee5.com/global/hi/tv-shows/details/kundali-bhagya/0-6-366/kundali-bhagya-march-08-2021/0-1-manual_7g9jv1os7730',
  81. 'only_matching': True,
  82. }, {
  83. 'url': 'https://www.zee5.com/web-series/details/mithya/0-6-4z587408/maine-dekhi-hai-uski-mrityu/0-1-6z587412',
  84. 'only_matching': True,
  85. }, {
  86. 'url': 'https://www.zee5.com/kids/kids-movies/maya-bommalu/0-0-movie_1040370005',
  87. 'only_matching': True,
  88. }, {
  89. 'url': 'https://www.zee5.com/news/details/jana-sena-chief-pawan-kalyan-shows-slippers-to-ysrcp-leaders/0-0-newsauto_6ettj4242oo0',
  90. 'only_matching': True,
  91. }, {
  92. 'url': 'https://www.zee5.com/music-videos/details/adhento-gaani-vunnapaatuga-jersey-nani-shraddha-srinath/0-0-56973',
  93. 'only_matching': True,
  94. }]
  95. _DEVICE_ID = str(uuid.uuid4())
  96. _USER_TOKEN = None
  97. _LOGIN_HINT = 'Use "--username <mobile_number>" to login using otp or "--username token" and "--password <user_token>" to login using user token.'
  98. _NETRC_MACHINE = 'zee5'
  99. _GEO_COUNTRIES = ['IN']
  100. _USER_COUNTRY = None
  101. def _perform_login(self, username, password):
  102. if len(username) == 10 and username.isdigit() and self._USER_TOKEN is None:
  103. self.report_login()
  104. otp_request_json = self._download_json(f'https://b2bapi.zee5.com/device/sendotp_v1.php?phoneno=91{username}',
  105. None, note='Sending OTP')
  106. if otp_request_json['code'] == 0:
  107. self.to_screen(otp_request_json['message'])
  108. else:
  109. raise ExtractorError(otp_request_json['message'], expected=True)
  110. otp_code = self._get_tfa_info('OTP')
  111. otp_verify_json = self._download_json(f'https://b2bapi.zee5.com/device/verifyotp_v1.php?phoneno=91{username}&otp={otp_code}&guest_token={self._DEVICE_ID}&platform=web',
  112. None, note='Verifying OTP', fatal=False)
  113. if not otp_verify_json:
  114. raise ExtractorError('Unable to verify OTP.', expected=True)
  115. self._USER_TOKEN = otp_verify_json.get('token')
  116. if not self._USER_TOKEN:
  117. raise ExtractorError(otp_request_json['message'], expected=True)
  118. elif username.lower() == 'token' and try_call(lambda: jwt_decode_hs256(password)):
  119. self._USER_TOKEN = password
  120. else:
  121. raise ExtractorError(self._LOGIN_HINT, expected=True)
  122. token = jwt_decode_hs256(self._USER_TOKEN)
  123. if token.get('exp', 0) <= int(time.time()):
  124. raise ExtractorError('User token has expired', expected=True)
  125. self._USER_COUNTRY = token.get('current_country')
  126. def _real_extract(self, url):
  127. video_id, display_id = self._match_valid_url(url).group('id', 'display_id')
  128. access_token_request = self._download_json(
  129. 'https://launchapi.zee5.com/launch?platform_name=web_app',
  130. video_id, note='Downloading access token')['platform_token']
  131. data = {
  132. 'x-access-token': access_token_request['token'],
  133. }
  134. if self._USER_TOKEN:
  135. data['Authorization'] = f'bearer {self._USER_TOKEN}'
  136. else:
  137. data['X-Z5-Guest-Token'] = self._DEVICE_ID
  138. json_data = self._download_json(
  139. 'https://spapi.zee5.com/singlePlayback/getDetails/secure', video_id, query={
  140. 'content_id': video_id,
  141. 'device_id': self._DEVICE_ID,
  142. 'platform_name': 'desktop_web',
  143. 'country': self._USER_COUNTRY or self.get_param('geo_bypass_country') or 'IN',
  144. 'check_parental_control': False,
  145. }, headers={'content-type': 'application/json'}, data=json.dumps(data).encode())
  146. asset_data = json_data['assetDetails']
  147. show_data = json_data.get('showDetails', {})
  148. if 'premium' in asset_data['business_type']:
  149. raise ExtractorError('Premium content is DRM protected.', expected=True)
  150. if not asset_data.get('hls_url'):
  151. self.raise_login_required(self._LOGIN_HINT, metadata_available=True, method=None)
  152. formats, m3u8_subs = self._extract_m3u8_formats_and_subtitles(asset_data['hls_url'], video_id, 'mp4', fatal=False)
  153. subtitles = {}
  154. for sub in asset_data.get('subtitle_url', []):
  155. sub_url = sub.get('url')
  156. if not sub_url:
  157. continue
  158. subtitles.setdefault(sub.get('language', 'en'), []).append({
  159. 'url': self._proto_relative_url(sub_url),
  160. })
  161. subtitles = self._merge_subtitles(subtitles, m3u8_subs)
  162. return {
  163. 'id': video_id,
  164. 'display_id': display_id,
  165. 'title': asset_data['title'],
  166. 'formats': formats,
  167. 'subtitles': subtitles,
  168. 'duration': int_or_none(asset_data.get('duration')),
  169. 'description': str_or_none(asset_data.get('description')),
  170. 'alt_title': str_or_none(asset_data.get('original_title')),
  171. 'uploader': str_or_none(asset_data.get('content_owner')),
  172. 'age_limit': parse_age_limit(asset_data.get('age_rating')),
  173. 'release_date': unified_strdate(asset_data.get('release_date')),
  174. 'timestamp': unified_timestamp(asset_data.get('release_date')),
  175. 'thumbnail': url_or_none(asset_data.get('image_url')),
  176. 'series': str_or_none(asset_data.get('tvshow_name')),
  177. 'season': try_get(show_data, lambda x: x['seasons']['title'], str),
  178. 'season_number': int_or_none(try_get(show_data, lambda x: x['seasons'][0]['orderid'])),
  179. 'episode_number': int_or_none(try_get(asset_data, lambda x: x['orderid'])),
  180. 'tags': try_get(asset_data, lambda x: x['tags'], list),
  181. }
  182. class Zee5SeriesIE(InfoExtractor):
  183. IE_NAME = 'zee5:series'
  184. _VALID_URL = r'''(?x)
  185. (?:
  186. zee5:series:|
  187. https?://(?:www\.)?zee5\.com/(?:[^#?]+/)?
  188. (?:tv-shows|web-series|kids|zee5originals)/(?!kids-movies)(?:[^#/?]+/){2}
  189. )
  190. (?P<id>[^#/?]+)(?:/episodes)?/?(?:$|[?#])
  191. '''
  192. _TESTS = [{
  193. 'url': 'https://www.zee5.com/kids/kids-shows/bandbudh-aur-budbak/0-6-1899',
  194. 'playlist_mincount': 156,
  195. 'info_dict': {
  196. 'id': '0-6-1899',
  197. },
  198. }, {
  199. 'url': 'https://www.zee5.com/tv-shows/details/bhabi-ji-ghar-par-hai/0-6-199',
  200. 'playlist_mincount': 1500,
  201. 'info_dict': {
  202. 'id': '0-6-199',
  203. },
  204. }, {
  205. 'url': 'https://www.zee5.com/tv-shows/details/agent-raghav-crime-branch/0-6-965',
  206. 'playlist_mincount': 24,
  207. 'info_dict': {
  208. 'id': '0-6-965',
  209. },
  210. }, {
  211. 'url': 'https://www.zee5.com/ta/tv-shows/details/nagabhairavi/0-6-3201',
  212. 'playlist_mincount': 3,
  213. 'info_dict': {
  214. 'id': '0-6-3201',
  215. },
  216. }, {
  217. 'url': 'https://www.zee5.com/global/hi/tv-shows/details/khwaabon-ki-zamin-par/0-6-270',
  218. 'playlist_mincount': 150,
  219. 'info_dict': {
  220. 'id': '0-6-270',
  221. },
  222. }, {
  223. 'url': 'https://www.zee5.com/tv-shows/details/chala-hawa-yeu-dya-ladies-zindabaad/0-6-2943/episodes',
  224. 'only_matching': True,
  225. }, {
  226. 'url': 'https://www.zee5.com/web-series/details/mithya/0-6-4z587408',
  227. 'only_matching': True,
  228. }]
  229. def _entries(self, show_id):
  230. access_token_request = self._download_json(
  231. 'https://launchapi.zee5.com/launch?platform_name=web_app',
  232. show_id, note='Downloading access token')['platform_token']
  233. headers = {
  234. 'X-Access-Token': access_token_request['token'],
  235. 'Referer': 'https://www.zee5.com/',
  236. }
  237. show_url = f'https://gwapi.zee5.com/content/tvshow/{show_id}?translation=en&country=IN'
  238. page_num = 0
  239. show_json = self._download_json(show_url, video_id=show_id, headers=headers)
  240. for season in show_json.get('seasons') or []:
  241. season_id = try_get(season, lambda x: x['id'], str)
  242. next_url = f'https://gwapi.zee5.com/content/tvshow/?season_id={season_id}&type=episode&translation=en&country=IN&on_air=false&asset_subtype=tvshow&page=1&limit=100'
  243. while next_url:
  244. page_num += 1
  245. episodes_json = self._download_json(
  246. next_url, video_id=show_id, headers=headers,
  247. note=f'Downloading JSON metadata page {page_num}')
  248. for episode in try_get(episodes_json, lambda x: x['episode'], list) or []:
  249. video_id = episode.get('id')
  250. yield self.url_result(
  251. f'zee5:{video_id}',
  252. ie=Zee5IE.ie_key(), video_id=video_id)
  253. next_url = url_or_none(episodes_json.get('next_episode_api'))
  254. def _real_extract(self, url):
  255. show_id = self._match_id(url)
  256. return self.playlist_result(self._entries(show_id), playlist_id=show_id)