Handle "content too short" errors properly
[youtube-dl] / youtube-dl
index 4acdecd795477a143b1acb412a941ae2c5a93ec2..f6e472445d8e0d9512805487a04afc88bcc68662 100755 (executable)
@@ -58,6 +58,22 @@ class UnavailableFormatError(Exception):
        This exception will be thrown when a video is requested
        in a format that is not available for that video.
        """
+       pass
+
+class ContentTooShortError(Exception):
+       """Content Too Short exception.
+
+       This exception may be raised by FileDownloader objects when a file they
+       download is too small for what the server announced first, indicating
+       the connection was probably interrupted.
+       """
+       # Both in bytes
+       downloaded = None
+       expected = None
+
+       def __init__(self, downloaded, expected):
+               self.downloaded = downloaded
+               self.expected = expected
 
 class FileDownloader(object):
        """File Downloader class.
@@ -260,7 +276,9 @@ class FileDownloader(object):
                        return
 
                try:
-                       filename = self.params['outtmpl'] % info_dict
+                       template_dict = dict(info_dict)
+                       template_dict['epoch'] = unicode(long(time.time()))
+                       filename = self.params['outtmpl'] % template_dict
                        self.report_destination(filename)
                except (ValueError, KeyError), err:
                        self.trouble('ERROR: invalid output template or system charset: %s' % str(err))
@@ -284,11 +302,15 @@ class FileDownloader(object):
                        self._do_download(outstream, info_dict['url'])
                        outstream.close()
                except (OSError, IOError), err:
+                       outstream.close()
                        os.remove(filename)
                        raise UnavailableFormatError
                except (urllib2.URLError, httplib.HTTPException, socket.error), err:
                        self.trouble('ERROR: unable to download video data: %s' % str(err))
                        return
+               except (ContentTooShortError, ), err:
+                       self.trouble('ERROR: content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded))
+                       return
 
                try:
                        self.post_process(filename, info_dict)
@@ -362,7 +384,7 @@ class FileDownloader(object):
 
                self.report_finish()
                if data_len is not None and str(byte_counter) != data_len:
-                       raise ValueError('Content too short: %s/%s bytes' % (byte_counter, data_len))
+                       raise ContentTooShortError(byte_counter, long(data_len))
 
 class InfoExtractor(object):
        """Information Extractor class.
@@ -433,7 +455,7 @@ class YoutubeIE(InfoExtractor):
        _LOGIN_URL = 'http://www.youtube.com/signup?next=/&gl=US&hl=en'
        _AGE_URL = 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
        _NETRC_MACHINE = 'youtube'
-       _available_formats = ['22', '18', '17', '13'] # listed in order of priority for -b flag
+       _available_formats = ['22', '35', '18', '17', '13'] # listed in order of priority for -b flag
        _video_extensions = {
                '13': '3gp',
                '17': 'mp4',
@@ -1020,11 +1042,11 @@ if __name__ == '__main__':
                video_format.add_option('-f', '--format',
                                action='append', dest='format', metavar='FMT', help='video format code')
                video_format.add_option('-b', '--best-quality',
-                               action='append_const', dest='format', help='download the best quality video possible', const='0')
+                               action='store_const', dest='format', help='download the best quality video possible', const='0')
                video_format.add_option('-m', '--mobile-version',
-                               action='append_const', dest='format', help='alias for -f 17', const='17')
+                               action='store_const', dest='format', help='alias for -f 17', const='17')
                video_format.add_option('-d', '--high-def',
-                               action='append_const', dest='format', help='alias for -f 22', const='22')
+                               action='store_const', dest='format', help='alias for -f 22', const='22')
                parser.add_option_group(video_format)
 
                verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options')
@@ -1036,13 +1058,13 @@ if __name__ == '__main__':
                                action='store_true', dest='geturl', help='simulate, quiet but print URL', default=False)
                verbosity.add_option('-e', '--get-title',
                                action='store_true', dest='gettitle', help='simulate, quiet but print title', default=False)
-               verbosity.add_option('-t', '--title',
-                               action='store_true', dest='usetitle', help='use title in file name', default=False)
-               verbosity.add_option('-l', '--literal',
-                               action='store_true', dest='useliteral', help='use literal title in file name', default=False)
                parser.add_option_group(verbosity)
 
                filesystem = optparse.OptionGroup(parser, 'Filesystem Options')
+               filesystem.add_option('-t', '--title',
+                               action='store_true', dest='usetitle', help='use title in file name', default=False)
+               filesystem.add_option('-l', '--literal',
+                               action='store_true', dest='useliteral', help='use literal title in file name', default=False)
                filesystem.add_option('-o', '--output',
                                dest='outtmpl', metavar='TPL', help='output filename template')
                filesystem.add_option('-a', '--batch-file',