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.

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