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.

169 lines
6.6 KiB

10 years ago
10 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import json
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. xpath_text,
  9. float_or_none,
  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. '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. 'title': 'American Dad - Putting Francine Out of Business',
  54. 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
  55. },
  56. }]
  57. @staticmethod
  58. def find_video_info(collection, slug):
  59. for video in collection.get('videos'):
  60. if video.get('slug') == slug:
  61. return video
  62. @staticmethod
  63. def find_collection_by_linkURL(collections, linkURL):
  64. for collection in collections:
  65. if collection.get('linkURL') == linkURL:
  66. return collection
  67. @staticmethod
  68. def find_collection_containing_video(collections, slug):
  69. for collection in collections:
  70. for video in collection.get('videos'):
  71. if video.get('slug') == slug:
  72. return collection, video
  73. def _real_extract(self, url):
  74. mobj = re.match(self._VALID_URL, url)
  75. show_path = mobj.group('show_path')
  76. episode_path = mobj.group('episode_path')
  77. is_playlist = True if mobj.group('is_playlist') else False
  78. webpage = self._download_webpage(url, episode_path)
  79. # Extract the value of `bootstrappedData` from the Javascript in the page.
  80. bootstrappedDataJS = self._search_regex(r'var bootstrappedData = ({.*});', webpage, episode_path)
  81. try:
  82. bootstrappedData = json.loads(bootstrappedDataJS)
  83. except ValueError as ve:
  84. errmsg = '%s: Failed to parse JSON ' % episode_path
  85. raise ExtractorError(errmsg, cause=ve)
  86. # Downloading videos from a /videos/playlist/ URL needs to be handled differently.
  87. # NOTE: We are only downloading one video (the current one) not the playlist
  88. if is_playlist:
  89. collections = bootstrappedData['playlists']['collections']
  90. collection = self.find_collection_by_linkURL(collections, show_path)
  91. video_info = self.find_video_info(collection, episode_path)
  92. show_title = video_info['showTitle']
  93. segment_ids = [video_info['videoPlaybackID']]
  94. else:
  95. collections = bootstrappedData['show']['collections']
  96. collection, video_info = self.find_collection_containing_video(collections, episode_path)
  97. show = bootstrappedData['show']
  98. show_title = show['title']
  99. segment_ids = [clip['videoPlaybackID'] for clip in video_info['clips']]
  100. episode_id = video_info['id']
  101. episode_title = video_info['title']
  102. episode_description = video_info['description']
  103. episode_duration = video_info.get('duration')
  104. entries = []
  105. for part_num, segment_id in enumerate(segment_ids):
  106. segment_url = 'http://www.adultswim.com/videos/api/v0/assets?id=%s&platform=mobile' % segment_id
  107. segment_title = '%s - %s' % (show_title, episode_title)
  108. if len(segment_ids) > 1:
  109. segment_title += ' Part %d' % (part_num + 1)
  110. idoc = self._download_xml(
  111. segment_url, segment_title,
  112. 'Downloading segment information', 'Unable to download segment information')
  113. segment_duration = float_or_none(
  114. xpath_text(idoc, './/trt', 'segment duration').strip())
  115. formats = []
  116. file_els = idoc.findall('.//files/file')
  117. for file_el in file_els:
  118. bitrate = file_el.attrib.get('bitrate')
  119. ftype = file_el.attrib.get('type')
  120. formats.append({
  121. 'format_id': '%s_%s' % (bitrate, ftype),
  122. 'url': file_el.text.strip(),
  123. # The bitrate may not be a number (for example: 'iphone')
  124. 'tbr': int(bitrate) if bitrate.isdigit() else None,
  125. 'quality': 1 if ftype == 'hd' else -1
  126. })
  127. self._sort_formats(formats)
  128. entries.append({
  129. 'id': segment_id,
  130. 'title': segment_title,
  131. 'formats': formats,
  132. 'duration': segment_duration,
  133. 'description': episode_description
  134. })
  135. return {
  136. '_type': 'playlist',
  137. 'id': episode_id,
  138. 'display_id': episode_path,
  139. 'entries': entries,
  140. 'title': '%s - %s' % (show_title, episode_title),
  141. 'description': episode_description,
  142. 'duration': episode_duration
  143. }