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