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.

150 lines
6.1 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'http://(?: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': 'flv',
  51. 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
  52. }
  53. }]
  54. def _real_extract(self, url):
  55. video_id = self._match_id(url)
  56. video_type = None
  57. webpage = self._download_webpage(url, video_id)
  58. # We first look for clipid, because clipprog always appears before
  59. patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')]
  60. results = list(filter(None, (re.search(p, webpage) for p in patterns)))
  61. if results:
  62. matches = results[0]
  63. video_type, video_id = matches.groups()
  64. video_type = 'clip' if video_type == 'id' else 'program'
  65. else:
  66. m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
  67. if m:
  68. video_id = m.group('id')
  69. video_type = 'program' if m.group('type') == 'prog' else 'clip'
  70. else:
  71. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  72. if senate_isvp_url:
  73. title = self._og_search_title(webpage)
  74. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  75. return self.url_result(surl, 'SenateISVP', video_id, title)
  76. if video_type is None or video_id is None:
  77. raise ExtractorError('unable to find video id and type')
  78. def get_text_attr(d, attr):
  79. return d.get(attr, {}).get('#text')
  80. data = self._download_json(
  81. 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
  82. video_id)['video']
  83. if data['@status'] != 'Success':
  84. raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
  85. doc = self._download_xml(
  86. 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
  87. video_id)
  88. description = self._html_search_meta('description', webpage)
  89. title = find_xpath_attr(doc, './/string', 'name', 'title').text
  90. thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
  91. files = data['files']
  92. capfile = get_text_attr(data, 'capfile')
  93. entries = []
  94. for partnum, f in enumerate(files):
  95. formats = []
  96. for quality in f['qualities']:
  97. formats.append({
  98. 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
  99. 'url': unescapeHTML(get_text_attr(quality, 'file')),
  100. 'height': int_or_none(get_text_attr(quality, 'height')),
  101. 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
  102. })
  103. if not formats:
  104. path = unescapeHTML(get_text_attr(f, 'path'))
  105. if not path:
  106. continue
  107. formats = self._extract_m3u8_formats(
  108. path, video_id, 'mp4', entry_protocol='m3u8_native',
  109. m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }]
  110. self._sort_formats(formats)
  111. entries.append({
  112. 'id': '%s_%d' % (video_id, partnum + 1),
  113. 'title': (
  114. title if len(files) == 1 else
  115. '%s part %d' % (title, partnum + 1)),
  116. 'formats': formats,
  117. 'description': description,
  118. 'thumbnail': thumbnail,
  119. 'duration': int_or_none(get_text_attr(f, 'length')),
  120. 'subtitles': {
  121. 'en': [{
  122. 'url': capfile,
  123. 'ext': determine_ext(capfile, 'dfxp')
  124. }],
  125. } if capfile else None,
  126. })
  127. if len(entries) == 1:
  128. entry = dict(entries[0])
  129. entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
  130. return entry
  131. else:
  132. return {
  133. '_type': 'playlist',
  134. 'entries': entries,
  135. 'title': title,
  136. 'id': 'c' + video_id if video_type == 'clip' else video_id,
  137. }