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