channel9.py 9.9 KB

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