cspan.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. ExtractorError,
  7. extract_attributes,
  8. find_xpath_attr,
  9. get_element_by_class,
  10. int_or_none,
  11. js_to_json,
  12. merge_dicts,
  13. smuggle_url,
  14. unescapeHTML,
  15. )
  16. from .senateisvp import SenateISVPIE
  17. from .ustream import UstreamIE
  18. class CSpanIE(InfoExtractor):
  19. _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
  20. IE_DESC = 'C-SPAN'
  21. _TESTS = [{
  22. 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
  23. 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
  24. 'info_dict': {
  25. 'id': '315139',
  26. 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
  27. },
  28. 'playlist_mincount': 2,
  29. 'skip': 'Regularly fails on travis, for unknown reasons',
  30. }, {
  31. 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
  32. # md5 is unstable
  33. 'info_dict': {
  34. 'id': 'c4486943',
  35. 'ext': 'mp4',
  36. 'title': 'CSPAN - International Health Care Models',
  37. 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
  38. }
  39. }, {
  40. 'url': 'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall',
  41. 'info_dict': {
  42. 'id': '342759',
  43. 'title': 'General Motors Ignition Switch Recall',
  44. },
  45. 'playlist_mincount': 6,
  46. }, {
  47. # Video from senate.gov
  48. 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
  49. 'info_dict': {
  50. 'id': 'judiciary031715',
  51. 'ext': 'mp4',
  52. 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
  53. },
  54. 'params': {
  55. 'skip_download': True, # m3u8 downloads
  56. }
  57. }, {
  58. # Ustream embedded video
  59. 'url': 'https://www.c-span.org/video/?114917-1/armed-services',
  60. 'info_dict': {
  61. 'id': '58428542',
  62. 'ext': 'flv',
  63. 'title': 'USHR07 Armed Services Committee',
  64. 'description': 'hsas00-2118-20150204-1000et-07\n\n\nUSHR07 Armed Services Committee',
  65. 'timestamp': 1423060374,
  66. 'upload_date': '20150204',
  67. 'uploader': 'HouseCommittee',
  68. 'uploader_id': '12987475',
  69. },
  70. }, {
  71. # Audio Only
  72. 'url': 'https://www.c-span.org/video/?437336-1/judiciary-antitrust-competition-policy-consumer-rights',
  73. 'only_matching': True,
  74. }]
  75. BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s'
  76. def _real_extract(self, url):
  77. video_id = self._match_id(url)
  78. video_type = None
  79. webpage = self._download_webpage(url, video_id)
  80. ustream_url = UstreamIE._extract_url(webpage)
  81. if ustream_url:
  82. return self.url_result(ustream_url, UstreamIE.ie_key())
  83. if '&vod' not in url:
  84. bc = self._search_regex(
  85. r"(<[^>]+id='brightcove-player-embed'[^>]+>)",
  86. webpage, 'brightcove embed', default=None)
  87. if bc:
  88. bc_attr = extract_attributes(bc)
  89. bc_url = self.BRIGHTCOVE_URL_TEMPLATE % (
  90. bc_attr.get('data-bcaccountid', '3162030207001'),
  91. bc_attr.get('data-noprebcplayerid', 'SyGGpuJy3g'),
  92. bc_attr.get('data-newbcplayerid', 'default'),
  93. bc_attr['data-bcid'])
  94. return self.url_result(smuggle_url(bc_url, {'source_url': url}))
  95. def add_referer(formats):
  96. for f in formats:
  97. f.setdefault('http_headers', {})['Referer'] = url
  98. # As of 01.12.2020 this path looks to cover all cases making the rest
  99. # of the code unnecessary
  100. jwsetup = self._parse_json(
  101. self._search_regex(
  102. r'(?s)jwsetup\s*=\s*({.+?})\s*;', webpage, 'jwsetup',
  103. default='{}'),
  104. video_id, transform_source=js_to_json, fatal=False)
  105. if jwsetup:
  106. info = self._parse_jwplayer_data(
  107. jwsetup, video_id, require_title=False, m3u8_id='hls',
  108. base_url=url)
  109. add_referer(info['formats'])
  110. ld_info = self._search_json_ld(webpage, video_id, default={})
  111. return merge_dicts(info, ld_info)
  112. # Obsolete
  113. # We first look for clipid, because clipprog always appears before
  114. patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')]
  115. results = list(filter(None, (re.search(p, webpage) for p in patterns)))
  116. if results:
  117. matches = results[0]
  118. video_type, video_id = matches.groups()
  119. video_type = 'clip' if video_type == 'id' else 'program'
  120. else:
  121. m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
  122. if m:
  123. video_id = m.group('id')
  124. video_type = 'program' if m.group('type') == 'prog' else 'clip'
  125. else:
  126. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  127. if senate_isvp_url:
  128. title = self._og_search_title(webpage)
  129. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  130. return self.url_result(surl, 'SenateISVP', video_id, title)
  131. video_id = self._search_regex(
  132. r'jwsetup\.clipprog\s*=\s*(\d+);',
  133. webpage, 'jwsetup program id', default=None)
  134. if video_id:
  135. video_type = 'program'
  136. if video_type is None or video_id is None:
  137. error_message = get_element_by_class('VLplayer-error-message', webpage)
  138. if error_message:
  139. raise ExtractorError(error_message)
  140. raise ExtractorError('unable to find video id and type')
  141. def get_text_attr(d, attr):
  142. return d.get(attr, {}).get('#text')
  143. data = self._download_json(
  144. 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
  145. video_id)['video']
  146. if data['@status'] != 'Success':
  147. raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
  148. doc = self._download_xml(
  149. 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
  150. video_id)
  151. description = self._html_search_meta('description', webpage)
  152. title = find_xpath_attr(doc, './/string', 'name', 'title').text
  153. thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
  154. files = data['files']
  155. capfile = get_text_attr(data, 'capfile')
  156. entries = []
  157. for partnum, f in enumerate(files):
  158. formats = []
  159. for quality in f.get('qualities', []):
  160. formats.append({
  161. 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
  162. 'url': unescapeHTML(get_text_attr(quality, 'file')),
  163. 'height': int_or_none(get_text_attr(quality, 'height')),
  164. 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
  165. })
  166. if not formats:
  167. path = unescapeHTML(get_text_attr(f, 'path'))
  168. if not path:
  169. continue
  170. formats = self._extract_m3u8_formats(
  171. path, video_id, 'mp4', entry_protocol='m3u8_native',
  172. m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }]
  173. add_referer(formats)
  174. self._sort_formats(formats)
  175. entries.append({
  176. 'id': '%s_%d' % (video_id, partnum + 1),
  177. 'title': (
  178. title if len(files) == 1 else
  179. '%s part %d' % (title, partnum + 1)),
  180. 'formats': formats,
  181. 'description': description,
  182. 'thumbnail': thumbnail,
  183. 'duration': int_or_none(get_text_attr(f, 'length')),
  184. 'subtitles': {
  185. 'en': [{
  186. 'url': capfile,
  187. 'ext': determine_ext(capfile, 'dfxp')
  188. }],
  189. } if capfile else None,
  190. })
  191. if len(entries) == 1:
  192. entry = dict(entries[0])
  193. entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
  194. return entry
  195. else:
  196. return {
  197. '_type': 'playlist',
  198. 'entries': entries,
  199. 'title': title,
  200. 'id': 'c' + video_id if video_type == 'clip' else video_id,
  201. }