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.

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