[BehindKink] Minor fixes
[youtube-dl] / 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
8 class AdultSwimIE(InfoExtractor):
9     _VALID_URL = r'https?://video\.adultswim\.com/(?P<path>.+?)(?:\.html)?(?:\?.*)?(?:#.*)?$'
10     _TEST = {
11         'url': 'http://video.adultswim.com/rick-and-morty/close-rick-counters-of-the-rick-kind.html?x=y#title',
12         'playlist': [
13             {
14                 'md5': '4da359ec73b58df4575cd01a610ba5dc',
15                 'info_dict': {
16                     'id': '8a250ba1450996e901453d7f02ca02f5',
17                     'ext': 'flv',
18                     'title': 'Rick and Morty Close Rick-Counters of the Rick Kind part 1',
19                     'description': 'Rick has a run in with some old associates, resulting in a fallout with Morty. You got any chips, broh?',
20                     'uploader': 'Rick and Morty',
21                     'thumbnail': 'http://i.cdn.turner.com/asfix/repository/8a250ba13f865824013fc9db8b6b0400/thumbnail_267549017116827057.jpg'
22                 }
23             },
24             {
25                 'md5': 'ffbdf55af9331c509d95350bd0cc1819',
26                 'info_dict': {
27                     'id': '8a250ba1450996e901453d7f4bd102f6',
28                     'ext': 'flv',
29                     'title': 'Rick and Morty Close Rick-Counters of the Rick Kind part 2',
30                     'description': 'Rick has a run in with some old associates, resulting in a fallout with Morty. You got any chips, broh?',
31                     'uploader': 'Rick and Morty',
32                     'thumbnail': 'http://i.cdn.turner.com/asfix/repository/8a250ba13f865824013fc9db8b6b0400/thumbnail_267549017116827057.jpg'
33                 }
34             },
35             {
36                 'md5': 'b92409635540304280b4b6c36bd14a0a',
37                 'info_dict': {
38                     'id': '8a250ba1450996e901453d7fa73c02f7',
39                     'ext': 'flv',
40                     'title': 'Rick and Morty Close Rick-Counters of the Rick Kind part 3',
41                     'description': 'Rick has a run in with some old associates, resulting in a fallout with Morty. You got any chips, broh?',
42                     'uploader': 'Rick and Morty',
43                     'thumbnail': 'http://i.cdn.turner.com/asfix/repository/8a250ba13f865824013fc9db8b6b0400/thumbnail_267549017116827057.jpg'
44                 }
45             },
46             {
47                 'md5': 'e8818891d60e47b29cd89d7b0278156d',
48                 'info_dict': {
49                     'id': '8a250ba1450996e901453d7fc8ba02f8',
50                     'ext': 'flv',
51                     'title': 'Rick and Morty Close Rick-Counters of the Rick Kind part 4',
52                     'description': 'Rick has a run in with some old associates, resulting in a fallout with Morty. You got any chips, broh?',
53                     'uploader': 'Rick and Morty',
54                     'thumbnail': 'http://i.cdn.turner.com/asfix/repository/8a250ba13f865824013fc9db8b6b0400/thumbnail_267549017116827057.jpg'
55                 }
56             }
57         ]
58     }
59
60     _video_extensions = {
61         '3500': 'flv',
62         '640': 'mp4',
63         '150': 'mp4',
64         'ipad': 'm3u8',
65         'iphone': 'm3u8'
66     }
67     _video_dimensions = {
68         '3500': (1280, 720),
69         '640': (480, 270),
70         '150': (320, 180)
71     }
72
73     def _real_extract(self, url):
74         mobj = re.match(self._VALID_URL, url)
75         video_path = mobj.group('path')
76
77         webpage = self._download_webpage(url, video_path)
78         episode_id = self._html_search_regex(r'<link rel="video_src" href="http://i\.adultswim\.com/adultswim/adultswimtv/tools/swf/viralplayer.swf\?id=([0-9a-f]+?)"\s*/?\s*>', webpage, 'episode_id')
79         title = self._og_search_title(webpage)
80
81         index_url = 'http://asfix.adultswim.com/asfix-svc/episodeSearch/getEpisodesByIDs?networkName=AS&ids=%s' % episode_id
82         idoc = self._download_xml(index_url, title, 'Downloading episode index', 'Unable to download episode index')
83
84         episode_el = idoc.find('.//episode')
85         show_title = episode_el.attrib.get('collectionTitle')
86         episode_title = episode_el.attrib.get('title')
87         thumbnail = episode_el.attrib.get('thumbnailUrl')
88         description = episode_el.find('./description').text.strip()
89
90         entries = []
91         segment_els = episode_el.findall('./segments/segment')
92
93         for part_num, segment_el in enumerate(segment_els):
94             segment_id = segment_el.attrib.get('id')
95             segment_title = '%s %s part %d' % (show_title, episode_title, part_num + 1)
96             thumbnail = segment_el.attrib.get('thumbnailUrl')
97             duration = segment_el.attrib.get('duration')
98
99             segment_url = 'http://asfix.adultswim.com/asfix-svc/episodeservices/getCvpPlaylist?networkName=AS&id=%s' % segment_id
100             idoc = self._download_xml(segment_url, segment_title, 'Downloading segment information', 'Unable to download segment information')
101
102             formats = []
103             file_els = idoc.findall('.//files/file')
104
105             for file_el in file_els:
106                 bitrate = file_el.attrib.get('bitrate')
107                 type = file_el.attrib.get('type')
108                 width, height = self._video_dimensions.get(bitrate, (None, None))
109                 formats.append({
110                     'format_id': '%s-%s' % (bitrate, type),
111                     'url': file_el.text,
112                     'ext': self._video_extensions.get(bitrate, 'mp4'),
113                     # The bitrate may not be a number (for example: 'iphone')
114                     'tbr': int(bitrate) if bitrate.isdigit() else None,
115                     'height': height,
116                     'width': width
117                 })
118
119             self._sort_formats(formats)
120
121             entries.append({
122                 'id': segment_id,
123                 'title': segment_title,
124                 'formats': formats,
125                 'uploader': show_title,
126                 'thumbnail': thumbnail,
127                 'duration': duration,
128                 'description': description
129             })
130
131         return {
132             '_type': 'playlist',
133             'id': episode_id,
134             'display_id': video_path,
135             'entries': entries,
136             'title': '%s %s' % (show_title, episode_title),
137             'description': description,
138             'thumbnail': thumbnail
139         }