2 import xml.etree.ElementTree
4 from .common import InfoExtractor
14 class ComedyCentralIE(InfoExtractor):
15 """Information extractor for The Daily Show and Colbert Report """
17 # urls can be abbreviations like :thedailyshow or :colbert
18 # urls for episodes like:
19 # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day
20 # or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news
21 # or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524
22 _VALID_URL = r"""^(:(?P<shortname>tds|thedailyshow|cr|colbert|colbertnation|colbertreport)
24 (?P<showname>thedailyshow|colbertnation)\.com/
25 (full-episodes/(?P<episode>.*)|
27 (the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
28 |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*)))))
31 _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
51 def suitable(cls, url):
52 """Receives a URL and returns True if suitable for this IE."""
53 return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
55 def _print_formats(self, formats):
56 print('Available formats:')
58 print('%s\t:\t%s\t[%s]' %(x, self._video_extensions.get(x, 'mp4'), self._video_dimensions.get(x, '???')))
61 def _real_extract(self, url):
62 mobj = re.match(self._VALID_URL, url, re.VERBOSE)
64 raise ExtractorError(u'Invalid URL: %s' % url)
66 if mobj.group('shortname'):
67 if mobj.group('shortname') in ('tds', 'thedailyshow'):
68 url = u'http://www.thedailyshow.com/full-episodes/'
70 url = u'http://www.colbertnation.com/full-episodes/'
71 mobj = re.match(self._VALID_URL, url, re.VERBOSE)
72 assert mobj is not None
74 if mobj.group('clip'):
75 if mobj.group('showname') == 'thedailyshow':
76 epTitle = mobj.group('tdstitle')
78 epTitle = mobj.group('cntitle')
81 dlNewest = not mobj.group('episode')
83 epTitle = mobj.group('showname')
85 epTitle = mobj.group('episode')
87 self.report_extraction(epTitle)
88 webpage,htmlHandle = self._download_webpage_handle(url, epTitle)
90 url = htmlHandle.geturl()
91 mobj = re.match(self._VALID_URL, url, re.VERBOSE)
93 raise ExtractorError(u'Invalid redirected URL: ' + url)
94 if mobj.group('episode') == '':
95 raise ExtractorError(u'Redirected URL is still not specific: ' + url)
96 epTitle = mobj.group('episode')
98 mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
100 if len(mMovieParams) == 0:
101 # The Colbert Report embeds the information in a without
102 # a URL prefix; so extract the alternate reference
103 # and then add the URL prefix manually.
105 altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video).*?:.*?)"', webpage)
106 if len(altMovieParams) == 0:
107 raise ExtractorError(u'unable to find Flash URL in webpage ' + url)
109 mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
111 uri = mMovieParams[0][1]
112 indexUrl = 'http://shadow.comedycentral.com/feeds/video_player/mrss/?' + compat_urllib_parse.urlencode({'uri': uri})
113 indexXml = self._download_webpage(indexUrl, epTitle,
114 u'Downloading show index',
115 u'unable to download episode index')
119 idoc = xml.etree.ElementTree.fromstring(indexXml)
120 itemEls = idoc.findall('.//item')
121 for partNum,itemEl in enumerate(itemEls):
122 mediaId = itemEl.findall('./guid')[0].text
123 shortMediaId = mediaId.split(':')[-1]
124 showId = mediaId.split(':')[-2].replace('.com', '')
125 officialTitle = itemEl.findall('./title')[0].text
126 officialDate = unified_strdate(itemEl.findall('./pubDate')[0].text)
128 configUrl = ('http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?' +
129 compat_urllib_parse.urlencode({'uri': mediaId}))
130 configXml = self._download_webpage(configUrl, epTitle,
131 u'Downloading configuration for %s' % shortMediaId)
133 cdoc = xml.etree.ElementTree.fromstring(configXml)
135 for rendition in cdoc.findall('.//rendition'):
136 finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
140 self._downloader.report_error(u'unable to download ' + mediaId + ': No videos found')
143 if self._downloader.params.get('listformats', None):
144 self._print_formats([i[0] for i in turls])
147 # For now, just pick the highest bitrate
148 format,rtmp_video_url = turls[-1]
150 # Get the format arg from the arg stream
151 req_format = self._downloader.params.get('format', None)
153 # Select format if we can find one
156 format, rtmp_video_url = f, v
159 m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp.comedystor/.*)$', rtmp_video_url)
161 raise ExtractorError(u'Cannot transform RTMP url')
162 base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
163 video_url = base + m.group('finalid')
165 effTitle = showId + u'-' + epTitle + u' part ' + compat_str(partNum+1)
170 'upload_date': officialDate,
175 'description': officialTitle,