You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
6.2 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. unescapeHTML,
  7. find_xpath_attr,
  8. smuggle_url,
  9. determine_ext,
  10. ExtractorError,
  11. )
  12. from .senateisvp import SenateISVPIE
  13. class CSpanIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
  15. IE_DESC = 'C-SPAN'
  16. _TESTS = [{
  17. 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
  18. 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
  19. 'info_dict': {
  20. 'id': '315139',
  21. 'ext': 'mp4',
  22. 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
  23. 'description': 'Attorney General Eric Holder speaks to reporters following the Supreme Court decision in [Shelby County v. Holder], in which the court ruled that the preclearance provisions of the Voting Rights Act could not be enforced.',
  24. },
  25. 'skip': 'Regularly fails on travis, for unknown reasons',
  26. }, {
  27. 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
  28. 'md5': '8e5fbfabe6ad0f89f3012a7943c1287b',
  29. 'info_dict': {
  30. 'id': 'c4486943',
  31. 'ext': 'mp4',
  32. 'title': 'CSPAN - International Health Care Models',
  33. 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
  34. }
  35. }, {
  36. 'url': 'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall',
  37. 'md5': '2ae5051559169baadba13fc35345ae74',
  38. 'info_dict': {
  39. 'id': '342759',
  40. 'ext': 'mp4',
  41. 'title': 'General Motors Ignition Switch Recall',
  42. 'duration': 14848,
  43. 'description': 'md5:118081aedd24bf1d3b68b3803344e7f3'
  44. },
  45. }, {
  46. # Video from senate.gov
  47. 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
  48. 'info_dict': {
  49. 'id': 'judiciary031715',
  50. 'ext': 'mp4',
  51. 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
  52. },
  53. 'params': {
  54. 'skip_download': True, # m3u8 downloads
  55. }
  56. }]
  57. def _real_extract(self, url):
  58. video_id = self._match_id(url)
  59. video_type = None
  60. webpage = self._download_webpage(url, video_id)
  61. # We first look for clipid, because clipprog always appears before
  62. patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')]
  63. results = list(filter(None, (re.search(p, webpage) for p in patterns)))
  64. if results:
  65. matches = results[0]
  66. video_type, video_id = matches.groups()
  67. video_type = 'clip' if video_type == 'id' else 'program'
  68. else:
  69. m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
  70. if m:
  71. video_id = m.group('id')
  72. video_type = 'program' if m.group('type') == 'prog' else 'clip'
  73. else:
  74. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  75. if senate_isvp_url:
  76. title = self._og_search_title(webpage)
  77. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  78. return self.url_result(surl, 'SenateISVP', video_id, title)
  79. if video_type is None or video_id is None:
  80. raise ExtractorError('unable to find video id and type')
  81. def get_text_attr(d, attr):
  82. return d.get(attr, {}).get('#text')
  83. data = self._download_json(
  84. 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
  85. video_id)['video']
  86. if data['@status'] != 'Success':
  87. raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
  88. doc = self._download_xml(
  89. 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
  90. video_id)
  91. description = self._html_search_meta('description', webpage)
  92. title = find_xpath_attr(doc, './/string', 'name', 'title').text
  93. thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
  94. files = data['files']
  95. capfile = get_text_attr(data, 'capfile')
  96. entries = []
  97. for partnum, f in enumerate(files):
  98. formats = []
  99. for quality in f['qualities']:
  100. formats.append({
  101. 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
  102. 'url': unescapeHTML(get_text_attr(quality, 'file')),
  103. 'height': int_or_none(get_text_attr(quality, 'height')),
  104. 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
  105. })
  106. if not formats:
  107. path = unescapeHTML(get_text_attr(f, 'path'))
  108. if not path:
  109. continue
  110. formats = self._extract_m3u8_formats(
  111. path, video_id, 'mp4', entry_protocol='m3u8_native',
  112. m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }]
  113. self._sort_formats(formats)
  114. entries.append({
  115. 'id': '%s_%d' % (video_id, partnum + 1),
  116. 'title': (
  117. title if len(files) == 1 else
  118. '%s part %d' % (title, partnum + 1)),
  119. 'formats': formats,
  120. 'description': description,
  121. 'thumbnail': thumbnail,
  122. 'duration': int_or_none(get_text_attr(f, 'length')),
  123. 'subtitles': {
  124. 'en': [{
  125. 'url': capfile,
  126. 'ext': determine_ext(capfile, 'dfxp')
  127. }],
  128. } if capfile else None,
  129. })
  130. if len(entries) == 1:
  131. entry = dict(entries[0])
  132. entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
  133. return entry
  134. else:
  135. return {
  136. '_type': 'playlist',
  137. 'entries': entries,
  138. 'title': title,
  139. 'id': 'c' + video_id if video_type == 'clip' else video_id,
  140. }