rcti.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import json
  2. import random
  3. import time
  4. from .common import InfoExtractor
  5. from ..networking.exceptions import HTTPError
  6. from ..utils import (
  7. ExtractorError,
  8. dict_get,
  9. strip_or_none,
  10. traverse_obj,
  11. try_get,
  12. )
  13. class RCTIPlusBaseIE(InfoExtractor):
  14. def _real_initialize(self):
  15. self._AUTH_KEY = self._download_json(
  16. 'https://api.rctiplus.com/api/v1/visitor?platform=web', # platform can be web, mweb, android, ios
  17. None, 'Fetching authorization key')['data']['access_token']
  18. def _call_api(self, url, video_id, note=None):
  19. json = self._download_json(
  20. url, video_id, note=note, headers={'Authorization': self._AUTH_KEY})
  21. if json.get('status', {}).get('code', 0) != 0:
  22. raise ExtractorError(f'{self.IE_NAME} said: {json["status"]["message_client"]}', cause=json)
  23. return json.get('data'), json.get('meta')
  24. class RCTIPlusIE(RCTIPlusBaseIE):
  25. _VALID_URL = r'https?://www\.rctiplus\.com/(?:programs/\d+?/.*?/)?(?P<type>episode|clip|extra|live-event|missed-event)/(?P<id>\d+)/(?P<display_id>[^/?#&]+)'
  26. _TESTS = [{
  27. 'url': 'https://www.rctiplus.com/programs/1259/kiko-untuk-lola/episode/22124/untuk-lola',
  28. 'md5': '56ed45affad45fa18d5592a1bc199997',
  29. 'info_dict': {
  30. 'id': 'v_e22124',
  31. 'title': 'Untuk Lola',
  32. 'display_id': 'untuk-lola',
  33. 'description': 'md5:2b809075c0b1e071e228ad6d13e41deb',
  34. 'ext': 'mp4',
  35. 'duration': 1400,
  36. 'timestamp': 1615978800,
  37. 'upload_date': '20210317',
  38. 'series': 'Kiko : Untuk Lola',
  39. 'season_number': 1,
  40. 'episode_number': 1,
  41. 'channel': 'RCTI',
  42. },
  43. 'params': {
  44. 'fixup': 'never',
  45. },
  46. }, { # Clip; Series title doesn't appear on metadata JSON
  47. 'url': 'https://www.rctiplus.com/programs/316/cahaya-terindah/clip/3921/make-a-wish',
  48. 'md5': 'd179b2ff356f0e91a53bcc6a4d8504f0',
  49. 'info_dict': {
  50. 'id': 'v_c3921',
  51. 'title': 'Make A Wish',
  52. 'display_id': 'make-a-wish',
  53. 'description': 'Make A Wish',
  54. 'ext': 'mp4',
  55. 'duration': 288,
  56. 'timestamp': 1571652600,
  57. 'upload_date': '20191021',
  58. 'series': 'Cahaya Terindah',
  59. 'channel': 'RCTI',
  60. },
  61. 'params': {
  62. 'fixup': 'never',
  63. },
  64. }, { # Extra
  65. 'url': 'https://www.rctiplus.com/programs/616/inews-malam/extra/9438/diungkapkan-melalui-surat-terbuka-ceo-ruangguru-belva-devara-mundur-dari-staf-khusus-presiden',
  66. 'md5': 'c48106afdbce609749f5e0c007d9278a',
  67. 'info_dict': {
  68. 'id': 'v_ex9438',
  69. 'title': 'md5:2ede828c0f8bde249e0912be150314ca',
  70. 'display_id': 'md5:62b8d4e9ff096db527a1ad797e8a9933',
  71. 'description': 'md5:2ede828c0f8bde249e0912be150314ca',
  72. 'ext': 'mp4',
  73. 'duration': 93,
  74. 'timestamp': 1587561540,
  75. 'upload_date': '20200422',
  76. 'series': 'iNews Malam',
  77. 'channel': 'INews',
  78. },
  79. }, { # Missed event/replay
  80. 'url': 'https://www.rctiplus.com/missed-event/2507/mou-signing-ceremony-27-juli-2021-1400-wib',
  81. 'md5': '649c5f27250faed1452ca8b91e06922d',
  82. 'info_dict': {
  83. 'id': 'v_pe2507',
  84. 'title': 'MOU Signing Ceremony | 27 Juli 2021 | 14.00 WIB',
  85. 'display_id': 'mou-signing-ceremony-27-juli-2021-1400-wib',
  86. 'ext': 'mp4',
  87. 'timestamp': 1627142400,
  88. 'upload_date': '20210724',
  89. 'was_live': True,
  90. 'release_timestamp': 1627369200,
  91. },
  92. 'params': {
  93. 'fixup': 'never',
  94. },
  95. }, { # Live event; Cloudfront CDN
  96. 'url': 'https://www.rctiplus.com/live-event/2530/dai-muda-charging-imun-dengan-iman-4-agustus-2021-1600-wib',
  97. 'info_dict': {
  98. 'id': 'v_le2530',
  99. 'title': 'Dai Muda : Charging Imun dengan Iman | 4 Agustus 2021 | 16.00 WIB',
  100. 'display_id': 'dai-muda-charging-imun-dengan-iman-4-agustus-2021-1600-wib',
  101. 'ext': 'mp4',
  102. 'timestamp': 1627898400,
  103. 'upload_date': '20210802',
  104. 'release_timestamp': 1628067600,
  105. },
  106. 'params': {
  107. 'skip_download': True,
  108. },
  109. 'skip': 'This live event has ended.',
  110. }, { # TV; live_at is null
  111. 'url': 'https://www.rctiplus.com/live-event/1/rcti',
  112. 'info_dict': {
  113. 'id': 'v_lt1',
  114. 'title': 'RCTI',
  115. 'display_id': 'rcti',
  116. 'ext': 'mp4',
  117. 'timestamp': 1546344000,
  118. 'upload_date': '20190101',
  119. 'is_live': True,
  120. },
  121. 'params': {
  122. 'skip_download': True,
  123. },
  124. }]
  125. _CONVIVA_JSON_TEMPLATE = {
  126. 't': 'CwsSessionHb',
  127. 'cid': 'ff84ae928c3b33064b76dec08f12500465e59a6f',
  128. 'clid': '0',
  129. 'sid': 0,
  130. 'seq': 0,
  131. 'caps': 0,
  132. 'sf': 7,
  133. 'sdk': True,
  134. }
  135. def _real_extract(self, url):
  136. match = self._match_valid_url(url).groupdict()
  137. video_type, video_id, display_id = match['type'], match['id'], match['display_id']
  138. url_api_version = 'v2' if video_type == 'missed-event' else 'v1'
  139. appier_id = '23984824_' + str(random.randint(0, 10000000000)) # Based on the webpage's uuidRandom generator
  140. video_json = self._call_api(
  141. f'https://api.rctiplus.com/api/{url_api_version}/{video_type}/{video_id}/url?appierid={appier_id}', display_id, 'Downloading video URL JSON')[0]
  142. video_url = video_json['url']
  143. is_upcoming = try_get(video_json, lambda x: x['current_date'] < x['live_at'])
  144. if is_upcoming is None:
  145. is_upcoming = try_get(video_json, lambda x: x['current_date'] < x['start_date'])
  146. if is_upcoming:
  147. self.raise_no_formats(
  148. 'This event will start at {}.'.format(video_json['live_label']) if video_json.get('live_label') else 'This event has not started yet.', expected=True)
  149. if 'akamaized' in video_url:
  150. # For some videos hosted on Akamai's CDN (possibly AES-encrypted ones?), a session needs to at least be made via Conviva's API
  151. conviva_json_data = {
  152. **self._CONVIVA_JSON_TEMPLATE,
  153. 'url': video_url,
  154. 'sst': int(time.time()),
  155. }
  156. conviva_json_res = self._download_json(
  157. 'https://ff84ae928c3b33064b76dec08f12500465e59a6f.cws.conviva.com/0/wsg', display_id,
  158. 'Creating Conviva session', 'Failed to create Conviva session',
  159. fatal=False, data=json.dumps(conviva_json_data).encode())
  160. if conviva_json_res and conviva_json_res.get('err') != 'ok':
  161. self.report_warning('Conviva said: {}'.format(str(conviva_json_res.get('err'))))
  162. video_meta, meta_paths = self._call_api(
  163. f'https://api.rctiplus.com/api/v1/{video_type}/{video_id}', display_id, 'Downloading video metadata')
  164. thumbnails, image_path = [], meta_paths.get('image_path', 'https://rstatic.akamaized.net/media/')
  165. if video_meta.get('portrait_image'):
  166. thumbnails.append({
  167. 'id': 'portrait_image',
  168. 'url': '{}{}{}'.format(image_path, 2000, video_meta['portrait_image']), # 2000px seems to be the highest resolution that can be given
  169. })
  170. if video_meta.get('landscape_image'):
  171. thumbnails.append({
  172. 'id': 'landscape_image',
  173. 'url': '{}{}{}'.format(image_path, 2000, video_meta['landscape_image']),
  174. })
  175. try:
  176. formats = self._extract_m3u8_formats(video_url, display_id, 'mp4', headers={'Referer': 'https://www.rctiplus.com/'})
  177. except ExtractorError as e:
  178. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  179. self.raise_geo_restricted(countries=['ID'], metadata_available=True)
  180. else:
  181. raise e
  182. for f in formats:
  183. if 'akamaized' in f['url'] or 'cloudfront' in f['url']:
  184. f.setdefault('http_headers', {})['Referer'] = 'https://www.rctiplus.com/' # Referer header is required for akamai/cloudfront CDNs
  185. return {
  186. 'id': video_meta.get('product_id') or video_json.get('product_id'),
  187. 'title': dict_get(video_meta, ('title', 'name')) or dict_get(video_json, ('content_name', 'assets_name')),
  188. 'display_id': display_id,
  189. 'description': video_meta.get('summary'),
  190. 'timestamp': video_meta.get('release_date') or video_json.get('start_date'),
  191. 'duration': video_meta.get('duration'),
  192. 'categories': [video_meta['genre']] if video_meta.get('genre') else None,
  193. 'average_rating': video_meta.get('star_rating'),
  194. 'series': video_meta.get('program_title') or video_json.get('program_title'),
  195. 'season_number': video_meta.get('season'),
  196. 'episode_number': video_meta.get('episode'),
  197. 'channel': video_json.get('tv_name'),
  198. 'channel_id': video_json.get('tv_id'),
  199. 'formats': formats,
  200. 'thumbnails': thumbnails,
  201. 'is_live': video_type == 'live-event' and not is_upcoming,
  202. 'was_live': video_type == 'missed-event',
  203. 'live_status': 'is_upcoming' if is_upcoming else None,
  204. 'release_timestamp': video_json.get('live_at'),
  205. }
  206. class RCTIPlusSeriesIE(RCTIPlusBaseIE):
  207. _VALID_URL = r'https?://www\.rctiplus\.com/programs/(?P<id>\d+)/(?P<display_id>[^/?#&]+)(?:/(?P<type>episodes|extras|clips))?'
  208. _TESTS = [{
  209. 'url': 'https://www.rctiplus.com/programs/829/putri-untuk-pangeran',
  210. 'playlist_mincount': 1019,
  211. 'info_dict': {
  212. 'id': '829',
  213. 'title': 'Putri Untuk Pangeran',
  214. 'description': 'md5:aca7b54d05bd95a67d4f4613cc1d622d',
  215. 'age_limit': 2,
  216. 'cast': ['Verrel Bramasta', 'Ranty Maria', 'Riza Syah', 'Ivan Fadilla', 'Nicole Parham', 'Dll', 'Aviv Elham'],
  217. 'display_id': 'putri-untuk-pangeran',
  218. 'tags': 'count:18',
  219. },
  220. }, { # No episodes
  221. 'url': 'https://www.rctiplus.com/programs/615/inews-pagi',
  222. 'playlist_mincount': 388,
  223. 'info_dict': {
  224. 'id': '615',
  225. 'title': 'iNews Pagi',
  226. 'description': 'md5:f18ee3d4643cfb41c358e5a9b693ee04',
  227. 'age_limit': 2,
  228. 'tags': 'count:11',
  229. 'display_id': 'inews-pagi',
  230. },
  231. }]
  232. _AGE_RATINGS = { # Based off https://id.wikipedia.org/wiki/Sistem_rating_konten_televisi with additional ratings
  233. 'S-SU': 2,
  234. 'SU': 2,
  235. 'P': 2,
  236. 'A': 7,
  237. 'R': 13,
  238. 'R-R/1': 17, # Labelled as 17+ despite being R
  239. 'D': 18,
  240. }
  241. @classmethod
  242. def suitable(cls, url):
  243. return False if RCTIPlusIE.suitable(url) else super().suitable(url)
  244. def _entries(self, url, display_id=None, note='Downloading entries JSON', metadata={}):
  245. total_pages = 0
  246. try:
  247. total_pages = self._call_api(
  248. f'{url}&length=20&page=0',
  249. display_id, note)[1]['pagination']['total_page']
  250. except ExtractorError as e:
  251. if 'not found' in str(e):
  252. return []
  253. raise e
  254. if total_pages <= 0:
  255. return []
  256. for page_num in range(1, total_pages + 1):
  257. episode_list = self._call_api(
  258. f'{url}&length=20&page={page_num}',
  259. display_id, f'{note} page {page_num}')[0] or []
  260. for video_json in episode_list:
  261. yield {
  262. '_type': 'url',
  263. 'url': video_json['share_link'],
  264. 'ie_key': RCTIPlusIE.ie_key(),
  265. 'id': video_json.get('product_id'),
  266. 'title': video_json.get('title'),
  267. 'display_id': video_json.get('title_code').replace('_', '-'),
  268. 'description': video_json.get('summary'),
  269. 'timestamp': video_json.get('release_date'),
  270. 'duration': video_json.get('duration'),
  271. 'season_number': video_json.get('season'),
  272. 'episode_number': video_json.get('episode'),
  273. **metadata,
  274. }
  275. def _series_entries(self, series_id, display_id=None, video_type=None, metadata={}):
  276. if not video_type or video_type in 'episodes':
  277. try:
  278. seasons_list = self._call_api(
  279. f'https://api.rctiplus.com/api/v1/program/{series_id}/season',
  280. display_id, 'Downloading seasons list JSON')[0]
  281. except ExtractorError as e:
  282. if 'not found' not in str(e):
  283. raise
  284. seasons_list = []
  285. for season in seasons_list:
  286. yield from self._entries(
  287. f'https://api.rctiplus.com/api/v2/program/{series_id}/episode?season={season["season"]}',
  288. display_id, f'Downloading season {season["season"]} episode entries', metadata)
  289. if not video_type or video_type in 'extras':
  290. yield from self._entries(
  291. f'https://api.rctiplus.com/api/v2/program/{series_id}/extra?content_id=0',
  292. display_id, 'Downloading extra entries', metadata)
  293. if not video_type or video_type in 'clips':
  294. yield from self._entries(
  295. f'https://api.rctiplus.com/api/v2/program/{series_id}/clip?content_id=0',
  296. display_id, 'Downloading clip entries', metadata)
  297. def _real_extract(self, url):
  298. series_id, display_id, video_type = self._match_valid_url(url).group('id', 'display_id', 'type')
  299. if video_type:
  300. self.report_warning(
  301. f'Only {video_type} will be downloaded. '
  302. f'To download everything from the series, remove "/{video_type}" from the URL')
  303. series_meta, meta_paths = self._call_api(
  304. f'https://api.rctiplus.com/api/v1/program/{series_id}/detail', display_id, 'Downloading series metadata')
  305. metadata = {
  306. 'age_limit': try_get(series_meta, lambda x: self._AGE_RATINGS[x['age_restriction'][0]['code']]),
  307. 'cast': traverse_obj(series_meta, (('starring', 'creator', 'writer'), ..., 'name'),
  308. expected_type=lambda x: strip_or_none(x) or None),
  309. 'tags': traverse_obj(series_meta, ('tag', ..., 'name'),
  310. expected_type=lambda x: strip_or_none(x) or None),
  311. }
  312. return self.playlist_result(
  313. self._series_entries(series_id, display_id, video_type, metadata), series_id,
  314. series_meta.get('title'), series_meta.get('summary'), display_id=display_id, **metadata)
  315. class RCTIPlusTVIE(RCTIPlusBaseIE):
  316. _VALID_URL = r'https?://www\.rctiplus\.com/((tv/(?P<tvname>\w+))|(?P<eventname>live-event|missed-event))'
  317. _TESTS = [{
  318. 'url': 'https://www.rctiplus.com/tv/rcti',
  319. 'info_dict': {
  320. 'id': 'v_lt1',
  321. 'title': 'RCTI',
  322. 'ext': 'mp4',
  323. 'timestamp': 1546344000,
  324. 'upload_date': '20190101',
  325. },
  326. 'params': {
  327. 'skip_download': True,
  328. },
  329. }, {
  330. # Returned video will always change
  331. 'url': 'https://www.rctiplus.com/live-event',
  332. 'only_matching': True,
  333. }, {
  334. # Returned video will also always change
  335. 'url': 'https://www.rctiplus.com/missed-event',
  336. 'only_matching': True,
  337. }]
  338. @classmethod
  339. def suitable(cls, url):
  340. return False if RCTIPlusIE.suitable(url) else super().suitable(url)
  341. def _real_extract(self, url):
  342. match = self._match_valid_url(url).groupdict()
  343. tv_id = match.get('tvname') or match.get('eventname')
  344. webpage = self._download_webpage(url, tv_id)
  345. video_type, video_id = self._search_regex(
  346. r'url\s*:\s*["\']https://api\.rctiplus\.com/api/v./(?P<type>[^/]+)/(?P<id>\d+)/url',
  347. webpage, 'video link', group=('type', 'id'))
  348. return self.url_result(f'https://www.rctiplus.com/{video_type}/{video_id}/{tv_id}', 'RCTIPlus')