]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/comedycentral.py
Fix imports and general cleanup
[youtube-dl.git] / youtube_dl / extractor / comedycentral.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .mtv import MTVServicesInfoExtractor
6 from ..compat import (
7     compat_str,
8     compat_urllib_parse,
9 )
10 from ..utils import (
11     ExtractorError,
12     float_or_none,
13     unified_strdate,
14 )
15
16
17 class ComedyCentralIE(MTVServicesInfoExtractor):
18     _VALID_URL = r'''(?x)https?://(?:www\.)?cc\.com/
19         (video-clips|episodes|cc-studios|video-collections|full-episodes)
20         /(?P<title>.*)'''
21     _FEED_URL = 'http://comedycentral.com/feeds/mrss/'
22
23     _TEST = {
24         'url': 'http://www.cc.com/video-clips/kllhuv/stand-up-greg-fitzsimmons--uncensored---too-good-of-a-mother',
25         'md5': 'c4f48e9eda1b16dd10add0744344b6d8',
26         'info_dict': {
27             'id': 'cef0cbb3-e776-4bc9-b62e-8016deccb354',
28             'ext': 'mp4',
29             'title': 'CC:Stand-Up|Greg Fitzsimmons: Life on Stage|Uncensored - Too Good of a Mother',
30             'description': 'After a certain point, breastfeeding becomes c**kblocking.',
31         },
32     }
33
34
35 class ComedyCentralShowsIE(MTVServicesInfoExtractor):
36     IE_DESC = 'The Daily Show / The Colbert Report'
37     # urls can be abbreviations like :thedailyshow or :colbert
38     # urls for episodes like:
39     # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day
40     #                     or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news
41     #                     or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524
42     _VALID_URL = r'''(?x)^(:(?P<shortname>tds|thedailyshow|cr|colbert|colbertnation|colbertreport)
43                       |https?://(:www\.)?
44                           (?P<showname>thedailyshow|thecolbertreport)\.(?:cc\.)?com/
45                          ((?:full-)?episodes/(?:[0-9a-z]{6}/)?(?P<episode>.*)|
46                           (?P<clip>
47                               (?:(?:guests/[^/]+|videos|video-playlists|special-editions|news-team/[^/]+)/[^/]+/(?P<videotitle>[^/?#]+))
48                               |(the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
49                               |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*))
50                           )|
51                           (?P<interview>
52                               extended-interviews/(?P<interID>[0-9a-z]+)/(?:playlist_tds_extended_)?(?P<interview_title>.*?)(/.*?)?)))
53                      (?:[?#].*|$)'''
54     _TESTS = [{
55         'url': 'http://thedailyshow.cc.com/watch/thu-december-13-2012/kristen-stewart',
56         'md5': '4e2f5cb088a83cd8cdb7756132f9739d',
57         'info_dict': {
58             'id': 'ab9ab3e7-5a98-4dbe-8b21-551dc0523d55',
59             'ext': 'mp4',
60             'upload_date': '20121213',
61             'description': 'Kristen Stewart learns to let loose in "On the Road."',
62             'uploader': 'thedailyshow',
63             'title': 'thedailyshow kristen-stewart part 1',
64         }
65     }, {
66         'url': 'http://thedailyshow.cc.com/extended-interviews/xm3fnq/andrew-napolitano-extended-interview',
67         'only_matching': True,
68     }, {
69         'url': 'http://thecolbertreport.cc.com/videos/29w6fx/-realhumanpraise-for-fox-news',
70         'only_matching': True,
71     }, {
72         'url': 'http://thecolbertreport.cc.com/videos/gh6urb/neil-degrasse-tyson-pt--1?xrs=eml_col_031114',
73         'only_matching': True,
74     }, {
75         'url': 'http://thedailyshow.cc.com/guests/michael-lewis/3efna8/exclusive---michael-lewis-extended-interview-pt--3',
76         'only_matching': True,
77     }, {
78         'url': 'http://thedailyshow.cc.com/episodes/sy7yv0/april-8--2014---denis-leary',
79         'only_matching': True,
80     }, {
81         'url': 'http://thecolbertreport.cc.com/episodes/8ase07/april-8--2014---jane-goodall',
82         'only_matching': True,
83     }, {
84         'url': 'http://thedailyshow.cc.com/video-playlists/npde3s/the-daily-show-19088-highlights',
85         'only_matching': True,
86     }, {
87         'url': 'http://thedailyshow.cc.com/special-editions/2l8fdb/special-edition---a-look-back-at-food',
88         'only_matching': True,
89     }, {
90         'url': 'http://thedailyshow.cc.com/news-team/michael-che/7wnfel/we-need-to-talk-about-israel',
91         'only_matching': True,
92     }]
93
94     _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
95
96     _video_extensions = {
97         '3500': 'mp4',
98         '2200': 'mp4',
99         '1700': 'mp4',
100         '1200': 'mp4',
101         '750': 'mp4',
102         '400': 'mp4',
103     }
104     _video_dimensions = {
105         '3500': (1280, 720),
106         '2200': (960, 540),
107         '1700': (768, 432),
108         '1200': (640, 360),
109         '750': (512, 288),
110         '400': (384, 216),
111     }
112
113     def _real_extract(self, url):
114         mobj = re.match(self._VALID_URL, url)
115
116         if mobj.group('shortname'):
117             if mobj.group('shortname') in ('tds', 'thedailyshow'):
118                 url = 'http://thedailyshow.cc.com/full-episodes/'
119             else:
120                 url = 'http://thecolbertreport.cc.com/full-episodes/'
121             mobj = re.match(self._VALID_URL, url, re.VERBOSE)
122             assert mobj is not None
123
124         if mobj.group('clip'):
125             if mobj.group('videotitle'):
126                 epTitle = mobj.group('videotitle')
127             elif mobj.group('showname') == 'thedailyshow':
128                 epTitle = mobj.group('tdstitle')
129             else:
130                 epTitle = mobj.group('cntitle')
131             dlNewest = False
132         elif mobj.group('interview'):
133             epTitle = mobj.group('interview_title')
134             dlNewest = False
135         else:
136             dlNewest = not mobj.group('episode')
137             if dlNewest:
138                 epTitle = mobj.group('showname')
139             else:
140                 epTitle = mobj.group('episode')
141         show_name = mobj.group('showname')
142
143         webpage, htmlHandle = self._download_webpage_handle(url, epTitle)
144         if dlNewest:
145             url = htmlHandle.geturl()
146             mobj = re.match(self._VALID_URL, url, re.VERBOSE)
147             if mobj is None:
148                 raise ExtractorError('Invalid redirected URL: ' + url)
149             if mobj.group('episode') == '':
150                 raise ExtractorError('Redirected URL is still not specific: ' + url)
151             epTitle = (mobj.group('episode') or mobj.group('videotitle')).rpartition('/')[-1]
152
153         mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
154         if len(mMovieParams) == 0:
155             # The Colbert Report embeds the information in a without
156             # a URL prefix; so extract the alternate reference
157             # and then add the URL prefix manually.
158
159             altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video|playlist).*?:.*?)"', webpage)
160             if len(altMovieParams) == 0:
161                 raise ExtractorError('unable to find Flash URL in webpage ' + url)
162             else:
163                 mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
164
165         uri = mMovieParams[0][1]
166         # Correct cc.com in uri
167         uri = re.sub(r'(episode:[^.]+)(\.cc)?\.com', r'\1.cc.com', uri)
168
169         index_url = 'http://%s.cc.com/feeds/mrss?%s' % (show_name, compat_urllib_parse.urlencode({'uri': uri}))
170         idoc = self._download_xml(
171             index_url, epTitle,
172             'Downloading show index', 'Unable to download episode index')
173
174         title = idoc.find('./channel/title').text
175         description = idoc.find('./channel/description').text
176
177         entries = []
178         item_els = idoc.findall('.//item')
179         for part_num, itemEl in enumerate(item_els):
180             upload_date = unified_strdate(itemEl.findall('./pubDate')[0].text)
181             thumbnail = itemEl.find('.//{http://search.yahoo.com/mrss/}thumbnail').attrib.get('url')
182
183             content = itemEl.find('.//{http://search.yahoo.com/mrss/}content')
184             duration = float_or_none(content.attrib.get('duration'))
185             mediagen_url = content.attrib['url']
186             guid = itemEl.find('./guid').text.rpartition(':')[-1]
187
188             cdoc = self._download_xml(
189                 mediagen_url, epTitle,
190                 'Downloading configuration for segment %d / %d' % (part_num + 1, len(item_els)))
191
192             turls = []
193             for rendition in cdoc.findall('.//rendition'):
194                 finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
195                 turls.append(finfo)
196
197             formats = []
198             for format, rtmp_video_url in turls:
199                 w, h = self._video_dimensions.get(format, (None, None))
200                 formats.append({
201                     'format_id': 'vhttp-%s' % format,
202                     'url': self._transform_rtmp_url(rtmp_video_url),
203                     'ext': self._video_extensions.get(format, 'mp4'),
204                     'height': h,
205                     'width': w,
206                 })
207                 formats.append({
208                     'format_id': 'rtmp-%s' % format,
209                     'url': rtmp_video_url.replace('viacomccstrm', 'viacommtvstrm'),
210                     'ext': self._video_extensions.get(format, 'mp4'),
211                     'height': h,
212                     'width': w,
213                 })
214                 self._sort_formats(formats)
215
216             virtual_id = show_name + ' ' + epTitle + ' part ' + compat_str(part_num + 1)
217             entries.append({
218                 'id': guid,
219                 'title': virtual_id,
220                 'formats': formats,
221                 'uploader': show_name,
222                 'upload_date': upload_date,
223                 'duration': duration,
224                 'thumbnail': thumbnail,
225                 'description': description,
226             })
227
228         return {
229             '_type': 'playlist',
230             'entries': entries,
231             'title': show_name + ' ' + title,
232             'description': description,
233         }