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.

196 lines
7.8 KiB

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