cspan.py 6.9 KB

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