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.

239 lines
10 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. 'skip': 'This video is only available for registered users',
  41. }, {
  42. 'url': 'http://www.adultswim.com/videos/playlists/american-parenting/putting-francine-out-of-business/',
  43. 'playlist': [
  44. {
  45. 'md5': '2eb5c06d0f9a1539da3718d897f13ec5',
  46. 'info_dict': {
  47. 'id': '-t8CamQlQ2aYZ49ItZCFog-0',
  48. 'ext': 'flv',
  49. 'title': 'American Dad - Putting Francine Out of Business',
  50. 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
  51. },
  52. }
  53. ],
  54. 'info_dict': {
  55. 'id': '-t8CamQlQ2aYZ49ItZCFog',
  56. 'title': 'American Dad - Putting Francine Out of Business',
  57. 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
  58. },
  59. }, {
  60. 'url': 'http://www.adultswim.com/videos/tim-and-eric-awesome-show-great-job/dr-steve-brule-for-your-wine/',
  61. 'playlist': [
  62. {
  63. 'md5': '3e346a2ab0087d687a05e1e7f3b3e529',
  64. 'info_dict': {
  65. 'id': 'sY3cMUR_TbuE4YmdjzbIcQ-0',
  66. 'ext': 'mp4',
  67. 'title': 'Tim and Eric Awesome Show Great Job! - Dr. Steve Brule, For Your Wine',
  68. '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',
  69. },
  70. }
  71. ],
  72. 'info_dict': {
  73. 'id': 'sY3cMUR_TbuE4YmdjzbIcQ',
  74. 'title': 'Tim and Eric Awesome Show Great Job! - Dr. Steve Brule, For Your Wine',
  75. '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',
  76. },
  77. 'params': {
  78. # m3u8 download
  79. 'skip_download': True,
  80. }
  81. }, {
  82. # heroMetadata.trailer
  83. 'url': 'http://www.adultswim.com/videos/decker/inside-decker-a-new-hero/',
  84. 'info_dict': {
  85. 'id': 'I0LQFQkaSUaFp8PnAWHhoQ',
  86. 'ext': 'mp4',
  87. 'title': 'Decker - Inside Decker: A New Hero',
  88. 'description': 'md5:c916df071d425d62d70c86d4399d3ee0',
  89. 'duration': 249.008,
  90. },
  91. 'params': {
  92. # m3u8 download
  93. 'skip_download': True,
  94. }
  95. }]
  96. @staticmethod
  97. def find_video_info(collection, slug):
  98. for video in collection.get('videos'):
  99. if video.get('slug') == slug:
  100. return video
  101. @staticmethod
  102. def find_collection_by_linkURL(collections, linkURL):
  103. for collection in collections:
  104. if collection.get('linkURL') == linkURL:
  105. return collection
  106. @staticmethod
  107. def find_collection_containing_video(collections, slug):
  108. for collection in collections:
  109. for video in collection.get('videos'):
  110. if video.get('slug') == slug:
  111. return collection, video
  112. return None, None
  113. def _real_extract(self, url):
  114. mobj = re.match(self._VALID_URL, url)
  115. show_path = mobj.group('show_path')
  116. episode_path = mobj.group('episode_path')
  117. is_playlist = True if mobj.group('is_playlist') else False
  118. webpage = self._download_webpage(url, episode_path)
  119. # Extract the value of `bootstrappedData` from the Javascript in the page.
  120. bootstrapped_data = self._parse_json(self._search_regex(
  121. r'var bootstrappedData = ({.*});', webpage, 'bootstraped data'), episode_path)
  122. # Downloading videos from a /videos/playlist/ URL needs to be handled differently.
  123. # NOTE: We are only downloading one video (the current one) not the playlist
  124. if is_playlist:
  125. collections = bootstrapped_data['playlists']['collections']
  126. collection = self.find_collection_by_linkURL(collections, show_path)
  127. video_info = self.find_video_info(collection, episode_path)
  128. show_title = video_info['showTitle']
  129. segment_ids = [video_info['videoPlaybackID']]
  130. else:
  131. collections = bootstrapped_data['show']['collections']
  132. collection, video_info = self.find_collection_containing_video(collections, episode_path)
  133. # Video wasn't found in the collections, let's try `slugged_video`.
  134. if video_info is None:
  135. if bootstrapped_data.get('slugged_video', {}).get('slug') == episode_path:
  136. video_info = bootstrapped_data['slugged_video']
  137. if not video_info:
  138. video_info = bootstrapped_data.get('heroMetadata', {}).get('trailer').get('video')
  139. if not video_info:
  140. raise ExtractorError('Unable to find video info')
  141. show = bootstrapped_data['show']
  142. show_title = show['title']
  143. stream = video_info.get('stream')
  144. if stream and stream.get('videoPlaybackID'):
  145. segment_ids = [stream['videoPlaybackID']]
  146. elif video_info.get('clips'):
  147. segment_ids = [clip['videoPlaybackID'] for clip in video_info['clips']]
  148. elif video_info.get('videoPlaybackID'):
  149. segment_ids = [video_info['videoPlaybackID']]
  150. else:
  151. if video_info.get('auth') is True:
  152. raise ExtractorError(
  153. 'This video is only available via cable service provider subscription that'
  154. ' is not currently supported. You may want to use --cookies.', expected=True)
  155. else:
  156. raise ExtractorError('Unable to find stream or clips')
  157. episode_id = video_info['id']
  158. episode_title = video_info['title']
  159. episode_description = video_info['description']
  160. episode_duration = video_info.get('duration')
  161. entries = []
  162. for part_num, segment_id in enumerate(segment_ids):
  163. segment_url = 'http://www.adultswim.com/videos/api/v0/assets?id=%s&platform=desktop' % segment_id
  164. segment_title = '%s - %s' % (show_title, episode_title)
  165. if len(segment_ids) > 1:
  166. segment_title += ' Part %d' % (part_num + 1)
  167. idoc = self._download_xml(
  168. segment_url, segment_title,
  169. 'Downloading segment information', 'Unable to download segment information')
  170. segment_duration = float_or_none(
  171. xpath_text(idoc, './/trt', 'segment duration').strip())
  172. formats = []
  173. file_els = idoc.findall('.//files/file') or idoc.findall('./files/file')
  174. unique_urls = []
  175. unique_file_els = []
  176. for file_el in file_els:
  177. media_url = file_el.text
  178. if not media_url or determine_ext(media_url) == 'f4m':
  179. continue
  180. if file_el.text not in unique_urls:
  181. unique_urls.append(file_el.text)
  182. unique_file_els.append(file_el)
  183. for file_el in unique_file_els:
  184. bitrate = file_el.attrib.get('bitrate')
  185. ftype = file_el.attrib.get('type')
  186. media_url = file_el.text
  187. if determine_ext(media_url) == 'm3u8':
  188. formats.extend(self._extract_m3u8_formats(
  189. media_url, segment_title, 'mp4', preference=0,
  190. m3u8_id='hls', fatal=False))
  191. else:
  192. formats.append({
  193. 'format_id': '%s_%s' % (bitrate, ftype),
  194. 'url': file_el.text.strip(),
  195. # The bitrate may not be a number (for example: 'iphone')
  196. 'tbr': int(bitrate) if bitrate.isdigit() else None,
  197. })
  198. self._sort_formats(formats)
  199. entries.append({
  200. 'id': segment_id,
  201. 'title': segment_title,
  202. 'formats': formats,
  203. 'duration': segment_duration,
  204. 'description': episode_description
  205. })
  206. return {
  207. '_type': 'playlist',
  208. 'id': episode_id,
  209. 'display_id': episode_path,
  210. 'entries': entries,
  211. 'title': '%s - %s' % (show_title, episode_title),
  212. 'description': episode_description,
  213. 'duration': episode_duration
  214. }