cspan.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. def get_text_attr(d, attr):
  14. return d.get(attr, {}).get('#text')
  15. class CSpanIE(InfoExtractor):
  16. _VALID_URL = r'http://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
  17. IE_DESC = 'C-SPAN'
  18. _TESTS = [{
  19. 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
  20. 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
  21. 'info_dict': {
  22. 'id': '315139',
  23. 'ext': 'mp4',
  24. 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
  25. '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.',
  26. },
  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': '8e5fbfabe6ad0f89f3012a7943c1287b',
  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. 'md5': '2ae5051559169baadba13fc35345ae74',
  40. 'info_dict': {
  41. 'id': '342759',
  42. 'ext': 'mp4',
  43. 'title': 'General Motors Ignition Switch Recall',
  44. 'duration': 14848,
  45. 'description': 'md5:118081aedd24bf1d3b68b3803344e7f3'
  46. },
  47. }, {
  48. # Video from senate.gov
  49. 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
  50. 'info_dict': {
  51. 'id': 'judiciary031715',
  52. 'ext': 'flv',
  53. 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
  54. }
  55. }]
  56. def _real_extract(self, url):
  57. video_id = self._match_id(url)
  58. webpage = self._download_webpage(url, video_id)
  59. matches = re.search(r'data-(prog|clip)id=\'([0-9]+)\'', webpage)
  60. if matches:
  61. video_type, video_id = matches.groups()
  62. if video_type == 'prog':
  63. video_type = 'program'
  64. else:
  65. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  66. if senate_isvp_url:
  67. title = self._og_search_title(webpage)
  68. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  69. return self.url_result(surl, 'SenateISVP', video_id, title)
  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. }