2 from __future__ import unicode_literals
7 from .common import InfoExtractor
15 class AdultSwimIE(InfoExtractor):
16 _VALID_URL = r'https?://(?:www\.)?adultswim\.com/videos/(?P<is_playlist>playlists/)?(?P<show_path>[^/]+)/(?P<episode_path>[^/?#]+)/?'
19 'url': 'http://adultswim.com/videos/rick-and-morty/pilot',
22 'md5': '247572debc75c7652f253c8daa51a14d',
24 'id': 'rQxZvXQ4ROaSOqq-or2Mow-0',
26 'title': 'Rick and Morty - Pilot Part 1',
27 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
31 'md5': '77b0e037a4b20ec6b98671c4c379f48d',
33 'id': 'rQxZvXQ4ROaSOqq-or2Mow-3',
35 'title': 'Rick and Morty - Pilot Part 4',
36 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
41 'id': 'rQxZvXQ4ROaSOqq-or2Mow',
42 'title': 'Rick and Morty - Pilot',
43 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
46 'url': 'http://www.adultswim.com/videos/playlists/american-parenting/putting-francine-out-of-business/',
49 'md5': '2eb5c06d0f9a1539da3718d897f13ec5',
51 'id': '-t8CamQlQ2aYZ49ItZCFog-0',
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].'
59 'id': '-t8CamQlQ2aYZ49ItZCFog',
60 'title': 'American Dad - Putting Francine Out of Business',
61 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
66 def find_video_info(collection, slug):
67 for video in collection.get('videos'):
68 if video.get('slug') == slug:
72 def find_collection_by_linkURL(collections, linkURL):
73 for collection in collections:
74 if collection.get('linkURL') == linkURL:
78 def find_collection_containing_video(collections, slug):
79 for collection in collections:
80 for video in collection.get('videos'):
81 if video.get('slug') == slug:
82 return collection, video
84 def _real_extract(self, url):
85 mobj = re.match(self._VALID_URL, url)
86 show_path = mobj.group('show_path')
87 episode_path = mobj.group('episode_path')
88 is_playlist = True if mobj.group('is_playlist') else False
90 webpage = self._download_webpage(url, episode_path)
92 # Extract the value of `bootstrappedData` from the Javascript in the page.
93 bootstrappedDataJS = self._search_regex(r'var bootstrappedData = ({.*});', webpage, episode_path)
96 bootstrappedData = json.loads(bootstrappedDataJS)
97 except ValueError as ve:
98 errmsg = '%s: Failed to parse JSON ' % episode_path
99 raise ExtractorError(errmsg, cause=ve)
101 # Downloading videos from a /videos/playlist/ URL needs to be handled differently.
102 # NOTE: We are only downloading one video (the current one) not the playlist
104 collections = bootstrappedData['playlists']['collections']
105 collection = self.find_collection_by_linkURL(collections, show_path)
106 video_info = self.find_video_info(collection, episode_path)
108 show_title = video_info['showTitle']
109 segment_ids = [video_info['videoPlaybackID']]
111 collections = bootstrappedData['show']['collections']
112 collection, video_info = self.find_collection_containing_video(collections, episode_path)
114 show = bootstrappedData['show']
115 show_title = show['title']
116 segment_ids = [clip['videoPlaybackID'] for clip in video_info['clips']]
118 episode_id = video_info['id']
119 episode_title = video_info['title']
120 episode_description = video_info['description']
121 episode_duration = video_info.get('duration')
124 for part_num, segment_id in enumerate(segment_ids):
125 segment_url = 'http://www.adultswim.com/videos/api/v0/assets?id=%s&platform=mobile' % segment_id
127 segment_title = '%s - %s' % (show_title, episode_title)
128 if len(segment_ids) > 1:
129 segment_title += ' Part %d' % (part_num + 1)
131 idoc = self._download_xml(
132 segment_url, segment_title,
133 'Downloading segment information', 'Unable to download segment information')
135 segment_duration = float_or_none(
136 xpath_text(idoc, './/trt', 'segment duration').strip())
139 file_els = idoc.findall('.//files/file')
141 for file_el in file_els:
142 bitrate = file_el.attrib.get('bitrate')
143 ftype = file_el.attrib.get('type')
146 'format_id': '%s_%s' % (bitrate, ftype),
147 'url': file_el.text.strip(),
148 # The bitrate may not be a number (for example: 'iphone')
149 'tbr': int(bitrate) if bitrate.isdigit() else None,
150 'quality': 1 if ftype == 'hd' else -1
153 self._sort_formats(formats)
157 'title': segment_title,
159 'duration': segment_duration,
160 'description': episode_description
166 'display_id': episode_path,
168 'title': '%s - %s' % (show_title, episode_title),
169 'description': episode_description,
170 'duration': episode_duration