adultswim.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .turner import TurnerBaseIE
  5. from ..utils import ExtractorError
  6. class AdultSwimIE(TurnerBaseIE):
  7. _VALID_URL = r'https?://(?:www\.)?adultswim\.com/videos/(?P<is_playlist>playlists/)?(?P<show_path>[^/]+)/(?P<episode_path>[^/?#]+)/?'
  8. _TESTS = [{
  9. 'url': 'http://adultswim.com/videos/rick-and-morty/pilot',
  10. 'playlist': [
  11. {
  12. 'md5': '247572debc75c7652f253c8daa51a14d',
  13. 'info_dict': {
  14. 'id': 'rQxZvXQ4ROaSOqq-or2Mow-0',
  15. 'ext': 'flv',
  16. 'title': 'Rick and Morty - Pilot Part 1',
  17. 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
  18. },
  19. },
  20. {
  21. 'md5': '77b0e037a4b20ec6b98671c4c379f48d',
  22. 'info_dict': {
  23. 'id': 'rQxZvXQ4ROaSOqq-or2Mow-3',
  24. 'ext': 'flv',
  25. 'title': 'Rick and Morty - Pilot Part 4',
  26. 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
  27. },
  28. },
  29. ],
  30. 'info_dict': {
  31. 'id': 'rQxZvXQ4ROaSOqq-or2Mow',
  32. 'title': 'Rick and Morty - Pilot',
  33. 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
  34. },
  35. 'skip': 'This video is only available for registered users',
  36. }, {
  37. 'url': 'http://www.adultswim.com/videos/playlists/american-parenting/putting-francine-out-of-business/',
  38. 'playlist': [
  39. {
  40. 'md5': '2eb5c06d0f9a1539da3718d897f13ec5',
  41. 'info_dict': {
  42. 'id': '-t8CamQlQ2aYZ49ItZCFog-0',
  43. 'ext': 'flv',
  44. 'title': 'American Dad - Putting Francine Out of Business',
  45. 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
  46. },
  47. }
  48. ],
  49. 'info_dict': {
  50. 'id': '-t8CamQlQ2aYZ49ItZCFog',
  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. 'url': 'http://www.adultswim.com/videos/tim-and-eric-awesome-show-great-job/dr-steve-brule-for-your-wine/',
  56. 'playlist': [
  57. {
  58. 'md5': '3e346a2ab0087d687a05e1e7f3b3e529',
  59. 'info_dict': {
  60. 'id': 'sY3cMUR_TbuE4YmdjzbIcQ-0',
  61. 'ext': 'mp4',
  62. 'title': 'Tim and Eric Awesome Show Great Job! - Dr. Steve Brule, For Your Wine',
  63. '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',
  64. },
  65. }
  66. ],
  67. 'info_dict': {
  68. 'id': 'sY3cMUR_TbuE4YmdjzbIcQ',
  69. 'title': 'Tim and Eric Awesome Show Great Job! - Dr. Steve Brule, For Your Wine',
  70. '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',
  71. },
  72. 'params': {
  73. # m3u8 download
  74. 'skip_download': True,
  75. }
  76. }, {
  77. # heroMetadata.trailer
  78. 'url': 'http://www.adultswim.com/videos/decker/inside-decker-a-new-hero/',
  79. 'info_dict': {
  80. 'id': 'I0LQFQkaSUaFp8PnAWHhoQ',
  81. 'ext': 'mp4',
  82. 'title': 'Decker - Inside Decker: A New Hero',
  83. 'description': 'md5:c916df071d425d62d70c86d4399d3ee0',
  84. 'duration': 249.008,
  85. },
  86. 'params': {
  87. # m3u8 download
  88. 'skip_download': True,
  89. },
  90. 'expected_warnings': ['Unable to download f4m manifest'],
  91. }]
  92. @staticmethod
  93. def find_video_info(collection, slug):
  94. for video in collection.get('videos'):
  95. if video.get('slug') == slug:
  96. return video
  97. @staticmethod
  98. def find_collection_by_linkURL(collections, linkURL):
  99. for collection in collections:
  100. if collection.get('linkURL') == linkURL:
  101. return collection
  102. @staticmethod
  103. def find_collection_containing_video(collections, slug):
  104. for collection in collections:
  105. for video in collection.get('videos'):
  106. if video.get('slug') == slug:
  107. return collection, video
  108. return None, None
  109. def _real_extract(self, url):
  110. mobj = re.match(self._VALID_URL, url)
  111. show_path = mobj.group('show_path')
  112. episode_path = mobj.group('episode_path')
  113. is_playlist = True if mobj.group('is_playlist') else False
  114. webpage = self._download_webpage(url, episode_path)
  115. # Extract the value of `bootstrappedData` from the Javascript in the page.
  116. bootstrapped_data = self._parse_json(self._search_regex(
  117. r'var bootstrappedData = ({.*});', webpage, 'bootstraped data'), episode_path)
  118. # Downloading videos from a /videos/playlist/ URL needs to be handled differently.
  119. # NOTE: We are only downloading one video (the current one) not the playlist
  120. if is_playlist:
  121. collections = bootstrapped_data['playlists']['collections']
  122. collection = self.find_collection_by_linkURL(collections, show_path)
  123. video_info = self.find_video_info(collection, episode_path)
  124. show_title = video_info['showTitle']
  125. segment_ids = [video_info['videoPlaybackID']]
  126. else:
  127. collections = bootstrapped_data['show']['collections']
  128. collection, video_info = self.find_collection_containing_video(collections, episode_path)
  129. # Video wasn't found in the collections, let's try `slugged_video`.
  130. if video_info is None:
  131. if bootstrapped_data.get('slugged_video', {}).get('slug') == episode_path:
  132. video_info = bootstrapped_data['slugged_video']
  133. if not video_info:
  134. video_info = bootstrapped_data.get('heroMetadata', {}).get('trailer').get('video')
  135. if not video_info:
  136. raise ExtractorError('Unable to find video info')
  137. show = bootstrapped_data['show']
  138. show_title = show['title']
  139. stream = video_info.get('stream')
  140. if stream and stream.get('videoPlaybackID'):
  141. segment_ids = [stream['videoPlaybackID']]
  142. elif video_info.get('clips'):
  143. segment_ids = [clip['videoPlaybackID'] for clip in video_info['clips']]
  144. elif video_info.get('videoPlaybackID'):
  145. segment_ids = [video_info['videoPlaybackID']]
  146. else:
  147. if video_info.get('auth') is True:
  148. raise ExtractorError(
  149. 'This video is only available via cable service provider subscription that'
  150. ' is not currently supported. You may want to use --cookies.', expected=True)
  151. else:
  152. raise ExtractorError('Unable to find stream or clips')
  153. episode_id = video_info['id']
  154. episode_title = video_info['title']
  155. episode_description = video_info['description']
  156. episode_duration = video_info.get('duration')
  157. entries = []
  158. for part_num, segment_id in enumerate(segment_ids):
  159. segement_info = self._extract_cvp_info(
  160. 'http://www.adultswim.com/videos/api/v0/assets?id=%s&platform=desktop' % segment_id,
  161. segment_id, {
  162. 'secure': {
  163. 'media_src': 'http://androidhls-secure.cdn.turner.com/adultswim/big',
  164. 'tokenizer_src': 'http://www.adultswim.com/astv/mvpd/processors/services/token_ipadAdobe.do',
  165. },
  166. })
  167. segment_title = '%s - %s' % (show_title, episode_title)
  168. if len(segment_ids) > 1:
  169. segment_title += ' Part %d' % (part_num + 1)
  170. segement_info.update({
  171. 'id': segment_id,
  172. 'title': segment_title,
  173. 'description': episode_description,
  174. })
  175. entries.append(segement_info)
  176. return {
  177. '_type': 'playlist',
  178. 'id': episode_id,
  179. 'display_id': episode_path,
  180. 'entries': entries,
  181. 'title': '%s - %s' % (show_title, episode_title),
  182. 'description': episode_description,
  183. 'duration': episode_duration
  184. }