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.

206 lines
8.7 KiB

10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. ExtractorError,
  8. float_or_none,
  9. xpath_text,
  10. )
  11. class AdultSwimIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?:www\.)?adultswim\.com/videos/(?P<is_playlist>playlists/)?(?P<show_path>[^/]+)/(?P<episode_path>[^/?#]+)/?'
  13. _TESTS = [{
  14. 'url': 'http://adultswim.com/videos/rick-and-morty/pilot',
  15. 'playlist': [
  16. {
  17. 'md5': '247572debc75c7652f253c8daa51a14d',
  18. 'info_dict': {
  19. 'id': 'rQxZvXQ4ROaSOqq-or2Mow-0',
  20. 'ext': 'flv',
  21. 'title': 'Rick and Morty - Pilot Part 1',
  22. 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
  23. },
  24. },
  25. {
  26. 'md5': '77b0e037a4b20ec6b98671c4c379f48d',
  27. 'info_dict': {
  28. 'id': 'rQxZvXQ4ROaSOqq-or2Mow-3',
  29. 'ext': 'flv',
  30. 'title': 'Rick and Morty - Pilot Part 4',
  31. 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
  32. },
  33. },
  34. ],
  35. 'info_dict': {
  36. 'id': 'rQxZvXQ4ROaSOqq-or2Mow',
  37. 'title': 'Rick and Morty - Pilot',
  38. 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
  39. }
  40. }, {
  41. 'url': 'http://www.adultswim.com/videos/playlists/american-parenting/putting-francine-out-of-business/',
  42. 'playlist': [
  43. {
  44. 'md5': '2eb5c06d0f9a1539da3718d897f13ec5',
  45. 'info_dict': {
  46. 'id': '-t8CamQlQ2aYZ49ItZCFog-0',
  47. 'ext': 'flv',
  48. 'title': 'American Dad - Putting Francine Out of Business',
  49. 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
  50. },
  51. }
  52. ],
  53. 'info_dict': {
  54. 'id': '-t8CamQlQ2aYZ49ItZCFog',
  55. 'title': 'American Dad - Putting Francine Out of Business',
  56. 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
  57. },
  58. }, {
  59. 'url': 'http://www.adultswim.com/videos/tim-and-eric-awesome-show-great-job/dr-steve-brule-for-your-wine/',
  60. 'playlist': [
  61. {
  62. 'md5': '3e346a2ab0087d687a05e1e7f3b3e529',
  63. 'info_dict': {
  64. 'id': 'sY3cMUR_TbuE4YmdjzbIcQ-0',
  65. 'ext': 'flv',
  66. 'title': 'Tim and Eric Awesome Show Great Job! - Dr. Steve Brule, For Your Wine',
  67. 'description': 'Dr. Brule reports live from Wine Country with a special report on wines. \r\nWatch Tim and Eric Awesome Show Great Job! episode #20, "Embarrassed" on Adult Swim.\r\n\r\n',
  68. },
  69. }
  70. ],
  71. 'info_dict': {
  72. 'id': 'sY3cMUR_TbuE4YmdjzbIcQ',
  73. 'title': 'Tim and Eric Awesome Show Great Job! - Dr. Steve Brule, For Your Wine',
  74. 'description': 'Dr. Brule reports live from Wine Country with a special report on wines. \r\nWatch Tim and Eric Awesome Show Great Job! episode #20, "Embarrassed" on Adult Swim.\r\n\r\n',
  75. },
  76. }]
  77. @staticmethod
  78. def find_video_info(collection, slug):
  79. for video in collection.get('videos'):
  80. if video.get('slug') == slug:
  81. return video
  82. @staticmethod
  83. def find_collection_by_linkURL(collections, linkURL):
  84. for collection in collections:
  85. if collection.get('linkURL') == linkURL:
  86. return collection
  87. @staticmethod
  88. def find_collection_containing_video(collections, slug):
  89. for collection in collections:
  90. for video in collection.get('videos'):
  91. if video.get('slug') == slug:
  92. return collection, video
  93. return None, None
  94. def _real_extract(self, url):
  95. mobj = re.match(self._VALID_URL, url)
  96. show_path = mobj.group('show_path')
  97. episode_path = mobj.group('episode_path')
  98. is_playlist = True if mobj.group('is_playlist') else False
  99. webpage = self._download_webpage(url, episode_path)
  100. # Extract the value of `bootstrappedData` from the Javascript in the page.
  101. bootstrapped_data = self._parse_json(self._search_regex(
  102. r'var bootstrappedData = ({.*});', webpage, 'bootstraped data'), episode_path)
  103. # Downloading videos from a /videos/playlist/ URL needs to be handled differently.
  104. # NOTE: We are only downloading one video (the current one) not the playlist
  105. if is_playlist:
  106. collections = bootstrapped_data['playlists']['collections']
  107. collection = self.find_collection_by_linkURL(collections, show_path)
  108. video_info = self.find_video_info(collection, episode_path)
  109. show_title = video_info['showTitle']
  110. segment_ids = [video_info['videoPlaybackID']]
  111. else:
  112. collections = bootstrapped_data['show']['collections']
  113. collection, video_info = self.find_collection_containing_video(collections, episode_path)
  114. # Video wasn't found in the collections, let's try `slugged_video`.
  115. if video_info is None:
  116. if bootstrapped_data.get('slugged_video', {}).get('slug') == episode_path:
  117. video_info = bootstrapped_data['slugged_video']
  118. else:
  119. raise ExtractorError('Unable to find video info')
  120. show = bootstrapped_data['show']
  121. show_title = show['title']
  122. stream = video_info.get('stream')
  123. clips = [stream] if stream else video_info['clips']
  124. segment_ids = [clip['videoPlaybackID'] for clip in clips]
  125. episode_id = video_info['id']
  126. episode_title = video_info['title']
  127. episode_description = video_info['description']
  128. episode_duration = video_info.get('duration')
  129. entries = []
  130. for part_num, segment_id in enumerate(segment_ids):
  131. segment_url = 'http://www.adultswim.com/videos/api/v0/assets?id=%s&platform=desktop' % segment_id
  132. segment_title = '%s - %s' % (show_title, episode_title)
  133. if len(segment_ids) > 1:
  134. segment_title += ' Part %d' % (part_num + 1)
  135. idoc = self._download_xml(
  136. segment_url, segment_title,
  137. 'Downloading segment information', 'Unable to download segment information')
  138. segment_duration = float_or_none(
  139. xpath_text(idoc, './/trt', 'segment duration').strip())
  140. formats = []
  141. file_els = idoc.findall('.//files/file') or idoc.findall('./files/file')
  142. unique_urls = []
  143. unique_file_els = []
  144. for file_el in file_els:
  145. media_url = file_el.text
  146. if not media_url or determine_ext(media_url) == 'f4m':
  147. continue
  148. if file_el.text not in unique_urls:
  149. unique_urls.append(file_el.text)
  150. unique_file_els.append(file_el)
  151. for file_el in unique_file_els:
  152. bitrate = file_el.attrib.get('bitrate')
  153. ftype = file_el.attrib.get('type')
  154. media_url = file_el.text
  155. if determine_ext(media_url) == 'm3u8':
  156. formats.extend(self._extract_m3u8_formats(
  157. media_url, segment_title, 'mp4', 'm3u8_native', preference=0, m3u8_id='hls'))
  158. else:
  159. formats.append({
  160. 'format_id': '%s_%s' % (bitrate, ftype),
  161. 'url': file_el.text.strip(),
  162. # The bitrate may not be a number (for example: 'iphone')
  163. 'tbr': int(bitrate) if bitrate.isdigit() else None,
  164. })
  165. self._sort_formats(formats)
  166. entries.append({
  167. 'id': segment_id,
  168. 'title': segment_title,
  169. 'formats': formats,
  170. 'duration': segment_duration,
  171. 'description': episode_description
  172. })
  173. return {
  174. '_type': 'playlist',
  175. 'id': episode_id,
  176. 'display_id': episode_path,
  177. 'entries': entries,
  178. 'title': '%s - %s' % (show_title, episode_title),
  179. 'description': episode_description,
  180. 'duration': episode_duration
  181. }