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.

277 lines
11 KiB

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