X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=youtube-dl;h=8143de35e90fa2f92b4e163fec4523ccbcec0a65;hb=2740c509b3c19fe8589b6aa6f746005aa12a7f82;hp=034da35f54e885ed6307c8b51cdeaba3fdf83fae;hpb=147753eb3380137155039cccc0c5c4f0d4b7136d;p=youtube-dl.git diff --git a/youtube-dl b/youtube-dl index 034da35f5..8143de35e 100755 --- a/youtube-dl +++ b/youtube-dl @@ -52,6 +52,13 @@ class PostProcessingError(Exception): """ pass +class UnavailableFormatError(Exception): + """Unavailable Format exception. + + This exception will be thrown when a video is requested + in a format that is not available for that video. + """ + class FileDownloader(object): """File Downloader class. @@ -65,9 +72,10 @@ class FileDownloader(object): For this, file downloader objects have a method that allows InfoExtractors to be registered in a given order. When it is passed a URL, the file downloader handles it to the first InfoExtractor it - finds that reports being able to handle it. The InfoExtractor returns - all the information to the FileDownloader and the latter downloads the - file or does whatever it's instructed to do. + finds that reports being able to handle it. The InfoExtractor extracts + all the information about the video or videos the URL refers to, and + asks the FileDownloader to process the video information, possibly + downloading the video. File downloaders accept a lot of parameters. In order not to saturate the object constructor with arguments, it receives a dictionary of @@ -189,7 +197,7 @@ class FileDownloader(object): def to_stdout(self, message, skip_eol=False): """Print message to stdout if not in quiet mode.""" if not self.params.get('quiet', False): - print u'%s%s' % (message, [u'\n', u''][skip_eol]), + print (u'%s%s' % (message, [u'\n', u''][skip_eol])).encode(locale.getpreferredencoding()), sys.stdout.flush() def to_stderr(self, message): @@ -243,9 +251,9 @@ class FileDownloader(object): """Process a single dictionary returned by an InfoExtractor.""" # Forced printings if self.params.get('forcetitle', False): - print info_dict['title'] + print info_dict['title'].encode(locale.getpreferredencoding()) if self.params.get('forceurl', False): - print info_dict['url'] + print info_dict['url'].encode(locale.getpreferredencoding()) # Do nothing else if in simulate mode if self.params.get('simulate', False): @@ -259,33 +267,38 @@ class FileDownloader(object): if self.params['nooverwrites'] and os.path.exists(filename): self.to_stderr('WARNING: file exists: %s; skipping' % filename) return + try: self.pmkdir(filename) except (OSError, IOError), err: self.trouble('ERROR: unable to create directories: %s' % str(err)) return + try: outstream = open(filename, 'wb') except (OSError, IOError), err: self.trouble('ERROR: unable to open for writing: %s' % str(err)) return + try: self._do_download(outstream, info_dict['url']) outstream.close() except (OSError, IOError), err: - self.trouble('ERROR: unable to write video data: %s' % str(err)) - return + if info_dict['best_quality']: + raise UnavailableFormatError + else: + self.trouble('ERROR: unable to write video data: %s' % str(err)) + return except (urllib2.URLError, httplib.HTTPException, socket.error), err: self.trouble('ERROR: unable to download video data: %s' % str(err)) return + try: self.post_process(filename, info_dict) except (PostProcessingError), err: self.trouble('ERROR: postprocessing: %s' % str(err)) return - return - def download(self, url_list): """Download a given list of URLs.""" if len(url_list) > 1 and self.fixed_template(): @@ -301,21 +314,8 @@ class FileDownloader(object): # Suitable InfoExtractor found suitable_found = True - # Extract information from URL - all_results = ie.extract(url) - results = [x for x in all_results if x is not None] - - # See if there were problems extracting any information - if len(results) != len(all_results): - self.trouble() - - # Two results could go to the same file - if len(results) > 1 and self.fixed_template(): - raise SameFileError(self.params['outtmpl']) - - # Process each result - for result in results: - self.process_info(result) + # Extract information from URL and process it + ie.extract(url) # Suitable InfoExtractor had been found; go to next URL break @@ -373,9 +373,10 @@ class InfoExtractor(object): Information extractors are the classes that, given a URL, extract information from the video (or videos) the URL refers to. This information includes the real video URL, the video title and simplified - title, author and others. It is returned in a list of dictionaries when - calling its extract() method. It is a list because a URL can refer to - more than one video (think of playlists). The dictionaries must include + title, author and others. The information is stored in a dictionary + which is then passed to the FileDownloader. The FileDownloader + processes this information possibly downloading the video to the file + system, among other possible outcomes. The dictionaries must include the following fields: id: Video identifier. @@ -435,6 +436,13 @@ 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 + _video_extensions = { + '13': '3gp', + '17': 'mp4', + '18': 'mp4', + '22': 'mp4', + } @staticmethod def suitable(url): @@ -487,6 +495,10 @@ class YoutubeIE(InfoExtractor): """Report extracted video URL.""" self._downloader.to_stdout(u'[youtube] %s: URL: %s' % (video_id, video_real_url)) + def report_unavailable_format(self, video_id, format): + """Report extracted video URL.""" + self._downloader.to_stdout(u'[youtube] %s: Format %s not available' % (video_id, format)) + def _real_initialize(self): if self._downloader is None: return @@ -508,7 +520,7 @@ class YoutubeIE(InfoExtractor): else: raise netrc.NetrcParseError('No authenticators for %s' % self._NETRC_MACHINE) except (IOError, netrc.NetrcParseError), err: - self._downloader.trouble(u'WARNING: parsing .netrc: %s' % str(err)) + self._downloader.to_stderr(u'WARNING: parsing .netrc: %s' % str(err)) return # Set language @@ -517,7 +529,7 @@ class YoutubeIE(InfoExtractor): self.report_lang() urllib2.urlopen(request).read() except (urllib2.URLError, httplib.HTTPException, socket.error), err: - self._downloader.trouble(u'WARNING: unable to set language: %s' % str(err)) + self._downloader.to_stderr(u'WARNING: unable to set language: %s' % str(err)) return # No authentication to be performed @@ -537,10 +549,10 @@ class YoutubeIE(InfoExtractor): self.report_login() login_results = urllib2.urlopen(request).read() if re.search(r'(?i)