cnbc.py 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from .common import InfoExtractor
  2. from ..utils import int_or_none, parse_iso8601, str_or_none, url_or_none
  3. from ..utils.traversal import traverse_obj
  4. class CNBCVideoIE(InfoExtractor):
  5. _VALID_URL = r'https?://(?:www\.)?cnbc\.com/video/(?:[^/?#]+/)+(?P<id>[^./?#&]+)\.html'
  6. _TESTS = [{
  7. 'url': 'https://www.cnbc.com/video/2023/12/07/mcdonalds-just-unveiled-cosmcsits-new-spinoff-brand.html',
  8. 'info_dict': {
  9. 'ext': 'mp4',
  10. 'id': '107344774',
  11. 'display_id': 'mcdonalds-just-unveiled-cosmcsits-new-spinoff-brand',
  12. 'modified_timestamp': 1702053483,
  13. 'timestamp': 1701977810,
  14. 'channel': 'News Videos',
  15. 'upload_date': '20231207',
  16. 'description': 'md5:882c001d85cb43d7579b514307b3e78b',
  17. 'release_timestamp': 1701977375,
  18. 'modified_date': '20231208',
  19. 'release_date': '20231207',
  20. 'duration': 65,
  21. 'creators': ['Sean Conlon'],
  22. 'title': 'Here\'s a first look at McDonald\'s new spinoff brand, CosMc\'s',
  23. 'thumbnail': 'https://image.cnbcfm.com/api/v1/image/107344192-1701894812493-CosMcsskyHero_2336x1040_hero-desktop.jpg?v=1701894855',
  24. },
  25. 'expected_warnings': ['Unable to download f4m manifest'],
  26. }, {
  27. 'url': 'https://www.cnbc.com/video/2023/12/08/jim-cramer-shares-his-take-on-seattles-tech-scene.html',
  28. 'info_dict': {
  29. 'creators': ['Jim Cramer'],
  30. 'channel': 'Mad Money with Jim Cramer',
  31. 'description': 'md5:72925be21b952e95eba51178dddf4e3e',
  32. 'duration': 299.0,
  33. 'ext': 'mp4',
  34. 'id': '107345451',
  35. 'display_id': 'jim-cramer-shares-his-take-on-seattles-tech-scene',
  36. 'thumbnail': 'https://image.cnbcfm.com/api/v1/image/107345481-1702079431MM-B-120823.jpg?v=1702079430',
  37. 'timestamp': 1702080139,
  38. 'title': 'Jim Cramer shares his take on Seattle\'s tech scene',
  39. 'release_date': '20231208',
  40. 'upload_date': '20231209',
  41. 'modified_timestamp': 1702080139,
  42. 'modified_date': '20231209',
  43. 'release_timestamp': 1702073551,
  44. },
  45. 'expected_warnings': ['Unable to download f4m manifest'],
  46. }, {
  47. 'url': 'https://www.cnbc.com/video/2023/12/08/the-epicenter-of-ai-is-in-seattle-says-jim-cramer.html',
  48. 'info_dict': {
  49. 'creators': ['Jim Cramer'],
  50. 'channel': 'Mad Money with Jim Cramer',
  51. 'description': 'md5:72925be21b952e95eba51178dddf4e3e',
  52. 'duration': 113.0,
  53. 'ext': 'mp4',
  54. 'id': '107345474',
  55. 'display_id': 'the-epicenter-of-ai-is-in-seattle-says-jim-cramer',
  56. 'thumbnail': 'https://image.cnbcfm.com/api/v1/image/107345486-Screenshot_2023-12-08_at_70339_PM.png?v=1702080248',
  57. 'timestamp': 1702080535,
  58. 'title': 'The epicenter of AI is in Seattle, says Jim Cramer',
  59. 'release_timestamp': 1702077347,
  60. 'modified_timestamp': 1702080535,
  61. 'release_date': '20231208',
  62. 'upload_date': '20231209',
  63. 'modified_date': '20231209',
  64. },
  65. 'expected_warnings': ['Unable to download f4m manifest'],
  66. }]
  67. def _real_extract(self, url):
  68. display_id = self._match_id(url)
  69. webpage = self._download_webpage(url, display_id)
  70. data = self._search_json(r'window\.__s_data=', webpage, 'video data', display_id)
  71. player_data = traverse_obj(data, (
  72. 'page', 'page', 'layout', ..., 'columns', ..., 'modules',
  73. lambda _, v: v['name'] == 'clipPlayer', 'data', {dict}), get_all=False)
  74. return {
  75. 'id': display_id,
  76. 'display_id': display_id,
  77. 'formats': self._extract_akamai_formats(player_data['playbackURL'], display_id),
  78. **self._search_json_ld(webpage, display_id, fatal=False),
  79. **traverse_obj(player_data, {
  80. 'id': ('id', {str_or_none}),
  81. 'title': ('title', {str}),
  82. 'description': ('description', {str}),
  83. 'creators': ('author', ..., 'name', {str}),
  84. 'timestamp': ('datePublished', {parse_iso8601}),
  85. 'release_timestamp': ('uploadDate', {parse_iso8601}),
  86. 'modified_timestamp': ('dateLastPublished', {parse_iso8601}),
  87. 'thumbnail': ('thumbnail', {url_or_none}),
  88. 'duration': ('duration', {int_or_none}),
  89. 'channel': ('section', 'title', {str}),
  90. }),
  91. }