cspan.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. )
  11. from .senateisvp import SenateISVPIE
  12. class CSpanIE(InfoExtractor):
  13. _VALID_URL = r'http://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
  14. IE_DESC = 'C-SPAN'
  15. _TESTS = [{
  16. 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
  17. 'md5': '067803f994e049b455a58b16e5aab442',
  18. 'info_dict': {
  19. 'id': '315139',
  20. 'ext': 'mp4',
  21. 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
  22. '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.',
  23. },
  24. 'skip': 'Regularly fails on travis, for unknown reasons',
  25. }, {
  26. 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
  27. 'md5': '4eafd1e91a75d2b1e6a3cbd0995816a2',
  28. 'info_dict': {
  29. 'id': 'c4486943',
  30. 'ext': 'mp4',
  31. 'title': 'CSPAN - International Health Care Models',
  32. 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
  33. }
  34. }, {
  35. 'url': 'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall',
  36. 'md5': '446562a736c6bf97118e389433ed88d4',
  37. 'info_dict': {
  38. 'id': '342759',
  39. 'ext': 'mp4',
  40. 'title': 'General Motors Ignition Switch Recall',
  41. 'duration': 14848,
  42. 'description': 'md5:118081aedd24bf1d3b68b3803344e7f3'
  43. },
  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': 'flv',
  50. 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
  51. }
  52. }]
  53. def _real_extract(self, url):
  54. video_id = self._match_id(url)
  55. webpage = self._download_webpage(url, video_id)
  56. matches = re.search(r'data-(prog|clip)id=\'([0-9]+)\'', webpage)
  57. if matches:
  58. video_type, video_id = matches.groups()
  59. if video_type == 'prog':
  60. video_type = 'program'
  61. else:
  62. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  63. if senate_isvp_url:
  64. title = self._og_search_title(webpage)
  65. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  66. return self.url_result(surl, 'SenateISVP', video_id, title)
  67. data = self._download_json(
  68. 'http://c-spanvideo.org/videoLibrary/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
  69. video_id)
  70. doc = self._download_xml(
  71. 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
  72. video_id)
  73. description = self._html_search_meta('description', webpage)
  74. title = find_xpath_attr(doc, './/string', 'name', 'title').text
  75. thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
  76. files = data['video']['files']
  77. try:
  78. capfile = data['video']['capfile']['#text']
  79. except KeyError:
  80. capfile = None
  81. entries = [{
  82. 'id': '%s_%d' % (video_id, partnum + 1),
  83. 'title': (
  84. title if len(files) == 1 else
  85. '%s part %d' % (title, partnum + 1)),
  86. 'url': unescapeHTML(f['path']['#text']),
  87. 'description': description,
  88. 'thumbnail': thumbnail,
  89. 'duration': int_or_none(f.get('length', {}).get('#text')),
  90. 'subtitles': {
  91. 'en': [{
  92. 'url': capfile,
  93. 'ext': determine_ext(capfile, 'dfxp')
  94. }],
  95. } if capfile else None,
  96. } for partnum, f in enumerate(files)]
  97. if len(entries) == 1:
  98. entry = dict(entries[0])
  99. entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
  100. return entry
  101. else:
  102. return {
  103. '_type': 'playlist',
  104. 'entries': entries,
  105. 'title': title,
  106. 'id': 'c' + video_id if video_type == 'clip' else video_id,
  107. }