cspan.py 5.3 KB

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