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.

116 lines
4.3 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. unescapeHTML,
  6. find_xpath_attr,
  7. )
  8. class CSpanIE(InfoExtractor):
  9. _VALID_URL = r'http://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
  10. IE_DESC = 'C-SPAN'
  11. _TESTS = [{
  12. 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
  13. 'md5': '8e44ce11f0f725527daccc453f553eb0',
  14. 'info_dict': {
  15. 'id': '315139',
  16. 'ext': 'mp4',
  17. 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
  18. 'description': 'Attorney General Eric Holder spoke 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 until Congress established new guidelines for review.',
  19. },
  20. 'skip': 'Regularly fails on travis, for unknown reasons',
  21. }, {
  22. 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
  23. # For whatever reason, the served video alternates between
  24. # two different ones
  25. #'md5': 'dbb0f047376d457f2ab8b3929cbb2d0c',
  26. 'info_dict': {
  27. 'id': '340723',
  28. 'ext': 'mp4',
  29. 'title': 'International Health Care Models',
  30. 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
  31. }
  32. }]
  33. def _real_extract(self, url):
  34. mobj = re.match(self._VALID_URL, url)
  35. page_id = mobj.group('id')
  36. webpage = self._download_webpage(url, page_id)
  37. video_id = self._search_regex(r'progid=\'?([0-9]+)\'?>', webpage, 'video id')
  38. description = self._html_search_regex(
  39. [
  40. # The full description
  41. r'<div class=\'expandable\'>(.*?)<a href=\'#\'',
  42. # If the description is small enough the other div is not
  43. # present, otherwise this is a stripped version
  44. r'<p class=\'initial\'>(.*?)</p>'
  45. ],
  46. webpage, 'description', flags=re.DOTALL)
  47. info_url = 'http://c-spanvideo.org/videoLibrary/assets/player/ajax-player.php?os=android&html5=program&id=' + video_id
  48. data = self._download_json(info_url, video_id)
  49. url = unescapeHTML(data['video']['files'][0]['path']['#text'])
  50. doc = self._download_xml('http://www.c-span.org/common/services/flashXml.php?programid=' + video_id + '&version=2014-01-23',
  51. video_id)
  52. formats = [
  53. {
  54. 'url': url,
  55. }
  56. ]
  57. def find_string(node, s):
  58. return find_xpath_attr(node, './/string', 'name', s).text
  59. def find_number(node, s):
  60. return int(find_xpath_attr(node, './/number', 'name', s).text)
  61. def find_array(node, s):
  62. return find_xpath_attr(node, './/array', 'name', s)
  63. def process_files(files, url, formats):
  64. for file in files:
  65. path = find_string(file, 'path')
  66. #duration = find_number(file, './number', 'name', 'length')
  67. hd = find_number(file, 'hd')
  68. formats.append({
  69. 'url': url,
  70. 'play_path': path,
  71. 'ext': 'flv',
  72. 'quality': hd,
  73. })
  74. def process_node(node, formats):
  75. url = find_xpath_attr(node, './string', 'name', 'url')
  76. if url is None:
  77. url = find_xpath_attr(node, './string', 'name', 'URL')
  78. if url is None:
  79. return
  80. url = url.text.replace('$(protocol)', 'rtmp').replace('$(port)', '1935')
  81. files = find_array(node, 'files')
  82. if files is None:
  83. return
  84. process_files(files, url, formats)
  85. process_node(doc.find('./media-link'), formats)
  86. streams = find_array(doc, 'streams')
  87. if streams is not None:
  88. for stream in streams:
  89. if find_string(stream, 'name') != 'vod':
  90. continue
  91. process_node(stream, formats)
  92. return {
  93. 'id': video_id,
  94. 'title': find_string(doc, 'title'),
  95. 'description': description,
  96. 'thumbnail': find_string(doc, 'poster'),
  97. 'formats': formats,
  98. }