]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/adultswim.py
Merge remote-tracking branch 'SyxbEaEQ2/rate-limit'
[yt-dlp.git] / youtube_dl / extractor / adultswim.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .common import InfoExtractor
8 from ..utils import (
9 ExtractorError,
10 )
11
12
13 class AdultSwimIE(InfoExtractor):
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 'title': 'Rick and Morty - Pilot',
40 'description': "Rick moves in with his daughter's family and establishes himself as a bad influence on his grandson, Morty. "
41 }
42 }, {
43 'url': 'http://www.adultswim.com/videos/playlists/american-parenting/putting-francine-out-of-business/',
44 'playlist': [
45 {
46 'md5': '2eb5c06d0f9a1539da3718d897f13ec5',
47 'info_dict': {
48 'id': '-t8CamQlQ2aYZ49ItZCFog-0',
49 'ext': 'flv',
50 'title': 'American Dad - Putting Francine Out of Business',
51 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
52 },
53 }
54 ],
55 'info_dict': {
56 'title': 'American Dad - Putting Francine Out of Business',
57 'description': 'Stan hatches a plan to get Francine out of the real estate business.Watch more American Dad on [adult swim].'
58 },
59 }]
60
61 @staticmethod
62 def find_video_info(collection, slug):
63 for video in collection.get('videos'):
64 if video.get('slug') == slug:
65 return video
66
67 @staticmethod
68 def find_collection_by_linkURL(collections, linkURL):
69 for collection in collections:
70 if collection.get('linkURL') == linkURL:
71 return collection
72
73 @staticmethod
74 def find_collection_containing_video(collections, slug):
75 for collection in collections:
76 for video in collection.get('videos'):
77 if video.get('slug') == slug:
78 return collection, video
79
80 def _real_extract(self, url):
81 mobj = re.match(self._VALID_URL, url)
82 show_path = mobj.group('show_path')
83 episode_path = mobj.group('episode_path')
84 is_playlist = True if mobj.group('is_playlist') else False
85
86 webpage = self._download_webpage(url, episode_path)
87
88 # Extract the value of `bootstrappedData` from the Javascript in the page.
89 bootstrappedDataJS = self._search_regex(r'var bootstrappedData = ({.*});', webpage, episode_path)
90
91 try:
92 bootstrappedData = json.loads(bootstrappedDataJS)
93 except ValueError as ve:
94 errmsg = '%s: Failed to parse JSON ' % episode_path
95 raise ExtractorError(errmsg, cause=ve)
96
97 # Downloading videos from a /videos/playlist/ URL needs to be handled differently.
98 # NOTE: We are only downloading one video (the current one) not the playlist
99 if is_playlist:
100 collections = bootstrappedData['playlists']['collections']
101 collection = self.find_collection_by_linkURL(collections, show_path)
102 video_info = self.find_video_info(collection, episode_path)
103
104 show_title = video_info['showTitle']
105 segment_ids = [video_info['videoPlaybackID']]
106 else:
107 collections = bootstrappedData['show']['collections']
108 collection, video_info = self.find_collection_containing_video(collections, episode_path)
109
110 show = bootstrappedData['show']
111 show_title = show['title']
112 segment_ids = [clip['videoPlaybackID'] for clip in video_info['clips']]
113
114 episode_id = video_info['id']
115 episode_title = video_info['title']
116 episode_description = video_info['description']
117 episode_duration = video_info.get('duration')
118
119 entries = []
120 for part_num, segment_id in enumerate(segment_ids):
121 segment_url = 'http://www.adultswim.com/videos/api/v0/assets?id=%s&platform=mobile' % segment_id
122
123 segment_title = '%s - %s' % (show_title, episode_title)
124 if len(segment_ids) > 1:
125 segment_title += ' Part %d' % (part_num + 1)
126
127 idoc = self._download_xml(
128 segment_url, segment_title,
129 'Downloading segment information', 'Unable to download segment information')
130
131 segment_duration = idoc.find('.//trt').text.strip()
132
133 formats = []
134 file_els = idoc.findall('.//files/file')
135
136 for file_el in file_els:
137 bitrate = file_el.attrib.get('bitrate')
138 ftype = file_el.attrib.get('type')
139
140 formats.append({
141 'format_id': '%s_%s' % (bitrate, ftype),
142 'url': file_el.text.strip(),
143 # The bitrate may not be a number (for example: 'iphone')
144 'tbr': int(bitrate) if bitrate.isdigit() else None,
145 'quality': 1 if ftype == 'hd' else -1
146 })
147
148 self._sort_formats(formats)
149
150 entries.append({
151 'id': segment_id,
152 'title': segment_title,
153 'formats': formats,
154 'duration': segment_duration,
155 'description': episode_description
156 })
157
158 return {
159 '_type': 'playlist',
160 'id': episode_id,
161 'display_id': episode_path,
162 'entries': entries,
163 'title': '%s - %s' % (show_title, episode_title),
164 'description': episode_description,
165 'duration': episode_duration
166 }