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.

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