Allow downloading current thedailyshow episode with youtube-dl :tds
[youtube-dl.git] / youtube-dl
index 651e9d3..9d379dc 100755 (executable)
@@ -15,7 +15,7 @@ __author__  = (
        )
 
 __license__ = 'Public Domain'
-__version__ = '2011.09.06-phihag'
+__version__ = '2011.09.09-phihag'
 
 UPDATE_URL = 'https://raw.github.com/phihag/youtube-dl/master/youtube-dl'
 
@@ -3037,9 +3037,9 @@ class MyVideoIE(InfoExtractor):
                        self._downloader.trouble(u'\nERROR: Unable to download video')
 
 class ComedyCentralIE(InfoExtractor):
-       """Information extractor for blip.tv"""
+       """Information extractor for The Daily Show and Colbert Report """
 
-       _VALID_URL = r'^(?:https?://)?(www\.)?(thedailyshow|colbertnation)\.com/full-episodes/(.*)$'
+       _VALID_URL = r'^(:(?P<shortname>tds|thedailyshow|cr|colbert|colbertnation|colbertreport))|(https?://)?(www\.)(?P<showname>thedailyshow|colbertnation)\.com/full-episodes/(?P<episode>.*)$'
 
        @staticmethod
        def suitable(url):
@@ -3064,15 +3064,39 @@ class ComedyCentralIE(InfoExtractor):
                if mobj is None:
                        self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
                        return
-               epTitle = mobj.group(3)
+
+               if mobj.group('shortname'):
+                       if mobj.group('shortname') in ('tds', 'thedailyshow'):
+                               url = 'http://www.thedailyshow.com/full-episodes/'
+                       else:
+                               url = 'http://www.colbertnation.com/full-episodes/'
+                       mobj = re.match(self._VALID_URL, url)
+                       assert mobj is not None
+
+               dlNewest = not mobj.group('episode')
+               if dlNewest:
+                       epTitle = mobj.group('showname')
+               else:
+                       epTitle = mobj.group('episode')
 
                req = urllib2.Request(url)
                self.report_extraction(epTitle)
                try:
-                       html = urllib2.urlopen(req).read()
+                       htmlHandle = urllib2.urlopen(req)
+                       html = htmlHandle.read()
                except (urllib2.URLError, httplib.HTTPException, socket.error), err:
                        self._downloader.trouble(u'ERROR: unable to download webpage: %s' % unicode(err))
                        return
+               if dlNewest:
+                       url = htmlHandle.geturl()
+                       mobj = re.match(self._VALID_URL, url)
+                       if mobj is None:
+                               self._downloader.trouble(u'ERROR: Invalid redirected URL: ' + url)
+                               return
+                       if mobj.group('episode') == '':
+                               self._downloader.trouble(u'ERROR: Redirected URL is still not specific: ' + url)
+                               return
+                       epTitle = mobj.group('episode')
 
                mMovieParams = re.findall('<param name="movie" value="(http://media.mtvnservices.com/(.*?:episode:([^:]*):)(.*?))"/>', html)
                if len(mMovieParams) == 0:
@@ -3084,12 +3108,12 @@ class ComedyCentralIE(InfoExtractor):
                        'colbertnation.com': 3,
                }.get(show_id, 4)
                OFFSET = {
-                       'thedailyshow.com': -ACT_COUNT,
+                       'thedailyshow.com': 1,
                        'colbertnation.com': 1,
-               }.get(show_id, -ACT_COUNT)
+               }.get(show_id, 1)
 
                first_player_url = mMovieParams[0][0]
-               mediaNum = int(mMovieParams[0][3]) + OFFSET
+               startMediaNum = int(mMovieParams[0][3]) + OFFSET
                movieId = mMovieParams[0][1]
 
                playerReq = urllib2.Request(first_player_url)
@@ -3102,8 +3126,8 @@ class ComedyCentralIE(InfoExtractor):
                player_url = playerResponse.geturl()
 
                for actNum in range(ACT_COUNT):
-                       actTitle = 'act' + str(actNum+1)
-                       mediaId = movieId + str(mediaNum + actNum)
+                       mediaNum = startMediaNum + actNum
+                       mediaId = movieId + str(mediaNum)
                        configUrl = ('http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?' +
                                                urllib.urlencode({'uri': mediaId}))
                        configReq = urllib2.Request(configUrl)
@@ -3121,7 +3145,7 @@ class ComedyCentralIE(InfoExtractor):
                                turls.append(finfo)
 
                        if len(turls) == 0:
-                               self._downloader.trouble(u'\nERROR: unable to download ' + actTitle + ': No videos found')
+                               self._downloader.trouble(u'\nERROR: unable to download ' + str(mediaNum) + ': No videos found')
                                continue
 
                        # For now, just pick the highest bitrate
@@ -3131,7 +3155,7 @@ class ComedyCentralIE(InfoExtractor):
 
                        effTitle = show_id.replace('.com', '') + '-' + epTitle
                        info = {
-                               'id': actTitle,
+                               'id': str(mediaNum),
                                'url': video_url,
                                'uploader': show_id,
                                'upload_date': 'NA',
@@ -3147,7 +3171,7 @@ class ComedyCentralIE(InfoExtractor):
                        try:
                                self._downloader.process_info(info)
                        except UnavailableVideoError, err:
-                               self._downloader.trouble(u'\nERROR: unable to download ' + actTitle)
+                               self._downloader.trouble(u'\nERROR: unable to download ' + str(mediaNum))
                                continue