Introduced Trouble(Exception) for more elegant non-fatal errors handling
authorFilippo Valsorda <filippo.valsorda@gmail.com>
Wed, 9 May 2012 09:41:34 +0000 (09:41 +0000)
committerFilippo Valsorda <filosottile@ip-10-58-141-254.eu-west-1.compute.internal>
Wed, 9 May 2012 09:43:11 +0000 (09:43 +0000)
youtube_dl/InfoExtractors.py
youtube_dl/utils.py

index 01201a145404deadfb74066d47a7296bf3ad2b5b..be6b0cb085e434985a1f7e65a017f62d24e5b7ff 100644 (file)
@@ -359,33 +359,32 @@ class YoutubeIE(InfoExtractor):
                # closed captions
                video_subtitles = None
                if self._downloader.params.get('writesubtitles', False):
-                       self.report_video_subtitles_download(video_id)
-                       request = urllib2.Request('http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id)
                        try:
-                               srt_list = urllib2.urlopen(request).read()
-                       except (urllib2.URLError, httplib.HTTPException, socket.error), err:
-                               self._downloader.trouble(u'WARNING: unable to download video subtitles: %s' % str(err))
-                       else:
+                               self.report_video_subtitles_download(video_id)
+                               request = urllib2.Request('http://video.google.com/timedtext?hl=en&type=list&v=%s' % video_id)
+                               try:
+                                       srt_list = urllib2.urlopen(request).read()
+                               except (urllib2.URLError, httplib.HTTPException, socket.error), err:
+                                       raise Trouble(u'WARNING: unable to download video subtitles: %s' % str(err))
                                srt_lang_list = re.findall(r'lang_code="([\w\-]+)"', srt_list)
-                               if srt_lang_list:
-                                       if self._downloader.params.get('subtitleslang', False):
-                                               srt_lang = self._downloader.params.get('subtitleslang')
-                                       elif 'en' in srt_lang_list:
-                                               srt_lang = 'en'
-                                       else:
-                                               srt_lang = srt_lang_list[0]
-                                       if not srt_lang in srt_lang_list:
-                                               self._downloader.trouble(u'WARNING: no closed captions found in the specified language')
-                                       else:
-                                               request = urllib2.Request('http://video.google.com/timedtext?hl=en&lang=%s&v=%s' % (srt_lang, video_id))
-                                               try:
-                                                       srt_xml = urllib2.urlopen(request).read()
-                                               except (urllib2.URLError, httplib.HTTPException, socket.error), err:
-                                                       self._downloader.trouble(u'WARNING: unable to download video subtitles: %s' % str(err))
-                                               else:
-                                                       video_subtitles = self._closed_captions_xml_to_srt(srt_xml.decode('utf-8'))
+                               if not srt_lang_list:
+                                       raise Trouble(u'WARNING: video has no closed captions')
+                               if self._downloader.params.get('subtitleslang', False):
+                                       srt_lang = self._downloader.params.get('subtitleslang')
+                               elif 'en' in srt_lang_list:
+                                       srt_lang = 'en'
                                else:
-                                       self._downloader.trouble(u'WARNING: video has no closed captions')
+                                       srt_lang = srt_lang_list[0]
+                               if not srt_lang in srt_lang_list:
+                                       raise Trouble(u'WARNING: no closed captions found in the specified language')
+                               request = urllib2.Request('http://video.google.com/timedtext?hl=en&lang=%s&v=%s' % (srt_lang, video_id))
+                               try:
+                                       srt_xml = urllib2.urlopen(request).read()
+                               except (urllib2.URLError, httplib.HTTPException, socket.error), err:
+                                       raise Trouble(u'WARNING: unable to download video subtitles: %s' % str(err))
+                               video_subtitles = self._closed_captions_xml_to_srt(srt_xml.decode('utf-8'))
+                       except Trouble as trouble:
+                               self._downloader.trouble(trouble[0])
 
                # token
                video_token = urllib.unquote_plus(video_info['token'][0])
index 6e982157c4811b9db43642af3b9ee90d8ab5e376..d18073d72894c905b92233d0493cf83d525a6607 100644 (file)
@@ -290,6 +290,13 @@ class ContentTooShortError(Exception):
                self.expected = expected
 
 
+class Trouble(Exception):
+       """Trouble helper exception
+       
+       This is an exception to be handled with
+       FileDownloader.trouble
+       """
+
 class YoutubeDLHandler(urllib2.HTTPHandler):
        """Handler for HTTP requests and responses.