]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/comedycentral.py
[brightcove:new] extract subtitles and strip video title
[youtube-dl] / 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_urlencode,
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|shows)
20         /(?P<title>.*)'''
21     _FEED_URL = 'http://comedycentral.com/feeds/mrss/'
22
23     _TESTS = [{
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         'url': 'http://www.cc.com/shows/the-daily-show-with-trevor-noah/interviews/6yx39d/exclusive-rand-paul-extended-interview',
34         'only_matching': True,
35     }]
36
37
38 class ComedyCentralShowsIE(MTVServicesInfoExtractor):
39     IE_DESC = 'The Daily Show / The Colbert Report'
40     # urls can be abbreviations like :thedailyshow
41     # urls for episodes like:
42     # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day
43     #                     or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news
44     #                     or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524
45     _VALID_URL = r'''(?x)^(:(?P<shortname>tds|thedailyshow)
46                       |https?://(:www\.)?
47                           (?P<showname>thedailyshow|thecolbertreport)\.(?:cc\.)?com/
48                          ((?:full-)?episodes/(?:[0-9a-z]{6}/)?(?P<episode>.*)|
49                           (?P<clip>
50                               (?:(?:guests/[^/]+|videos|video-playlists|special-editions|news-team/[^/]+)/[^/]+/(?P<videotitle>[^/?#]+))
51                               |(the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
52                               |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*))
53                           )|
54                           (?P<interview>
55                               extended-interviews/(?P<interID>[0-9a-z]+)/
56                               (?:playlist_tds_extended_)?(?P<interview_title>[^/?#]*?)
57                               (?:/[^/?#]?|[?#]|$))))
58                      '''
59     _TESTS = [{
60         'url': 'http://thedailyshow.cc.com/watch/thu-december-13-2012/kristen-stewart',
61         'md5': '4e2f5cb088a83cd8cdb7756132f9739d',
62         'info_dict': {
63             'id': 'ab9ab3e7-5a98-4dbe-8b21-551dc0523d55',
64             'ext': 'mp4',
65             'upload_date': '20121213',
66             'description': 'Kristen Stewart learns to let loose in "On the Road."',
67             'uploader': 'thedailyshow',
68             'title': 'thedailyshow kristen-stewart part 1',
69         }
70     }, {
71         'url': 'http://thedailyshow.cc.com/extended-interviews/b6364d/sarah-chayes-extended-interview',
72         'info_dict': {
73             'id': 'sarah-chayes-extended-interview',
74             'description': 'Carnegie Endowment Senior Associate Sarah Chayes discusses how corrupt institutions function throughout the world in her book "Thieves of State: Why Corruption Threatens Global Security."',
75             'title': 'thedailyshow Sarah Chayes Extended Interview',
76         },
77         'playlist': [
78             {
79                 'info_dict': {
80                     'id': '0baad492-cbec-4ec1-9e50-ad91c291127f',
81                     'ext': 'mp4',
82                     'upload_date': '20150129',
83                     'description': 'Carnegie Endowment Senior Associate Sarah Chayes discusses how corrupt institutions function throughout the world in her book "Thieves of State: Why Corruption Threatens Global Security."',
84                     'uploader': 'thedailyshow',
85                     'title': 'thedailyshow sarah-chayes-extended-interview part 1',
86                 },
87             },
88             {
89                 'info_dict': {
90                     'id': '1e4fb91b-8ce7-4277-bd7c-98c9f1bbd283',
91                     'ext': 'mp4',
92                     'upload_date': '20150129',
93                     'description': 'Carnegie Endowment Senior Associate Sarah Chayes discusses how corrupt institutions function throughout the world in her book "Thieves of State: Why Corruption Threatens Global Security."',
94                     'uploader': 'thedailyshow',
95                     'title': 'thedailyshow sarah-chayes-extended-interview part 2',
96                 },
97             },
98         ],
99         'params': {
100             'skip_download': True,
101         },
102     }, {
103         'url': 'http://thedailyshow.cc.com/extended-interviews/xm3fnq/andrew-napolitano-extended-interview',
104         'only_matching': True,
105     }, {
106         'url': 'http://thecolbertreport.cc.com/videos/29w6fx/-realhumanpraise-for-fox-news',
107         'only_matching': True,
108     }, {
109         'url': 'http://thecolbertreport.cc.com/videos/gh6urb/neil-degrasse-tyson-pt--1?xrs=eml_col_031114',
110         'only_matching': True,
111     }, {
112         'url': 'http://thedailyshow.cc.com/guests/michael-lewis/3efna8/exclusive---michael-lewis-extended-interview-pt--3',
113         'only_matching': True,
114     }, {
115         'url': 'http://thedailyshow.cc.com/episodes/sy7yv0/april-8--2014---denis-leary',
116         'only_matching': True,
117     }, {
118         'url': 'http://thecolbertreport.cc.com/episodes/8ase07/april-8--2014---jane-goodall',
119         'only_matching': True,
120     }, {
121         'url': 'http://thedailyshow.cc.com/video-playlists/npde3s/the-daily-show-19088-highlights',
122         'only_matching': True,
123     }, {
124         'url': 'http://thedailyshow.cc.com/video-playlists/t6d9sg/the-daily-show-20038-highlights/be3cwo',
125         'only_matching': True,
126     }, {
127         'url': 'http://thedailyshow.cc.com/special-editions/2l8fdb/special-edition---a-look-back-at-food',
128         'only_matching': True,
129     }, {
130         'url': 'http://thedailyshow.cc.com/news-team/michael-che/7wnfel/we-need-to-talk-about-israel',
131         'only_matching': True,
132     }]
133
134     _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
135
136     _video_extensions = {
137         '3500': 'mp4',
138         '2200': 'mp4',
139         '1700': 'mp4',
140         '1200': 'mp4',
141         '750': 'mp4',
142         '400': 'mp4',
143     }
144     _video_dimensions = {
145         '3500': (1280, 720),
146         '2200': (960, 540),
147         '1700': (768, 432),
148         '1200': (640, 360),
149         '750': (512, 288),
150         '400': (384, 216),
151     }
152
153     def _real_extract(self, url):
154         mobj = re.match(self._VALID_URL, url)
155
156         if mobj.group('shortname'):
157             return self.url_result('http://www.cc.com/shows/the-daily-show-with-trevor-noah/full-episodes')
158
159         if mobj.group('clip'):
160             if mobj.group('videotitle'):
161                 epTitle = mobj.group('videotitle')
162             elif mobj.group('showname') == 'thedailyshow':
163                 epTitle = mobj.group('tdstitle')
164             else:
165                 epTitle = mobj.group('cntitle')
166             dlNewest = False
167         elif mobj.group('interview'):
168             epTitle = mobj.group('interview_title')
169             dlNewest = False
170         else:
171             dlNewest = not mobj.group('episode')
172             if dlNewest:
173                 epTitle = mobj.group('showname')
174             else:
175                 epTitle = mobj.group('episode')
176         show_name = mobj.group('showname')
177
178         webpage, htmlHandle = self._download_webpage_handle(url, epTitle)
179         if dlNewest:
180             url = htmlHandle.geturl()
181             mobj = re.match(self._VALID_URL, url, re.VERBOSE)
182             if mobj is None:
183                 raise ExtractorError('Invalid redirected URL: ' + url)
184             if mobj.group('episode') == '':
185                 raise ExtractorError('Redirected URL is still not specific: ' + url)
186             epTitle = (mobj.group('episode') or mobj.group('videotitle')).rpartition('/')[-1]
187
188         mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
189         if len(mMovieParams) == 0:
190             # The Colbert Report embeds the information in a without
191             # a URL prefix; so extract the alternate reference
192             # and then add the URL prefix manually.
193
194             altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video|playlist).*?:.*?)"', webpage)
195             if len(altMovieParams) == 0:
196                 raise ExtractorError('unable to find Flash URL in webpage ' + url)
197             else:
198                 mMovieParams = [('http://media.mtvnservices.com/' + altMovieParams[0], altMovieParams[0])]
199
200         uri = mMovieParams[0][1]
201         # Correct cc.com in uri
202         uri = re.sub(r'(episode:[^.]+)(\.cc)?\.com', r'\1.com', uri)
203
204         index_url = 'http://%s.cc.com/feeds/mrss?%s' % (show_name, compat_urllib_parse_urlencode({'uri': uri}))
205         idoc = self._download_xml(
206             index_url, epTitle,
207             'Downloading show index', 'Unable to download episode index')
208
209         title = idoc.find('./channel/title').text
210         description = idoc.find('./channel/description').text
211
212         entries = []
213         item_els = idoc.findall('.//item')
214         for part_num, itemEl in enumerate(item_els):
215             upload_date = unified_strdate(itemEl.findall('./pubDate')[0].text)
216             thumbnail = itemEl.find('.//{http://search.yahoo.com/mrss/}thumbnail').attrib.get('url')
217
218             content = itemEl.find('.//{http://search.yahoo.com/mrss/}content')
219             duration = float_or_none(content.attrib.get('duration'))
220             mediagen_url = content.attrib['url']
221             guid = itemEl.find('./guid').text.rpartition(':')[-1]
222
223             cdoc = self._download_xml(
224                 mediagen_url, epTitle,
225                 'Downloading configuration for segment %d / %d' % (part_num + 1, len(item_els)))
226
227             turls = []
228             for rendition in cdoc.findall('.//rendition'):
229                 finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
230                 turls.append(finfo)
231
232             formats = []
233             for format, rtmp_video_url in turls:
234                 w, h = self._video_dimensions.get(format, (None, None))
235                 formats.append({
236                     'format_id': 'vhttp-%s' % format,
237                     'url': self._transform_rtmp_url(rtmp_video_url),
238                     'ext': self._video_extensions.get(format, 'mp4'),
239                     'height': h,
240                     'width': w,
241                 })
242                 formats.append({
243                     'format_id': 'rtmp-%s' % format,
244                     'url': rtmp_video_url.replace('viacomccstrm', 'viacommtvstrm'),
245                     'ext': self._video_extensions.get(format, 'mp4'),
246                     'height': h,
247                     'width': w,
248                 })
249                 self._sort_formats(formats)
250
251             subtitles = self._extract_subtitles(cdoc, guid)
252
253             virtual_id = show_name + ' ' + epTitle + ' part ' + compat_str(part_num + 1)
254             entries.append({
255                 'id': guid,
256                 'title': virtual_id,
257                 'formats': formats,
258                 'uploader': show_name,
259                 'upload_date': upload_date,
260                 'duration': duration,
261                 'thumbnail': thumbnail,
262                 'description': description,
263                 'subtitles': subtitles,
264             })
265
266         return {
267             '_type': 'playlist',
268             'id': epTitle,
269             'entries': entries,
270             'title': show_name + ' ' + title,
271             'description': description,
272         }