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.

168 lines
6.5 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. from .ustream import UstreamIE
  14. class CSpanIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
  16. IE_DESC = 'C-SPAN'
  17. _TESTS = [{
  18. 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
  19. 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
  20. 'info_dict': {
  21. 'id': '315139',
  22. 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
  23. },
  24. 'playlist_mincount': 2,
  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 is unstable
  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. 'info_dict': {
  38. 'id': '342759',
  39. 'title': 'General Motors Ignition Switch Recall',
  40. },
  41. 'playlist_mincount': 6,
  42. }, {
  43. # Video from senate.gov
  44. 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
  45. 'info_dict': {
  46. 'id': 'judiciary031715',
  47. 'ext': 'mp4',
  48. 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
  49. },
  50. 'params': {
  51. 'skip_download': True, # m3u8 downloads
  52. }
  53. }, {
  54. # Ustream embedded video
  55. 'url': 'https://www.c-span.org/video/?114917-1/armed-services',
  56. 'info_dict': {
  57. 'id': '58428542',
  58. 'ext': 'flv',
  59. 'title': 'USHR07 Armed Services Committee',
  60. 'description': 'hsas00-2118-20150204-1000et-07\n\n\nUSHR07 Armed Services Committee',
  61. 'timestamp': 1423060374,
  62. 'upload_date': '20150204',
  63. 'uploader': 'HouseCommittee',
  64. 'uploader_id': '12987475',
  65. },
  66. }]
  67. def _real_extract(self, url):
  68. video_id = self._match_id(url)
  69. video_type = None
  70. webpage = self._download_webpage(url, video_id)
  71. ustream_url = UstreamIE._extract_url(webpage)
  72. if ustream_url:
  73. return self.url_result(ustream_url, UstreamIE.ie_key())
  74. # We first look for clipid, because clipprog always appears before
  75. patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')]
  76. results = list(filter(None, (re.search(p, webpage) for p in patterns)))
  77. if results:
  78. matches = results[0]
  79. video_type, video_id = matches.groups()
  80. video_type = 'clip' if video_type == 'id' else 'program'
  81. else:
  82. m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
  83. if m:
  84. video_id = m.group('id')
  85. video_type = 'program' if m.group('type') == 'prog' else 'clip'
  86. else:
  87. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  88. if senate_isvp_url:
  89. title = self._og_search_title(webpage)
  90. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  91. return self.url_result(surl, 'SenateISVP', video_id, title)
  92. if video_type is None or video_id is None:
  93. raise ExtractorError('unable to find video id and type')
  94. def get_text_attr(d, attr):
  95. return d.get(attr, {}).get('#text')
  96. data = self._download_json(
  97. 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
  98. video_id)['video']
  99. if data['@status'] != 'Success':
  100. raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
  101. doc = self._download_xml(
  102. 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
  103. video_id)
  104. description = self._html_search_meta('description', webpage)
  105. title = find_xpath_attr(doc, './/string', 'name', 'title').text
  106. thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
  107. files = data['files']
  108. capfile = get_text_attr(data, 'capfile')
  109. entries = []
  110. for partnum, f in enumerate(files):
  111. formats = []
  112. for quality in f['qualities']:
  113. formats.append({
  114. 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
  115. 'url': unescapeHTML(get_text_attr(quality, 'file')),
  116. 'height': int_or_none(get_text_attr(quality, 'height')),
  117. 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
  118. })
  119. if not formats:
  120. path = unescapeHTML(get_text_attr(f, 'path'))
  121. if not path:
  122. continue
  123. formats = self._extract_m3u8_formats(
  124. path, video_id, 'mp4', entry_protocol='m3u8_native',
  125. m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }]
  126. self._sort_formats(formats)
  127. entries.append({
  128. 'id': '%s_%d' % (video_id, partnum + 1),
  129. 'title': (
  130. title if len(files) == 1 else
  131. '%s part %d' % (title, partnum + 1)),
  132. 'formats': formats,
  133. 'description': description,
  134. 'thumbnail': thumbnail,
  135. 'duration': int_or_none(get_text_attr(f, 'length')),
  136. 'subtitles': {
  137. 'en': [{
  138. 'url': capfile,
  139. 'ext': determine_ext(capfile, 'dfxp')
  140. }],
  141. } if capfile else None,
  142. })
  143. if len(entries) == 1:
  144. entry = dict(entries[0])
  145. entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
  146. return entry
  147. else:
  148. return {
  149. '_type': 'playlist',
  150. 'entries': entries,
  151. 'title': title,
  152. 'id': 'c' + video_id if video_type == 'clip' else video_id,
  153. }