cbs.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from .theplatform import ThePlatformFeedIE
  2. from ..utils import (
  3. ExtractorError,
  4. int_or_none,
  5. find_xpath_attr,
  6. xpath_element,
  7. xpath_text,
  8. update_url_query,
  9. url_or_none,
  10. )
  11. class CBSBaseIE(ThePlatformFeedIE): # XXX: Do not subclass from concrete IE
  12. def _parse_smil_subtitles(self, smil, namespace=None, subtitles_lang='en'):
  13. subtitles = {}
  14. for k, ext in [('sMPTE-TTCCURL', 'tt'), ('ClosedCaptionURL', 'ttml'), ('webVTTCaptionURL', 'vtt')]:
  15. cc_e = find_xpath_attr(smil, self._xpath_ns('.//param', namespace), 'name', k)
  16. if cc_e is not None:
  17. cc_url = cc_e.get('value')
  18. if cc_url:
  19. subtitles.setdefault(subtitles_lang, []).append({
  20. 'ext': ext,
  21. 'url': cc_url,
  22. })
  23. return subtitles
  24. def _extract_common_video_info(self, content_id, asset_types, mpx_acc, extra_info):
  25. tp_path = 'dJ5BDC/media/guid/%d/%s' % (mpx_acc, content_id)
  26. tp_release_url = f'https://link.theplatform.com/s/{tp_path}'
  27. info = self._extract_theplatform_metadata(tp_path, content_id)
  28. formats, subtitles = [], {}
  29. last_e = None
  30. for asset_type, query in asset_types.items():
  31. try:
  32. tp_formats, tp_subtitles = self._extract_theplatform_smil(
  33. update_url_query(tp_release_url, query), content_id,
  34. 'Downloading %s SMIL data' % asset_type)
  35. except ExtractorError as e:
  36. last_e = e
  37. if asset_type != 'fallback':
  38. continue
  39. query['formats'] = '' # blank query to check if expired
  40. try:
  41. tp_formats, tp_subtitles = self._extract_theplatform_smil(
  42. update_url_query(tp_release_url, query), content_id,
  43. 'Downloading %s SMIL data, trying again with another format' % asset_type)
  44. except ExtractorError as e:
  45. last_e = e
  46. continue
  47. formats.extend(tp_formats)
  48. subtitles = self._merge_subtitles(subtitles, tp_subtitles)
  49. if last_e and not formats:
  50. self.raise_no_formats(last_e, True, content_id)
  51. extra_info.update({
  52. 'id': content_id,
  53. 'formats': formats,
  54. 'subtitles': subtitles,
  55. })
  56. info.update({k: v for k, v in extra_info.items() if v is not None})
  57. return info
  58. def _extract_video_info(self, *args, **kwargs):
  59. # Extract assets + metadata and call _extract_common_video_info
  60. raise NotImplementedError('This method must be implemented by subclasses')
  61. def _real_extract(self, url):
  62. return self._extract_video_info(self._match_id(url))
  63. class CBSIE(CBSBaseIE):
  64. _VALID_URL = r'''(?x)
  65. (?:
  66. cbs:|
  67. https?://(?:www\.)?(?:
  68. cbs\.com/(?:shows|movies)/(?:video|[^/]+/video|[^/]+)/|
  69. colbertlateshow\.com/(?:video|podcasts)/)
  70. )(?P<id>[\w-]+)'''
  71. # All tests are blocked outside US
  72. _TESTS = [{
  73. 'url': 'https://www.cbs.com/shows/video/xrUyNLtl9wd8D_RWWAg9NU2F_V6QpB3R/',
  74. 'info_dict': {
  75. 'id': 'xrUyNLtl9wd8D_RWWAg9NU2F_V6QpB3R',
  76. 'ext': 'mp4',
  77. 'title': 'Tough As Nails - Dreams Never Die',
  78. 'description': 'md5:a3535a62531cdd52b0364248a2c1ae33',
  79. 'duration': 2588,
  80. 'timestamp': 1639015200,
  81. 'upload_date': '20211209',
  82. 'uploader': 'CBSI-NEW',
  83. },
  84. 'params': {
  85. # m3u8 download
  86. 'skip_download': True,
  87. },
  88. }, {
  89. 'url': 'https://www.cbs.com/shows/video/sZH1MGgomIosZgxGJ1l263MFq16oMtW1/',
  90. 'info_dict': {
  91. 'id': 'sZH1MGgomIosZgxGJ1l263MFq16oMtW1',
  92. 'title': 'The Late Show - 3/16/22 (Michael Buble, Rose Matafeo)',
  93. 'timestamp': 1647488100,
  94. 'description': 'md5:d0e6ec23c544b7fa8e39a8e6844d2439',
  95. 'uploader': 'CBSI-NEW',
  96. 'upload_date': '20220317',
  97. },
  98. 'params': {
  99. 'ignore_no_formats_error': True,
  100. 'skip_download': True,
  101. },
  102. 'expected_warnings': [
  103. 'This content expired on', 'No video formats found', 'Requested format is not available'],
  104. }, {
  105. 'url': 'http://colbertlateshow.com/video/8GmB0oY0McANFvp2aEffk9jZZZ2YyXxy/the-colbeard/',
  106. 'only_matching': True,
  107. }, {
  108. 'url': 'http://www.colbertlateshow.com/podcasts/dYSwjqPs_X1tvbV_P2FcPWRa_qT6akTC/in-the-bad-room-with-stephen/',
  109. 'only_matching': True,
  110. }]
  111. def _extract_video_info(self, content_id, site='cbs', mpx_acc=2198311517):
  112. items_data = self._download_xml(
  113. 'https://can.cbs.com/thunder/player/videoPlayerService.php',
  114. content_id, query={'partner': site, 'contentId': content_id})
  115. video_data = xpath_element(items_data, './/item')
  116. title = xpath_text(video_data, 'videoTitle', 'title') or xpath_text(video_data, 'videotitle', 'title')
  117. asset_types = {}
  118. has_drm = False
  119. for item in items_data.findall('.//item'):
  120. asset_type = xpath_text(item, 'assetType')
  121. query = {
  122. 'mbr': 'true',
  123. 'assetTypes': asset_type,
  124. }
  125. if not asset_type:
  126. # fallback for content_ids that videoPlayerService doesn't return anything for
  127. asset_type = 'fallback'
  128. query['formats'] = 'M3U+none,MPEG4,M3U+appleHlsEncryption,MP3'
  129. del query['assetTypes']
  130. if asset_type in asset_types:
  131. continue
  132. elif any(excluded in asset_type for excluded in ('HLS_FPS', 'DASH_CENC', 'OnceURL')):
  133. if 'DASH_CENC' in asset_type:
  134. has_drm = True
  135. continue
  136. if asset_type.startswith('HLS') or 'StreamPack' in asset_type:
  137. query['formats'] = 'MPEG4,M3U'
  138. elif asset_type in ('RTMP', 'WIFI', '3G'):
  139. query['formats'] = 'MPEG4,FLV'
  140. asset_types[asset_type] = query
  141. if not asset_types and has_drm:
  142. self.report_drm(content_id)
  143. return self._extract_common_video_info(content_id, asset_types, mpx_acc, extra_info={
  144. 'title': title,
  145. 'series': xpath_text(video_data, 'seriesTitle'),
  146. 'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
  147. 'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
  148. 'duration': int_or_none(xpath_text(video_data, 'videoLength'), 1000),
  149. 'thumbnail': url_or_none(xpath_text(video_data, 'previewImageURL')),
  150. })