logo

youtube-dl

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

channel9.py (10284B)


  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. clean_html,
  6. ExtractorError,
  7. int_or_none,
  8. parse_iso8601,
  9. qualities,
  10. unescapeHTML,
  11. )
  12. class Channel9IE(InfoExtractor):
  13. IE_DESC = 'Channel 9'
  14. IE_NAME = 'channel9'
  15. _VALID_URL = r'https?://(?:www\.)?(?:channel9\.msdn\.com|s\.ch9\.ms)/(?P<contentpath>.+?)(?P<rss>/RSS)?/?(?:[?#&]|$)'
  16. _TESTS = [{
  17. 'url': 'http://channel9.msdn.com/Events/TechEd/Australia/2013/KOS002',
  18. 'md5': '32083d4eaf1946db6d454313f44510ca',
  19. 'info_dict': {
  20. 'id': '6c413323-383a-49dc-88f9-a22800cab024',
  21. 'ext': 'wmv',
  22. 'title': 'Developer Kick-Off Session: Stuff We Love',
  23. 'description': 'md5:b80bf9355a503c193aff7ec6cd5a7731',
  24. 'duration': 4576,
  25. 'thumbnail': r're:https?://.*\.jpg',
  26. 'timestamp': 1377717420,
  27. 'upload_date': '20130828',
  28. 'session_code': 'KOS002',
  29. 'session_room': 'Arena 1A',
  30. 'session_speakers': 'count:5',
  31. },
  32. }, {
  33. 'url': 'http://channel9.msdn.com/posts/Self-service-BI-with-Power-BI-nuclear-testing',
  34. 'md5': 'dcf983ee6acd2088e7188c3cf79b46bc',
  35. 'info_dict': {
  36. 'id': 'fe8e435f-bb93-4e01-8e97-a28c01887024',
  37. 'ext': 'wmv',
  38. 'title': 'Self-service BI with Power BI - nuclear testing',
  39. 'description': 'md5:2d17fec927fc91e9e17783b3ecc88f54',
  40. 'duration': 1540,
  41. 'thumbnail': r're:https?://.*\.jpg',
  42. 'timestamp': 1386381991,
  43. 'upload_date': '20131207',
  44. 'authors': ['Mike Wilmot'],
  45. },
  46. }, {
  47. # low quality mp4 is best
  48. 'url': 'https://channel9.msdn.com/Events/CPP/CppCon-2015/Ranges-for-the-Standard-Library',
  49. 'info_dict': {
  50. 'id': '33ad69d2-6a4e-4172-83a1-a523013dec76',
  51. 'ext': 'mp4',
  52. 'title': 'Ranges for the Standard Library',
  53. 'description': 'md5:9895e0a9fd80822d2f01c454b8f4a372',
  54. 'duration': 5646,
  55. 'thumbnail': r're:https?://.*\.jpg',
  56. 'upload_date': '20150930',
  57. 'timestamp': 1443640735,
  58. },
  59. 'params': {
  60. 'skip_download': True,
  61. },
  62. }, {
  63. 'url': 'https://channel9.msdn.com/Events/DEVintersection/DEVintersection-2016/RSS',
  64. 'info_dict': {
  65. 'id': 'Events/DEVintersection/DEVintersection-2016',
  66. 'title': 'DEVintersection 2016 Orlando Sessions',
  67. },
  68. 'playlist_mincount': 14,
  69. }, {
  70. 'url': 'https://channel9.msdn.com/Niners/Splendid22/Queue/76acff796e8f411184b008028e0d492b/RSS',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'https://channel9.msdn.com/Events/Speakers/scott-hanselman/RSS?UrlSafeName=scott-hanselman',
  74. 'only_matching': True,
  75. }]
  76. _RSS_URL = 'http://channel9.msdn.com/%s/RSS'
  77. @staticmethod
  78. def _extract_urls(webpage):
  79. return re.findall(
  80. r'<iframe[^>]+src=["\'](https?://channel9\.msdn\.com/(?:[^/]+/)+)player\b',
  81. webpage)
  82. def _extract_list(self, video_id, rss_url=None):
  83. if not rss_url:
  84. rss_url = self._RSS_URL % video_id
  85. rss = self._download_xml(rss_url, video_id, 'Downloading RSS')
  86. entries = [self.url_result(session_url.text, 'Channel9')
  87. for session_url in rss.findall('./channel/item/link')]
  88. title_text = rss.find('./channel/title').text
  89. return self.playlist_result(entries, video_id, title_text)
  90. def _real_extract(self, url):
  91. content_path, rss = re.match(self._VALID_URL, url).groups()
  92. if rss:
  93. return self._extract_list(content_path, url)
  94. webpage = self._download_webpage(
  95. url, content_path, 'Downloading web page')
  96. episode_data = self._search_regex(
  97. r"data-episode='([^']+)'", webpage, 'episode data', default=None)
  98. if episode_data:
  99. episode_data = self._parse_json(unescapeHTML(
  100. episode_data), content_path)
  101. content_id = episode_data['contentId']
  102. is_session = '/Sessions(' in episode_data['api']
  103. content_url = 'https://channel9.msdn.com/odata' + episode_data['api'] + '?$select=Captions,CommentCount,MediaLengthInSeconds,PublishedDate,Rating,RatingCount,Title,VideoMP4High,VideoMP4Low,VideoMP4Medium,VideoPlayerPreviewImage,VideoWMV,VideoWMVHQ,Views,'
  104. if is_session:
  105. content_url += 'Code,Description,Room,Slides,Speakers,ZipFile&$expand=Speakers'
  106. else:
  107. content_url += 'Authors,Body&$expand=Authors'
  108. content_data = self._download_json(content_url, content_id)
  109. title = content_data['Title']
  110. QUALITIES = (
  111. 'mp3',
  112. 'wmv', 'mp4',
  113. 'wmv-low', 'mp4-low',
  114. 'wmv-mid', 'mp4-mid',
  115. 'wmv-high', 'mp4-high',
  116. )
  117. quality_key = qualities(QUALITIES)
  118. def quality(quality_id, format_url):
  119. return (len(QUALITIES) if '_Source.' in format_url
  120. else quality_key(quality_id))
  121. formats = []
  122. urls = set()
  123. SITE_QUALITIES = {
  124. 'MP3': 'mp3',
  125. 'MP4': 'mp4',
  126. 'Low Quality WMV': 'wmv-low',
  127. 'Low Quality MP4': 'mp4-low',
  128. 'Mid Quality WMV': 'wmv-mid',
  129. 'Mid Quality MP4': 'mp4-mid',
  130. 'High Quality WMV': 'wmv-high',
  131. 'High Quality MP4': 'mp4-high',
  132. }
  133. formats_select = self._search_regex(
  134. r'(?s)<select[^>]+name=["\']format[^>]+>(.+?)</select', webpage,
  135. 'formats select', default=None)
  136. if formats_select:
  137. for mobj in re.finditer(
  138. r'<option\b[^>]+\bvalue=(["\'])(?P<url>(?:(?!\1).)+)\1[^>]*>\s*(?P<format>[^<]+?)\s*<',
  139. formats_select):
  140. format_url = mobj.group('url')
  141. if format_url in urls:
  142. continue
  143. urls.add(format_url)
  144. format_id = mobj.group('format')
  145. quality_id = SITE_QUALITIES.get(format_id, format_id)
  146. formats.append({
  147. 'url': format_url,
  148. 'format_id': quality_id,
  149. 'quality': quality(quality_id, format_url),
  150. 'vcodec': 'none' if quality_id == 'mp3' else None,
  151. })
  152. API_QUALITIES = {
  153. 'VideoMP4Low': 'mp4-low',
  154. 'VideoWMV': 'wmv-mid',
  155. 'VideoMP4Medium': 'mp4-mid',
  156. 'VideoMP4High': 'mp4-high',
  157. 'VideoWMVHQ': 'wmv-hq',
  158. }
  159. for format_id, q in API_QUALITIES.items():
  160. q_url = content_data.get(format_id)
  161. if not q_url or q_url in urls:
  162. continue
  163. urls.add(q_url)
  164. formats.append({
  165. 'url': q_url,
  166. 'format_id': q,
  167. 'quality': quality(q, q_url),
  168. })
  169. self._sort_formats(formats)
  170. slides = content_data.get('Slides')
  171. zip_file = content_data.get('ZipFile')
  172. if not formats and not slides and not zip_file:
  173. raise ExtractorError(
  174. 'None of recording, slides or zip are available for %s' % content_path)
  175. subtitles = {}
  176. for caption in content_data.get('Captions', []):
  177. caption_url = caption.get('Url')
  178. if not caption_url:
  179. continue
  180. subtitles.setdefault(caption.get('Language', 'en'), []).append({
  181. 'url': caption_url,
  182. 'ext': 'vtt',
  183. })
  184. common = {
  185. 'id': content_id,
  186. 'title': title,
  187. 'description': clean_html(content_data.get('Description') or content_data.get('Body')),
  188. 'thumbnail': content_data.get('VideoPlayerPreviewImage'),
  189. 'duration': int_or_none(content_data.get('MediaLengthInSeconds')),
  190. 'timestamp': parse_iso8601(content_data.get('PublishedDate')),
  191. 'avg_rating': int_or_none(content_data.get('Rating')),
  192. 'rating_count': int_or_none(content_data.get('RatingCount')),
  193. 'view_count': int_or_none(content_data.get('Views')),
  194. 'comment_count': int_or_none(content_data.get('CommentCount')),
  195. 'subtitles': subtitles,
  196. }
  197. if is_session:
  198. speakers = []
  199. for s in content_data.get('Speakers', []):
  200. speaker_name = s.get('FullName')
  201. if not speaker_name:
  202. continue
  203. speakers.append(speaker_name)
  204. common.update({
  205. 'session_code': content_data.get('Code'),
  206. 'session_room': content_data.get('Room'),
  207. 'session_speakers': speakers,
  208. })
  209. else:
  210. authors = []
  211. for a in content_data.get('Authors', []):
  212. author_name = a.get('DisplayName')
  213. if not author_name:
  214. continue
  215. authors.append(author_name)
  216. common['authors'] = authors
  217. contents = []
  218. if slides:
  219. d = common.copy()
  220. d.update({'title': title + '-Slides', 'url': slides})
  221. contents.append(d)
  222. if zip_file:
  223. d = common.copy()
  224. d.update({'title': title + '-Zip', 'url': zip_file})
  225. contents.append(d)
  226. if formats:
  227. d = common.copy()
  228. d.update({'title': title, 'formats': formats})
  229. contents.append(d)
  230. return self.playlist_result(contents)
  231. else:
  232. return self._extract_list(content_path)