logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

cspan.py (10277B)


  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. ExtractorError,
  7. extract_attributes,
  8. find_xpath_attr,
  9. get_element_by_attribute,
  10. get_element_by_class,
  11. int_or_none,
  12. js_to_json,
  13. merge_dicts,
  14. parse_iso8601,
  15. smuggle_url,
  16. str_to_int,
  17. unescapeHTML,
  18. )
  19. from .senateisvp import SenateISVPIE
  20. from .ustream import UstreamIE
  21. class CSpanIE(InfoExtractor):
  22. _VALID_URL = r'https?://(?:www\.)?c-span\.org/video/\?(?P<id>[0-9a-f]+)'
  23. IE_DESC = 'C-SPAN'
  24. _TESTS = [{
  25. 'url': 'http://www.c-span.org/video/?313572-1/HolderonV',
  26. 'md5': '94b29a4f131ff03d23471dd6f60b6a1d',
  27. 'info_dict': {
  28. 'id': '315139',
  29. 'title': 'Attorney General Eric Holder on Voting Rights Act Decision',
  30. },
  31. 'playlist_mincount': 2,
  32. 'skip': 'Regularly fails on travis, for unknown reasons',
  33. }, {
  34. 'url': 'http://www.c-span.org/video/?c4486943/cspan-international-health-care-models',
  35. # md5 is unstable
  36. 'info_dict': {
  37. 'id': 'c4486943',
  38. 'ext': 'mp4',
  39. 'title': 'CSPAN - International Health Care Models',
  40. 'description': 'md5:7a985a2d595dba00af3d9c9f0783c967',
  41. }
  42. }, {
  43. 'url': 'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall',
  44. 'info_dict': {
  45. 'id': '342759',
  46. 'title': 'General Motors Ignition Switch Recall',
  47. },
  48. 'playlist_mincount': 6,
  49. }, {
  50. # Video from senate.gov
  51. 'url': 'http://www.c-span.org/video/?104517-1/immigration-reforms-needed-protect-skilled-american-workers',
  52. 'info_dict': {
  53. 'id': 'judiciary031715',
  54. 'ext': 'mp4',
  55. 'title': 'Immigration Reforms Needed to Protect Skilled American Workers',
  56. },
  57. 'params': {
  58. 'skip_download': True, # m3u8 downloads
  59. }
  60. }, {
  61. # Ustream embedded video
  62. 'url': 'https://www.c-span.org/video/?114917-1/armed-services',
  63. 'info_dict': {
  64. 'id': '58428542',
  65. 'ext': 'flv',
  66. 'title': 'USHR07 Armed Services Committee',
  67. 'description': 'hsas00-2118-20150204-1000et-07\n\n\nUSHR07 Armed Services Committee',
  68. 'timestamp': 1423060374,
  69. 'upload_date': '20150204',
  70. 'uploader': 'HouseCommittee',
  71. 'uploader_id': '12987475',
  72. },
  73. }, {
  74. # Audio Only
  75. 'url': 'https://www.c-span.org/video/?437336-1/judiciary-antitrust-competition-policy-consumer-rights',
  76. 'only_matching': True,
  77. }]
  78. BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/%s_%s/index.html?videoId=%s'
  79. def _real_extract(self, url):
  80. video_id = self._match_id(url)
  81. video_type = None
  82. webpage = self._download_webpage(url, video_id)
  83. ustream_url = UstreamIE._extract_url(webpage)
  84. if ustream_url:
  85. return self.url_result(ustream_url, UstreamIE.ie_key())
  86. if '&vod' not in url:
  87. bc = self._search_regex(
  88. r"(<[^>]+id='brightcove-player-embed'[^>]+>)",
  89. webpage, 'brightcove embed', default=None)
  90. if bc:
  91. bc_attr = extract_attributes(bc)
  92. bc_url = self.BRIGHTCOVE_URL_TEMPLATE % (
  93. bc_attr.get('data-bcaccountid', '3162030207001'),
  94. bc_attr.get('data-noprebcplayerid', 'SyGGpuJy3g'),
  95. bc_attr.get('data-newbcplayerid', 'default'),
  96. bc_attr['data-bcid'])
  97. return self.url_result(smuggle_url(bc_url, {'source_url': url}))
  98. def add_referer(formats):
  99. for f in formats:
  100. f.setdefault('http_headers', {})['Referer'] = url
  101. # As of 01.12.2020 this path looks to cover all cases making the rest
  102. # of the code unnecessary
  103. jwsetup = self._parse_json(
  104. self._search_regex(
  105. r'(?s)jwsetup\s*=\s*({.+?})\s*;', webpage, 'jwsetup',
  106. default='{}'),
  107. video_id, transform_source=js_to_json, fatal=False)
  108. if jwsetup:
  109. info = self._parse_jwplayer_data(
  110. jwsetup, video_id, require_title=False, m3u8_id='hls',
  111. base_url=url)
  112. add_referer(info['formats'])
  113. for subtitles in info['subtitles'].values():
  114. for subtitle in subtitles:
  115. ext = determine_ext(subtitle['url'])
  116. if ext == 'php':
  117. ext = 'vtt'
  118. subtitle['ext'] = ext
  119. ld_info = self._search_json_ld(webpage, video_id, default={})
  120. title = get_element_by_class('video-page-title', webpage) or \
  121. self._og_search_title(webpage)
  122. description = get_element_by_attribute('itemprop', 'description', webpage) or \
  123. self._html_search_meta(['og:description', 'description'], webpage)
  124. return merge_dicts(info, ld_info, {
  125. 'title': title,
  126. 'thumbnail': get_element_by_attribute('itemprop', 'thumbnailUrl', webpage),
  127. 'description': description,
  128. 'timestamp': parse_iso8601(get_element_by_attribute('itemprop', 'uploadDate', webpage)),
  129. 'location': get_element_by_attribute('itemprop', 'contentLocation', webpage),
  130. 'duration': int_or_none(self._search_regex(
  131. r'jwsetup\.seclength\s*=\s*(\d+);',
  132. webpage, 'duration', fatal=False)),
  133. 'view_count': str_to_int(self._search_regex(
  134. r"<span[^>]+class='views'[^>]*>([\d,]+)\s+Views</span>",
  135. webpage, 'views', fatal=False)),
  136. })
  137. # Obsolete
  138. # We first look for clipid, because clipprog always appears before
  139. patterns = [r'id=\'clip(%s)\'\s*value=\'([0-9]+)\'' % t for t in ('id', 'prog')]
  140. results = list(filter(None, (re.search(p, webpage) for p in patterns)))
  141. if results:
  142. matches = results[0]
  143. video_type, video_id = matches.groups()
  144. video_type = 'clip' if video_type == 'id' else 'program'
  145. else:
  146. m = re.search(r'data-(?P<type>clip|prog)id=["\'](?P<id>\d+)', webpage)
  147. if m:
  148. video_id = m.group('id')
  149. video_type = 'program' if m.group('type') == 'prog' else 'clip'
  150. else:
  151. senate_isvp_url = SenateISVPIE._search_iframe_url(webpage)
  152. if senate_isvp_url:
  153. title = self._og_search_title(webpage)
  154. surl = smuggle_url(senate_isvp_url, {'force_title': title})
  155. return self.url_result(surl, 'SenateISVP', video_id, title)
  156. video_id = self._search_regex(
  157. r'jwsetup\.clipprog\s*=\s*(\d+);',
  158. webpage, 'jwsetup program id', default=None)
  159. if video_id:
  160. video_type = 'program'
  161. if video_type is None or video_id is None:
  162. error_message = get_element_by_class('VLplayer-error-message', webpage)
  163. if error_message:
  164. raise ExtractorError(error_message)
  165. raise ExtractorError('unable to find video id and type')
  166. def get_text_attr(d, attr):
  167. return d.get(attr, {}).get('#text')
  168. data = self._download_json(
  169. 'http://www.c-span.org/assets/player/ajax-player.php?os=android&html5=%s&id=%s' % (video_type, video_id),
  170. video_id)['video']
  171. if data['@status'] != 'Success':
  172. raise ExtractorError('%s said: %s' % (self.IE_NAME, get_text_attr(data, 'error')), expected=True)
  173. doc = self._download_xml(
  174. 'http://www.c-span.org/common/services/flashXml.php?%sid=%s' % (video_type, video_id),
  175. video_id)
  176. description = self._html_search_meta('description', webpage)
  177. title = find_xpath_attr(doc, './/string', 'name', 'title').text
  178. thumbnail = find_xpath_attr(doc, './/string', 'name', 'poster').text
  179. files = data['files']
  180. capfile = get_text_attr(data, 'capfile')
  181. entries = []
  182. for partnum, f in enumerate(files):
  183. formats = []
  184. for quality in f.get('qualities', []):
  185. formats.append({
  186. 'format_id': '%s-%sp' % (get_text_attr(quality, 'bitrate'), get_text_attr(quality, 'height')),
  187. 'url': unescapeHTML(get_text_attr(quality, 'file')),
  188. 'height': int_or_none(get_text_attr(quality, 'height')),
  189. 'tbr': int_or_none(get_text_attr(quality, 'bitrate')),
  190. })
  191. if not formats:
  192. path = unescapeHTML(get_text_attr(f, 'path'))
  193. if not path:
  194. continue
  195. formats = self._extract_m3u8_formats(
  196. path, video_id, 'mp4', entry_protocol='m3u8_native',
  197. m3u8_id='hls') if determine_ext(path) == 'm3u8' else [{'url': path, }]
  198. add_referer(formats)
  199. self._sort_formats(formats)
  200. entries.append({
  201. 'id': '%s_%d' % (video_id, partnum + 1),
  202. 'title': (
  203. title if len(files) == 1 else
  204. '%s part %d' % (title, partnum + 1)),
  205. 'formats': formats,
  206. 'description': description,
  207. 'thumbnail': thumbnail,
  208. 'duration': int_or_none(get_text_attr(f, 'length')),
  209. 'subtitles': {
  210. 'en': [{
  211. 'url': capfile,
  212. 'ext': determine_ext(capfile, 'dfxp')
  213. }],
  214. } if capfile else None,
  215. })
  216. if len(entries) == 1:
  217. entry = dict(entries[0])
  218. entry['id'] = 'c' + video_id if video_type == 'clip' else video_id
  219. return entry
  220. else:
  221. return {
  222. '_type': 'playlist',
  223. 'entries': entries,
  224. 'title': title,
  225. 'id': 'c' + video_id if video_type == 'clip' else video_id,
  226. }