]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/adultswim.py
Merge tag 'upstream/2015.07.21'
[youtubedl] / youtube_dl / extractor / adultswim.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 float_or_none,
10 xpath_text,
11 )
12
13
14 class AdultSwimIE(InfoExtractor):
15 _VALID_URL = r'https?://(?:www\.)?adultswim\.com/videos/(?P<is_playlist>playlists/)?(?P<show_path>[^/]+)/(?P<episode_path>[^/?#]+)/?'
16
17 _TESTS = [{
18 'url': 'http://adultswim.com/videos/rick-and-morty/pilot',
19 'playlist': [
20 {
21 'md5': '247572debc75c7652f253c8daa51a14d',
22 'info_dict': {
23 'id': 'rQxZvXQ4ROaSOqq-or2Mow-0',
24 'ext': 'flv',
25 'title': 'Rick and Morty - Pilot Part 1',
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 'md5': '77b0e037a4b20ec6b98671c4c379f48d',
31 'info_dict': {
32 'id': 'rQxZvXQ4ROaSOqq-or2Mow-3',
33 'ext': 'flv',
34 'title': 'Rick and Morty - Pilot Part 4',
35 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
36 },
37 },
38 ],
39 'info_dict': {
40 'id': 'rQxZvXQ4ROaSOqq-or2Mow',
41 'title': 'Rick and Morty - Pilot',
42 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
43 }
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': 'flv',
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 }]
81
82 @staticmethod
83 def find_video_info(collection, slug):
84 for video in collection.get('videos'):
85 if video.get('slug') == slug:
86 return video
87
88 @staticmethod
89 def find_collection_by_linkURL(collections, linkURL):
90 for collection in collections:
91 if collection.get('linkURL') == linkURL:
92 return collection
93
94 @staticmethod
95 def find_collection_containing_video(collections, slug):
96 for collection in collections:
97 for video in collection.get('videos'):
98 if video.get('slug') == slug:
99 return collection, video
100 return None, None
101
102 def _real_extract(self, url):
103 mobj = re.match(self._VALID_URL, url)
104 show_path = mobj.group('show_path')
105 episode_path = mobj.group('episode_path')
106 is_playlist = True if mobj.group('is_playlist') else False
107
108 webpage = self._download_webpage(url, episode_path)
109
110 # Extract the value of `bootstrappedData` from the Javascript in the page.
111 bootstrapped_data = self._parse_json(self._search_regex(
112 r'var bootstrappedData = ({.*});', webpage, 'bootstraped data'), episode_path)
113
114 # Downloading videos from a /videos/playlist/ URL needs to be handled differently.
115 # NOTE: We are only downloading one video (the current one) not the playlist
116 if is_playlist:
117 collections = bootstrapped_data['playlists']['collections']
118 collection = self.find_collection_by_linkURL(collections, show_path)
119 video_info = self.find_video_info(collection, episode_path)
120
121 show_title = video_info['showTitle']
122 segment_ids = [video_info['videoPlaybackID']]
123 else:
124 collections = bootstrapped_data['show']['collections']
125 collection, video_info = self.find_collection_containing_video(collections, episode_path)
126
127 # Video wasn't found in the collections, let's try `slugged_video`.
128 if video_info is None:
129 if bootstrapped_data.get('slugged_video', {}).get('slug') == episode_path:
130 video_info = bootstrapped_data['slugged_video']
131 else:
132 raise ExtractorError('Unable to find video info')
133
134 show = bootstrapped_data['show']
135 show_title = show['title']
136 segment_ids = [clip['videoPlaybackID'] for clip in video_info['clips']]
137
138 episode_id = video_info['id']
139 episode_title = video_info['title']
140 episode_description = video_info['description']
141 episode_duration = video_info.get('duration')
142
143 entries = []
144 for part_num, segment_id in enumerate(segment_ids):
145 segment_url = 'http://www.adultswim.com/videos/api/v0/assets?id=%s&platform=mobile' % segment_id
146
147 segment_title = '%s - %s' % (show_title, episode_title)
148 if len(segment_ids) > 1:
149 segment_title += ' Part %d' % (part_num + 1)
150
151 idoc = self._download_xml(
152 segment_url, segment_title,
153 'Downloading segment information', 'Unable to download segment information')
154
155 segment_duration = float_or_none(
156 xpath_text(idoc, './/trt', 'segment duration').strip())
157
158 formats = []
159 file_els = idoc.findall('.//files/file')
160
161 for file_el in file_els:
162 bitrate = file_el.attrib.get('bitrate')
163 ftype = file_el.attrib.get('type')
164
165 formats.append({
166 'format_id': '%s_%s' % (bitrate, ftype),
167 'url': file_el.text.strip(),
168 # The bitrate may not be a number (for example: 'iphone')
169 'tbr': int(bitrate) if bitrate.isdigit() else None,
170 'quality': 1 if ftype == 'hd' else -1
171 })
172
173 self._sort_formats(formats)
174
175 entries.append({
176 'id': segment_id,
177 'title': segment_title,
178 'formats': formats,
179 'duration': segment_duration,
180 'description': episode_description
181 })
182
183 return {
184 '_type': 'playlist',
185 'id': episode_id,
186 'display_id': episode_path,
187 'entries': entries,
188 'title': '%s - %s' % (show_title, episode_title),
189 'description': episode_description,
190 'duration': episode_duration
191 }