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.

266 lines
11 KiB

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