From 881e6a1f5c47a65348879f817ad833081e8c5ada Mon Sep 17 00:00:00 2001 From: Philipp Hagemeister Date: Sun, 25 Jan 2015 04:49:44 +0100 Subject: [PATCH] Add --xattr-set-filesize option (Fixes #1348) --- youtube_dl/YoutubeDL.py | 3 ++- youtube_dl/__init__.py | 6 ++++++ youtube_dl/downloader/common.py | 32 +++++++++++++++++--------------- youtube_dl/downloader/http.py | 8 ++++++++ youtube_dl/options.py | 4 ++++ 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index b7e93b8dda..0241f7e3c8 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -231,7 +231,8 @@ class YoutubeDL(object): The following parameters are not used by YoutubeDL itself, they are used by the FileDownloader: nopart, updatetime, buffersize, ratelimit, min_filesize, max_filesize, test, - noresizebuffer, retries, continuedl, noprogress, consoletitle + noresizebuffer, retries, continuedl, noprogress, consoletitle, + xattr_set_filesize. The following options are used by the post processors: prefer_ffmpeg: If True, use ffmpeg instead of avconv if both are available, diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index 09da8802d6..112a8ba60e 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -241,6 +241,11 @@ def _real_main(argv=None): 'verboseOutput': opts.verbose, 'exec_cmd': opts.exec_cmd, }) + if opts.xattr_set_filesize: + try: + import xattr + except ImportError: + parser.error('setting filesize xattr requested but python-xattr is not available') ydl_opts = { 'usenetrc': opts.usenetrc, @@ -337,6 +342,7 @@ def _real_main(argv=None): 'external_downloader': opts.external_downloader, 'list_thumbnails': opts.list_thumbnails, 'playlist_items': opts.playlist_items, + 'xattr_set_filesize': opts.xattr_set_filesize, } with YoutubeDL(ydl_opts) as ydl: diff --git a/youtube_dl/downloader/common.py b/youtube_dl/downloader/common.py index c35c42c1dc..7bb3a948d2 100644 --- a/youtube_dl/downloader/common.py +++ b/youtube_dl/downloader/common.py @@ -25,21 +25,23 @@ class FileDownloader(object): Available options: - verbose: Print additional info to stdout. - quiet: Do not print messages to stdout. - ratelimit: Download speed limit, in bytes/sec. - retries: Number of times to retry for HTTP error 5xx - buffersize: Size of download buffer in bytes. - noresizebuffer: Do not automatically resize the download buffer. - continuedl: Try to continue downloads if possible. - noprogress: Do not print the progress bar. - logtostderr: Log messages to stderr instead of stdout. - consoletitle: Display progress in console window's titlebar. - nopart: Do not use temporary .part files. - updatetime: Use the Last-modified header to set output file timestamps. - test: Download only first bytes to test the downloader. - min_filesize: Skip files smaller than this size - max_filesize: Skip files larger than this size + verbose: Print additional info to stdout. + quiet: Do not print messages to stdout. + ratelimit: Download speed limit, in bytes/sec. + retries: Number of times to retry for HTTP error 5xx + buffersize: Size of download buffer in bytes. + noresizebuffer: Do not automatically resize the download buffer. + continuedl: Try to continue downloads if possible. + noprogress: Do not print the progress bar. + logtostderr: Log messages to stderr instead of stdout. + consoletitle: Display progress in console window's titlebar. + nopart: Do not use temporary .part files. + updatetime: Use the Last-modified header to set output file timestamps. + test: Download only first bytes to test the downloader. + min_filesize: Skip files smaller than this size + max_filesize: Skip files larger than this size + xattr_set_filesize: Set ytdl.filesize user xattribute with expected size. + (experimenatal) Subclasses of this one must re-define the real_download method. """ diff --git a/youtube_dl/downloader/http.py b/youtube_dl/downloader/http.py index 4db50ee90e..8a1d578d54 100644 --- a/youtube_dl/downloader/http.py +++ b/youtube_dl/downloader/http.py @@ -157,6 +157,14 @@ class HttpFD(FileDownloader): except (OSError, IOError) as err: self.report_error('unable to open for writing: %s' % str(err)) return False + + if self.params.get('xattr_set_filesize', False) and data_len is not None: + try: + import xattr + xattr.setxattr(tmpfilename, 'user.ytdl.filesize', str(data_len)) + except(OSError, IOError, ImportError) as err: + self.report_error('unable to set filesize xattr: %s' % str(err)) + try: stream.write(data_block) except (IOError, OSError) as err: diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 1ddbdbc787..dbc6f5528f 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -394,6 +394,10 @@ def parseOpts(overrideArguments=None): '--playlist-reverse', action='store_true', help='Download playlist videos in reverse order') + downloader.add_option( + '--xattr-set-filesize', + dest='xattr_set_filesize', action='store_true', + help='(experimental) set file xattribute ytdl.filesize with expected filesize') downloader.add_option( '--external-downloader', dest='external_downloader', metavar='COMMAND',