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.

256 lines
9.7 KiB

10 years ago
11 years ago
  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': ['Andrew Coates', 'Brady Gaster', 'Mads Kristensen', 'Ed Blankenship', 'Patrick Klug'],
  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/Niners/Splendid22/Queue/76acff796e8f411184b008028e0d492b/RSS',
  64. 'info_dict': {
  65. 'id': 'Niners/Splendid22/Queue/76acff796e8f411184b008028e0d492b',
  66. 'title': 'Channel 9',
  67. },
  68. 'playlist_mincount': 100,
  69. }, {
  70. 'url': 'https://channel9.msdn.com/Events/DEVintersection/DEVintersection-2016/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. def _extract_list(self, video_id, rss_url=None):
  78. if not rss_url:
  79. rss_url = self._RSS_URL % video_id
  80. rss = self._download_xml(rss_url, video_id, 'Downloading RSS')
  81. entries = [self.url_result(session_url.text, 'Channel9')
  82. for session_url in rss.findall('./channel/item/link')]
  83. title_text = rss.find('./channel/title').text
  84. return self.playlist_result(entries, video_id, title_text)
  85. def _real_extract(self, url):
  86. content_path, rss = re.match(self._VALID_URL, url).groups()
  87. if rss:
  88. return self._extract_list(content_path, url)
  89. webpage = self._download_webpage(
  90. url, content_path, 'Downloading web page')
  91. episode_data = self._search_regex(
  92. r"data-episode='([^']+)'", webpage, 'episode data', default=None)
  93. if episode_data:
  94. episode_data = self._parse_json(unescapeHTML(
  95. episode_data), content_path)
  96. content_id = episode_data['contentId']
  97. is_session = '/Sessions(' in episode_data['api']
  98. content_url = 'https://channel9.msdn.com/odata' + episode_data['api']
  99. if is_session:
  100. content_url += '?$expand=Speakers'
  101. else:
  102. content_url += '?$expand=Authors'
  103. content_data = self._download_json(content_url, content_id)
  104. title = content_data['Title']
  105. QUALITIES = (
  106. 'mp3',
  107. 'wmv', 'mp4',
  108. 'wmv-low', 'mp4-low',
  109. 'wmv-mid', 'mp4-mid',
  110. 'wmv-high', 'mp4-high',
  111. )
  112. quality_key = qualities(QUALITIES)
  113. def quality(quality_id, format_url):
  114. return (len(QUALITIES) if '_Source.' in format_url
  115. else quality_key(quality_id))
  116. formats = []
  117. urls = set()
  118. SITE_QUALITIES = {
  119. 'MP3': 'mp3',
  120. 'MP4': 'mp4',
  121. 'Low Quality WMV': 'wmv-low',
  122. 'Low Quality MP4': 'mp4-low',
  123. 'Mid Quality WMV': 'wmv-mid',
  124. 'Mid Quality MP4': 'mp4-mid',
  125. 'High Quality WMV': 'wmv-high',
  126. 'High Quality MP4': 'mp4-high',
  127. }
  128. formats_select = self._search_regex(
  129. r'(?s)<select[^>]+name=["\']format[^>]+>(.+?)</select', webpage,
  130. 'formats select', default=None)
  131. if formats_select:
  132. for mobj in re.finditer(
  133. r'<option\b[^>]+\bvalue=(["\'])(?P<url>(?:(?!\1).)+)\1[^>]*>\s*(?P<format>[^<]+?)\s*<',
  134. formats_select):
  135. format_url = mobj.group('url')
  136. if format_url in urls:
  137. continue
  138. urls.add(format_url)
  139. format_id = mobj.group('format')
  140. quality_id = SITE_QUALITIES.get(format_id, format_id)
  141. formats.append({
  142. 'url': format_url,
  143. 'format_id': quality_id,
  144. 'quality': quality(quality_id, format_url),
  145. 'vcodec': 'none' if quality_id == 'mp3' else None,
  146. })
  147. API_QUALITIES = {
  148. 'VideoMP4Low': 'mp4-low',
  149. 'VideoWMV': 'wmv-mid',
  150. 'VideoMP4Medium': 'mp4-mid',
  151. 'VideoMP4High': 'mp4-high',
  152. 'VideoWMVHQ': 'wmv-hq',
  153. }
  154. for format_id, q in API_QUALITIES.items():
  155. q_url = content_data.get(format_id)
  156. if not q_url or q_url in urls:
  157. continue
  158. urls.add(q_url)
  159. formats.append({
  160. 'url': q_url,
  161. 'format_id': q,
  162. 'quality': quality(q, q_url),
  163. })
  164. self._sort_formats(formats)
  165. slides = content_data.get('Slides')
  166. zip_file = content_data.get('ZipFile')
  167. if not formats and not slides and not zip_file:
  168. raise ExtractorError(
  169. 'None of recording, slides or zip are available for %s' % content_path)
  170. subtitles = {}
  171. for caption in content_data.get('Captions', []):
  172. caption_url = caption.get('Url')
  173. if not caption_url:
  174. continue
  175. subtitles.setdefault(caption.get('Language', 'en'), []).append({
  176. 'url': caption_url,
  177. 'ext': 'vtt',
  178. })
  179. common = {
  180. 'id': content_id,
  181. 'title': title,
  182. 'description': clean_html(content_data.get('Description') or content_data.get('Body')),
  183. 'thumbnail': content_data.get('Thumbnail') or content_data.get('VideoPlayerPreviewImage'),
  184. 'duration': int_or_none(content_data.get('MediaLengthInSeconds')),
  185. 'timestamp': parse_iso8601(content_data.get('PublishedDate')),
  186. 'avg_rating': int_or_none(content_data.get('Rating')),
  187. 'rating_count': int_or_none(content_data.get('RatingCount')),
  188. 'view_count': int_or_none(content_data.get('Views')),
  189. 'comment_count': int_or_none(content_data.get('CommentCount')),
  190. 'subtitles': subtitles,
  191. }
  192. if is_session:
  193. speakers = []
  194. for s in content_data.get('Speakers', []):
  195. speaker_name = s.get('FullName')
  196. if not speaker_name:
  197. continue
  198. speakers.append(speaker_name)
  199. common.update({
  200. 'session_code': content_data.get('Code'),
  201. 'session_room': content_data.get('Room'),
  202. 'session_speakers': speakers,
  203. })
  204. else:
  205. authors = []
  206. for a in content_data.get('Authors', []):
  207. author_name = a.get('DisplayName')
  208. if not author_name:
  209. continue
  210. authors.append(author_name)
  211. common['authors'] = authors
  212. contents = []
  213. if slides:
  214. d = common.copy()
  215. d.update({'title': title + '-Slides', 'url': slides})
  216. contents.append(d)
  217. if zip_file:
  218. d = common.copy()
  219. d.update({'title': title + '-Zip', 'url': zip_file})
  220. contents.append(d)
  221. if formats:
  222. d = common.copy()
  223. d.update({'title': title, 'formats': formats})
  224. contents.append(d)
  225. return self.playlist_result(contents)
  226. else:
  227. return self._extract_list(content_path)