Optionally set the expected final filesize of a download as an xattribute.

Requires python-xattr and a filesystem that supports user xattributes.
This commit is contained in:
brimston3 2013-08-03 16:06:13 -04:00
parent 70c4c03cb8
commit 8a1c26021c
2 changed files with 27 additions and 2 deletions

View File

@ -11,6 +11,10 @@ if os.name == 'nt':
from .utils import *
try:
import xattr
except ImportError:
pass
class FileDownloader(object):
"""File Downloader class.
@ -39,6 +43,7 @@ class FileDownloader(object):
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
setxattrfilesize: Set ytdl.filesize user xattribute with expected size.
"""
params = None
@ -499,11 +504,17 @@ class FileDownloader(object):
try:
(stream, tmpfilename) = sanitize_open(tmpfilename, open_mode)
assert stream is not None
filename = self.undo_temp_name(tmpfilename)
self.report_destination(filename)
except (OSError, IOError) as err:
self.report_error(u'unable to open for writing: %s' % str(err))
return False
if self.params.get('setxattrfilesize', False) and data_len is not None:
try:
xattr.setxattr(tmpfilename, 'user.ytdl.filesize', str(data_len))
except (OSError, IOError) as err:
self.report_error(u'unable to set filesize xattr: %s' % str(err))
filename = self.undo_temp_name(tmpfilename)
self.report_destination(filename)
try:
stream.write(data_block)
except (IOError, OSError) as err:

View File

@ -44,6 +44,12 @@ import sys
import warnings
import platform
try:
import xattr
xattr_available = True
except ImportError:
xattr_available = False
from .utils import *
from .update import update_self
from .version import __version__
@ -306,6 +312,10 @@ def parseOpts(overrideArguments=None):
filesystem.add_option('--write-thumbnail',
action='store_true', dest='writethumbnail',
help='write thumbnail image to disk', default=False)
filesystem.add_option('-X','--xattr-filesize',
action='store_true', dest='setxattrfilesize',
help='set ytdl_filesize file xattr with expected filesize',
default=False)
postproc.add_option('-x', '--extract-audio', action='store_true', dest='extractaudio', default=False,
@ -508,6 +518,9 @@ def _real_main(argv=None):
date = DateRange.day(opts.date)
else:
date = DateRange(opts.dateafter, opts.datebefore)
if opts.setxattrfilesize:
if not xattr_available:
parser.error(u'setting filesize xattr requested but python xattr is not available')
if sys.version_info < (3,):
# In Python 2, sys.argv is a bytestring (also note http://bugs.python.org/issue2128 for Windows systems)
@ -579,6 +592,7 @@ def _real_main(argv=None):
'min_filesize': opts.min_filesize,
'max_filesize': opts.max_filesize,
'daterange': date,
'setxattrfilesize': opts.setxattrfilesize,
})
if opts.verbose: