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.

283 lines
11 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. parse_filesize,
  7. qualities,
  8. )
  9. class Channel9IE(InfoExtractor):
  10. '''
  11. Common extractor for channel9.msdn.com.
  12. The type of provided URL (video or playlist) is determined according to
  13. meta Search.PageType from web page HTML rather than URL itself, as it is
  14. not always possible to do.
  15. '''
  16. IE_DESC = 'Channel 9'
  17. IE_NAME = 'channel9'
  18. _VALID_URL = r'https?://(?:www\.)?channel9\.msdn\.com/(?P<contentpath>.+)/?'
  19. _TESTS = [
  20. {
  21. 'url': 'http://channel9.msdn.com/Events/TechEd/Australia/2013/KOS002',
  22. 'md5': 'bbd75296ba47916b754e73c3a4bbdf10',
  23. 'info_dict': {
  24. 'id': 'Events/TechEd/Australia/2013/KOS002',
  25. 'ext': 'mp4',
  26. 'title': 'Developer Kick-Off Session: Stuff We Love',
  27. 'description': 'md5:c08d72240b7c87fcecafe2692f80e35f',
  28. 'duration': 4576,
  29. 'thumbnail': 're:http://.*\.jpg',
  30. 'session_code': 'KOS002',
  31. 'session_day': 'Day 1',
  32. 'session_room': 'Arena 1A',
  33. 'session_speakers': ['Ed Blankenship', 'Andrew Coates', 'Brady Gaster', 'Patrick Klug', 'Mads Kristensen'],
  34. },
  35. },
  36. {
  37. 'url': 'http://channel9.msdn.com/posts/Self-service-BI-with-Power-BI-nuclear-testing',
  38. 'md5': 'b43ee4529d111bc37ba7ee4f34813e68',
  39. 'info_dict': {
  40. 'id': 'posts/Self-service-BI-with-Power-BI-nuclear-testing',
  41. 'ext': 'mp4',
  42. 'title': 'Self-service BI with Power BI - nuclear testing',
  43. 'description': 'md5:d1e6ecaafa7fb52a2cacdf9599829f5b',
  44. 'duration': 1540,
  45. 'thumbnail': 're:http://.*\.jpg',
  46. 'authors': ['Mike Wilmot'],
  47. },
  48. },
  49. {
  50. # low quality mp4 is best
  51. 'url': 'https://channel9.msdn.com/Events/CPP/CppCon-2015/Ranges-for-the-Standard-Library',
  52. 'info_dict': {
  53. 'id': 'Events/CPP/CppCon-2015/Ranges-for-the-Standard-Library',
  54. 'ext': 'mp4',
  55. 'title': 'Ranges for the Standard Library',
  56. 'description': 'md5:2e6b4917677af3728c5f6d63784c4c5d',
  57. 'duration': 5646,
  58. 'thumbnail': 're:http://.*\.jpg',
  59. },
  60. 'params': {
  61. 'skip_download': True,
  62. },
  63. }
  64. ]
  65. _RSS_URL = 'http://channel9.msdn.com/%s/RSS'
  66. def _formats_from_html(self, html):
  67. FORMAT_REGEX = r'''
  68. (?x)
  69. <a\s+href="(?P<url>[^"]+)">(?P<quality>[^<]+)</a>\s*
  70. <span\s+class="usage">\((?P<note>[^\)]+)\)</span>\s*
  71. (?:<div\s+class="popup\s+rounded">\s*
  72. <h3>File\s+size</h3>\s*(?P<filesize>.*?)\s*
  73. </div>)? # File size part may be missing
  74. '''
  75. quality = qualities((
  76. 'MP3', 'MP4',
  77. 'Low Quality WMV', 'Low Quality MP4',
  78. 'Mid Quality WMV', 'Mid Quality MP4',
  79. 'High Quality WMV', 'High Quality MP4'))
  80. formats = [{
  81. 'url': x.group('url'),
  82. 'format_id': x.group('quality'),
  83. 'format_note': x.group('note'),
  84. 'format': '%s (%s)' % (x.group('quality'), x.group('note')),
  85. 'filesize_approx': parse_filesize(x.group('filesize')),
  86. 'quality': quality(x.group('quality')),
  87. 'vcodec': 'none' if x.group('note') == 'Audio only' else None,
  88. } for x in list(re.finditer(FORMAT_REGEX, html))]
  89. self._sort_formats(formats)
  90. return formats
  91. def _extract_title(self, html):
  92. title = self._html_search_meta('title', html, 'title')
  93. if title is None:
  94. title = self._og_search_title(html)
  95. TITLE_SUFFIX = ' (Channel 9)'
  96. if title is not None and title.endswith(TITLE_SUFFIX):
  97. title = title[:-len(TITLE_SUFFIX)]
  98. return title
  99. def _extract_description(self, html):
  100. DESCRIPTION_REGEX = r'''(?sx)
  101. <div\s+class="entry-content">\s*
  102. <div\s+id="entry-body">\s*
  103. (?P<description>.+?)\s*
  104. </div>\s*
  105. </div>
  106. '''
  107. m = re.search(DESCRIPTION_REGEX, html)
  108. if m is not None:
  109. return m.group('description')
  110. return self._html_search_meta('description', html, 'description')
  111. def _extract_duration(self, html):
  112. m = re.search(r'"length": *"(?P<hours>\d{2}):(?P<minutes>\d{2}):(?P<seconds>\d{2})"', html)
  113. return ((int(m.group('hours')) * 60 * 60) + (int(m.group('minutes')) * 60) + int(m.group('seconds'))) if m else None
  114. def _extract_slides(self, html):
  115. m = re.search(r'<a href="(?P<slidesurl>[^"]+)" class="slides">Slides</a>', html)
  116. return m.group('slidesurl') if m is not None else None
  117. def _extract_zip(self, html):
  118. m = re.search(r'<a href="(?P<zipurl>[^"]+)" class="zip">Zip</a>', html)
  119. return m.group('zipurl') if m is not None else None
  120. def _extract_avg_rating(self, html):
  121. m = re.search(r'<p class="avg-rating">Avg Rating: <span>(?P<avgrating>[^<]+)</span></p>', html)
  122. return float(m.group('avgrating')) if m is not None else 0
  123. def _extract_rating_count(self, html):
  124. m = re.search(r'<div class="rating-count">\((?P<ratingcount>[^<]+)\)</div>', html)
  125. return int(self._fix_count(m.group('ratingcount'))) if m is not None else 0
  126. def _extract_view_count(self, html):
  127. m = re.search(r'<li class="views">\s*<span class="count">(?P<viewcount>[^<]+)</span> Views\s*</li>', html)
  128. return int(self._fix_count(m.group('viewcount'))) if m is not None else 0
  129. def _extract_comment_count(self, html):
  130. m = re.search(r'<li class="comments">\s*<a href="#comments">\s*<span class="count">(?P<commentcount>[^<]+)</span> Comments\s*</a>\s*</li>', html)
  131. return int(self._fix_count(m.group('commentcount'))) if m is not None else 0
  132. def _fix_count(self, count):
  133. return int(str(count).replace(',', '')) if count is not None else None
  134. def _extract_authors(self, html):
  135. m = re.search(r'(?s)<li class="author">(.*?)</li>', html)
  136. if m is None:
  137. return None
  138. return re.findall(r'<a href="/Niners/[^"]+">([^<]+)</a>', m.group(1))
  139. def _extract_session_code(self, html):
  140. m = re.search(r'<li class="code">\s*(?P<code>.+?)\s*</li>', html)
  141. return m.group('code') if m is not None else None
  142. def _extract_session_day(self, html):
  143. m = re.search(r'<li class="day">\s*<a href="/Events/[^"]+">(?P<day>[^<]+)</a>\s*</li>', html)
  144. return m.group('day').strip() if m is not None else None
  145. def _extract_session_room(self, html):
  146. m = re.search(r'<li class="room">\s*(?P<room>.+?)\s*</li>', html)
  147. return m.group('room') if m is not None else None
  148. def _extract_session_speakers(self, html):
  149. return re.findall(r'<a href="/Events/Speakers/[^"]+">([^<]+)</a>', html)
  150. def _extract_content(self, html, content_path):
  151. # Look for downloadable content
  152. formats = self._formats_from_html(html)
  153. slides = self._extract_slides(html)
  154. zip_ = self._extract_zip(html)
  155. # Nothing to download
  156. if len(formats) == 0 and slides is None and zip_ is None:
  157. self._downloader.report_warning('None of recording, slides or zip are available for %s' % content_path)
  158. return
  159. # Extract meta
  160. title = self._extract_title(html)
  161. description = self._extract_description(html)
  162. thumbnail = self._og_search_thumbnail(html)
  163. duration = self._extract_duration(html)
  164. avg_rating = self._extract_avg_rating(html)
  165. rating_count = self._extract_rating_count(html)
  166. view_count = self._extract_view_count(html)
  167. comment_count = self._extract_comment_count(html)
  168. common = {
  169. '_type': 'video',
  170. 'id': content_path,
  171. 'description': description,
  172. 'thumbnail': thumbnail,
  173. 'duration': duration,
  174. 'avg_rating': avg_rating,
  175. 'rating_count': rating_count,
  176. 'view_count': view_count,
  177. 'comment_count': comment_count,
  178. }
  179. result = []
  180. if slides is not None:
  181. d = common.copy()
  182. d.update({'title': title + '-Slides', 'url': slides})
  183. result.append(d)
  184. if zip_ is not None:
  185. d = common.copy()
  186. d.update({'title': title + '-Zip', 'url': zip_})
  187. result.append(d)
  188. if len(formats) > 0:
  189. d = common.copy()
  190. d.update({'title': title, 'formats': formats})
  191. result.append(d)
  192. return result
  193. def _extract_entry_item(self, html, content_path):
  194. contents = self._extract_content(html, content_path)
  195. if contents is None:
  196. return contents
  197. if len(contents) > 1:
  198. raise ExtractorError('Got more than one entry')
  199. result = contents[0]
  200. result['authors'] = self._extract_authors(html)
  201. return result
  202. def _extract_session(self, html, content_path):
  203. contents = self._extract_content(html, content_path)
  204. if contents is None:
  205. return contents
  206. session_meta = {
  207. 'session_code': self._extract_session_code(html),
  208. 'session_day': self._extract_session_day(html),
  209. 'session_room': self._extract_session_room(html),
  210. 'session_speakers': self._extract_session_speakers(html),
  211. }
  212. for content in contents:
  213. content.update(session_meta)
  214. return self.playlist_result(contents)
  215. def _extract_list(self, content_path):
  216. rss = self._download_xml(self._RSS_URL % content_path, content_path, 'Downloading RSS')
  217. entries = [self.url_result(session_url.text, 'Channel9')
  218. for session_url in rss.findall('./channel/item/link')]
  219. title_text = rss.find('./channel/title').text
  220. return self.playlist_result(entries, content_path, title_text)
  221. def _real_extract(self, url):
  222. mobj = re.match(self._VALID_URL, url)
  223. content_path = mobj.group('contentpath')
  224. webpage = self._download_webpage(url, content_path, 'Downloading web page')
  225. page_type_m = re.search(r'<meta name="WT.entryid" content="(?P<pagetype>[^:]+)[^"]+"/>', webpage)
  226. if page_type_m is not None:
  227. page_type = page_type_m.group('pagetype')
  228. if page_type == 'Entry': # Any 'item'-like page, may contain downloadable content
  229. return self._extract_entry_item(webpage, content_path)
  230. elif page_type == 'Session': # Event session page, may contain downloadable content
  231. return self._extract_session(webpage, content_path)
  232. elif page_type == 'Event':
  233. return self._extract_list(content_path)
  234. else:
  235. raise ExtractorError('Unexpected WT.entryid %s' % page_type, expected=True)
  236. else: # Assuming list
  237. return self._extract_list(content_path)