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.

133 lines
5.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'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. webpage = self._download_webpage(url, video_id)
  57. matches = re.search(r'data-(prog|clip)id=\'([0-9]+)\'', webpage)
  58. if matches:
  59. video_type, video_id = matches.groups()
  60. if video_type == 'prog':
  61. video_type = 'program'
  62. else:
  63. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  64. if senate_isvp_url:
  65. title = self._og_search_title(webpage)
  66. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  67. return self.url_result(surl, 'SenateISVP', video_id, title)
  68. def get_text_attr(d, attr):
  69. return d.get(attr, {}).get('#text')
  70. data = self._download_json(
  71. 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
  72. video_id)['video']
  73. if data['@status'] != 'Success':
  74. raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
  75. doc = self._download_xml(
  76. 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
  77. video_id)
  78. description = self._html_search_meta('description', webpage)
  79. title = find_xpath_attr(doc, './/string', 'name', 'title').text
  80. thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
  81. files = data['files']
  82. capfile = get_text_attr(data, 'capfile')
  83. entries = []
  84. for partnum, f in enumerate(files):
  85. formats = []
  86. for quality in f['qualities']:
  87. formats.append({
  88. 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
  89. 'url': unescapeHTML(get_text_attr(quality, 'file')),
  90. 'height': int_or_none(get_text_attr(quality, 'height')),
  91. 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
  92. })
  93. self._sort_formats(formats)
  94. entries.append({
  95. 'id': '%s_%d' % (video_id, partnum + 1),
  96. 'title': (
  97. title if len(files) == 1 else
  98. '%s part %d' % (title, partnum + 1)),
  99. 'formats': formats,
  100. 'description': description,
  101. 'thumbnail': thumbnail,
  102. 'duration': int_or_none(get_text_attr(f, 'length')),
  103. 'subtitles': {
  104. 'en': [{
  105. 'url': capfile,
  106. 'ext': determine_ext(capfile, 'dfxp')
  107. }],
  108. } if capfile else None,
  109. })
  110. if len(entries) == 1:
  111. entry = dict(entries[0])
  112. entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
  113. return entry
  114. else:
  115. return {
  116. '_type': 'playlist',
  117. 'entries': entries,
  118. 'title': title,
  119. 'id': 'c' + video_id if video_type == 'clip' else video_id,
  120. }