gbnews.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from .common import InfoExtractor
  2. from ..utils import (
  3. ExtractorError,
  4. extract_attributes,
  5. get_elements_html_by_class,
  6. url_or_none,
  7. )
  8. from ..utils.traversal import traverse_obj
  9. class GBNewsIE(InfoExtractor):
  10. IE_DESC = 'GB News clips, features and live streams'
  11. _VALID_URL = r'https?://(?:www\.)?gbnews\.(?:uk|com)/(?:\w+/)?(?P<id>[^#?]+)'
  12. _PLATFORM = 'safari'
  13. _SSMP_URL = 'https://mm-v2.simplestream.com/ssmp/api.php'
  14. _TESTS = [{
  15. 'url': 'https://www.gbnews.com/news/bbc-claudine-gay-harvard-university-antisemitism-row',
  16. 'info_dict': {
  17. 'id': '52264136',
  18. 'ext': 'mp4',
  19. 'thumbnail': r're:https?://www\.gbnews\.\w+/.+\.(?:jpe?g|png|webp)',
  20. 'display_id': 'bbc-claudine-gay-harvard-university-antisemitism-row',
  21. 'description': 'The post was criticised by former employers of the broadcaster',
  22. 'title': 'BBC deletes post after furious backlash over headline downplaying antisemitism',
  23. },
  24. }, {
  25. 'url': 'https://www.gbnews.com/royal/prince-harry-in-love-with-kate-meghan-markle-jealous-royal',
  26. 'info_dict': {
  27. 'id': '52328390',
  28. 'ext': 'mp4',
  29. 'thumbnail': r're:https?://www\.gbnews\.\w+/.+\.(?:jpe?g|png|webp)',
  30. 'display_id': 'prince-harry-in-love-with-kate-meghan-markle-jealous-royal',
  31. 'description': 'Ingrid Seward has published 17 books documenting the highs and lows of the Royal Family',
  32. 'title': 'Royal author claims Prince Harry was \'in love\' with Kate - Meghan was \'jealous\'',
  33. },
  34. }, {
  35. 'url': 'https://www.gbnews.uk/watchlive',
  36. 'info_dict': {
  37. 'id': '1069',
  38. 'ext': 'mp4',
  39. 'thumbnail': r're:https?://www\.gbnews\.\w+/.+\.(?:jpe?g|png|webp)',
  40. 'display_id': 'watchlive',
  41. 'live_status': 'is_live',
  42. 'title': r're:^GB News Live',
  43. },
  44. 'params': {'skip_download': 'm3u8'},
  45. }]
  46. _SS_ENDPOINTS = None
  47. def _get_ss_endpoint(self, data_id, data_env):
  48. if not self._SS_ENDPOINTS:
  49. self._SS_ENDPOINTS = {}
  50. if not data_id:
  51. data_id = 'GB003'
  52. if not data_env:
  53. data_env = 'production'
  54. key = data_id, data_env
  55. result = self._SS_ENDPOINTS.get(key)
  56. if result:
  57. return result
  58. json_data = self._download_json(
  59. self._SSMP_URL, None, 'Downloading Simplestream JSON metadata', query={
  60. 'id': data_id,
  61. 'env': data_env,
  62. })
  63. meta_url = traverse_obj(json_data, ('response', 'api_hostname', {url_or_none}))
  64. if not meta_url:
  65. raise ExtractorError('No API host found')
  66. self._SS_ENDPOINTS[key] = meta_url
  67. return meta_url
  68. def _real_extract(self, url):
  69. display_id = self._match_id(url).rpartition('/')[2]
  70. webpage = self._download_webpage(url, display_id)
  71. video_data = None
  72. elements = get_elements_html_by_class('simplestream', webpage)
  73. for html_tag in elements:
  74. attributes = extract_attributes(html_tag)
  75. if 'sidebar' not in (attributes.get('class') or ''):
  76. video_data = attributes
  77. if not video_data:
  78. raise ExtractorError('Could not find video element', expected=True)
  79. endpoint_url = self._get_ss_endpoint(video_data.get('data-id'), video_data.get('data-env'))
  80. uvid = video_data['data-uvid']
  81. video_type = video_data.get('data-type')
  82. if not video_type or video_type == 'vod':
  83. video_type = 'show'
  84. stream_data = self._download_json(
  85. f'{endpoint_url}/api/{video_type}/stream/{uvid}',
  86. uvid, 'Downloading stream JSON', query={
  87. 'key': video_data.get('data-key'),
  88. 'platform': self._PLATFORM,
  89. })
  90. if traverse_obj(stream_data, 'drm'):
  91. self.report_drm(uvid)
  92. return {
  93. 'id': uvid,
  94. 'display_id': display_id,
  95. 'title': self._og_search_title(webpage, default=None),
  96. 'description': self._og_search_description(webpage, default=None),
  97. 'formats': self._extract_m3u8_formats(traverse_obj(stream_data, (
  98. 'response', 'stream', {url_or_none})), uvid, 'mp4'),
  99. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  100. 'is_live': video_type == 'live',
  101. }