+class ComedyCentralIE(InfoExtractor):
+ """Information extractor for blip.tv"""
+
+ _VALID_URL = r'^(?:https?://)?(www\.)?(thedailyshow|colbertnation)\.com/full-episodes/(.*)$'
+
+ @staticmethod
+ def suitable(url):
+ return (re.match(ComedyCentralIE._VALID_URL, url) is not None)
+
+ def report_extraction(self, episode_id):
+ self._downloader.to_screen(u'[comedycentral] %s: Extracting information' % episode_id)
+
+ def report_config_download(self, episode_id):
+ self._downloader.to_screen(u'[comedycentral] %s: Downloading configuration' % episode_id)
+
+ def _simplify_title(self, title):
+ res = re.sub(ur'(?u)([^%s]+)' % simple_title_chars, ur'_', title)
+ res = res.strip(ur'_')
+ return res
+
+ def _real_extract(self, url):
+ mobj = re.match(self._VALID_URL, url)
+ if mobj is None:
+ self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
+ return
+ epTitle = mobj.group(3)
+
+ req = urllib2.Request(url)
+ self.report_extraction(epTitle)
+ try:
+ html = urllib2.urlopen(req).read()
+ except (urllib2.URLError, httplib.HTTPException, socket.error), err:
+ self._downloader.trouble(u'ERROR: unable to download webpage: %s' % unicode(err))
+ return
+
+ mMovieParams = re.findall('<param name="movie" value="http://media.mtvnservices.com/(.*?:episode:.*?:)(.*?)"/>', html)
+ if len(mMovieParams) == 0:
+ self._downloader.trouble(u'ERROR: unable to find Flash URL in webpage ' + url)
+ return
+ ACT_COUNT = 4
+ mediaNum = int(mMovieParams[0][1]) - ACT_COUNT
+
+ for actNum in range(ACT_COUNT):
+ mediaId = mMovieParams[0][0] + str(mediaNum + actNum)
+ configUrl = ('http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?' +
+ urllib.urlencode({'uri': mediaId}))
+ configReq = urllib2.Request(configUrl)
+ self.report_config_download(epTitle)
+ try:
+ configXml = urllib2.urlopen(configReq).read()
+ except (urllib2.URLError, httplib.HTTPException, socket.error), err:
+ self._downloader.trouble(u'ERROR: unable to download webpage: %s' % unicode(err))
+ return
+
+ cdoc = xml.etree.ElementTree.fromstring(configXml)
+ turls = []
+ for rendition in cdoc.findall('.//rendition'):
+ finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
+ turls.append(finfo)
+
+ # For now, just pick the highest bitrate
+ format,video_url = turls[-1]
+
+ self._downloader.increment_downloads()
+ actTitle = epTitle + '-act' + str(actNum+1)
+ info = {
+ 'id': epTitle,
+ 'url': video_url,
+ 'uploader': 'NA',
+ 'upload_date': 'NA',
+ 'title': actTitle,
+ 'stitle': self._simplify_title(actTitle),
+ 'ext': 'mp4',
+ 'format': format,
+ 'thumbnail': None,
+ 'description': 'TODO: Not yet supported',
+ 'player_url': None
+ }
+
+ try:
+ self._downloader.process_info(info)
+ except UnavailableVideoError, err:
+ self._downloader.trouble(u'\nERROR: unable to download video')
+
+