Abort download in case of error writing file data to disk
[youtube-dl] / youtube-dl
index b0c0d1939e405c52480019a6d0ef004f0e6f724d..fb231ae400d7e32cb70407395d48c940564bcf76 100755 (executable)
@@ -27,7 +27,7 @@ except ImportError:
        from cgi import parse_qs
 
 std_headers = {
-       'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.7) Gecko/20100720 Firefox/3.6.7',
+       'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.8) Gecko/20100723 Firefox/3.6.8',
        'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language': 'en-us,en;q=0.5',
@@ -353,7 +353,7 @@ class FileDownloader(object):
                                (percent_str, data_len_str, speed_str, eta_str), skip_eol=True)
 
        def report_resuming_byte(self, resume_len):
-               """Report attemtp to resume at given byte."""
+               """Report attempt to resume at given byte."""
                self.to_stdout(u'[download] Resuming download at byte %s' % resume_len)
        
        def report_retry(self, count, retries):
@@ -404,7 +404,8 @@ class FileDownloader(object):
                        template_dict['ord'] = unicode('%05d' % self._num_downloads)
                        filename = self.params['outtmpl'] % template_dict
                except (ValueError, KeyError), err:
-                       self.trouble('ERROR: invalid output template or system charset: %s' % str(err))
+                       self.trouble(u'ERROR: invalid system charset or erroneous output template')
+                       return
                if self.params.get('nooverwrites', False) and os.path.exists(filename):
                        self.to_stderr(u'WARNING: file exists: %s; skipping' % filename)
                        return
@@ -543,7 +544,7 @@ class FileDownloader(object):
                                        else:
                                                # Examine the reported length
                                                if (content_length is not None and
-                                                   resume_len - 100 < long(content_length) < resume_len + 100):
+                                                   (resume_len - 100 < long(content_length) < resume_len + 100)):
                                                        # The file had already been fully downloaded.
                                                        # Explanation to the above condition: in issue #175 it was revealed that
                                                        # YouTube sometimes adds or removes a few bytes from the end of the file,
@@ -593,7 +594,8 @@ class FileDownloader(object):
                        try:
                                stream.write(data_block)
                        except (IOError, OSError), err:
-                               self.trouble('\nERROR: unable to write data: %s' % str(err))
+                               self.trouble(u'\nERROR: unable to write data: %s' % str(err))
+                               return False
                        block_size = self.best_block_size(after - before, data_block_len)
 
                        # Progress message
@@ -1039,15 +1041,15 @@ class MetacafeIE(InfoExtractor):
                        return
                mediaURL = urllib.unquote(mobj.group(1))
 
-               #mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
-               #if mobj is None:
-               #       self._downloader.trouble(u'ERROR: unable to extract gdaKey')
-               #       return
-               #gdaKey = mobj.group(1)
-               #
-               #video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
-
-               video_url = mediaURL
+               # Extract gdaKey if available
+               mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
+               if mobj is None:
+                       video_url = mediaURL
+                       #self._downloader.trouble(u'ERROR: unable to extract gdaKey')
+                       #return
+               else:
+                       gdaKey = mobj.group(1)
+                       video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
 
                mobj = re.search(r'(?im)<title>(.*) - Video</title>', webpage)
                if mobj is None:
@@ -1941,6 +1943,11 @@ class YoutubePlaylistIE(InfoExtractor):
                                break
                        pagenum = pagenum + 1
 
+               playliststart = self._downloader.params.get('playliststart', 1)
+               playliststart -= 1 #our arrays are zero-based but the playlist is 1-based
+               if playliststart > 0:
+                       video_ids = video_ids[playliststart:]
+                       
                for id in video_ids:
                        self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % id)
                return
@@ -1996,6 +2003,11 @@ class YoutubeUserIE(InfoExtractor):
                                ids_in_page.append(mobj.group(1))
                video_ids.extend(ids_in_page)
 
+               playliststart = self._downloader.params.get('playliststart', 1)
+               playliststart = playliststart-1 #our arrays are zero-based but the playlist is 1-based
+               if playliststart > 0:
+                       video_ids = video_ids[playliststart:]   
+
                for id in video_ids:
                        self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % id)
                return
@@ -2077,7 +2089,7 @@ if __name__ == '__main__':
                # Parse command line
                parser = optparse.OptionParser(
                        usage='Usage: %prog [options] url...',
-                       version='2010.07.24',
+                       version='2010.08.04',
                        conflict_handler='resolve',
                )
 
@@ -2093,6 +2105,8 @@ if __name__ == '__main__':
                                dest='ratelimit', metavar='LIMIT', help='download rate limit (e.g. 50k or 44.6m)')
                parser.add_option('-R', '--retries',
                                dest='retries', metavar='RETRIES', help='number of retries (default is 10)', default=10)
+               parser.add_option('--playlist-start',
+                               dest='playliststart', metavar='NUMBER', help='playlist video to start at (default is 1)', default=1)
 
                authentication = optparse.OptionGroup(parser, 'Authentication Options')
                authentication.add_option('-u', '--username',
@@ -2188,6 +2202,11 @@ if __name__ == '__main__':
                                opts.retries = long(opts.retries)
                        except (TypeError, ValueError), err:
                                parser.error(u'invalid retry count specified')
+               if opts.playliststart is not None:
+                       try:
+                               opts.playliststart = long(opts.playliststart)
+                       except (TypeError, ValueError), err:
+                               parser.error(u'invalid playlist page specified')
 
                # Information extractors
                youtube_ie = YoutubeIE()
@@ -2229,6 +2248,7 @@ if __name__ == '__main__':
                        'retries': opts.retries,
                        'continuedl': opts.continue_dl,
                        'noprogress': opts.noprogress,
+                       'playliststart': opts.playliststart,
                        })
                fd.add_info_extractor(youtube_search_ie)
                fd.add_info_extractor(youtube_pl_ie)