mirror of
https://github.com/ytdl-org/youtube-dl
synced 2025-03-17 13:32:21 +09:00
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:
parent
70c4c03cb8
commit
8a1c26021c
@ -11,6 +11,10 @@ if os.name == 'nt':
|
|||||||
|
|
||||||
from .utils import *
|
from .utils import *
|
||||||
|
|
||||||
|
try:
|
||||||
|
import xattr
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
class FileDownloader(object):
|
class FileDownloader(object):
|
||||||
"""File Downloader class.
|
"""File Downloader class.
|
||||||
@ -39,6 +43,7 @@ class FileDownloader(object):
|
|||||||
test: Download only first bytes to test the downloader.
|
test: Download only first bytes to test the downloader.
|
||||||
min_filesize: Skip files smaller than this size
|
min_filesize: Skip files smaller than this size
|
||||||
max_filesize: Skip files larger than this size
|
max_filesize: Skip files larger than this size
|
||||||
|
setxattrfilesize: Set ytdl.filesize user xattribute with expected size.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
params = None
|
params = None
|
||||||
@ -499,11 +504,17 @@ class FileDownloader(object):
|
|||||||
try:
|
try:
|
||||||
(stream, tmpfilename) = sanitize_open(tmpfilename, open_mode)
|
(stream, tmpfilename) = sanitize_open(tmpfilename, open_mode)
|
||||||
assert stream is not None
|
assert stream is not None
|
||||||
filename = self.undo_temp_name(tmpfilename)
|
|
||||||
self.report_destination(filename)
|
|
||||||
except (OSError, IOError) as err:
|
except (OSError, IOError) as err:
|
||||||
self.report_error(u'unable to open for writing: %s' % str(err))
|
self.report_error(u'unable to open for writing: %s' % str(err))
|
||||||
return False
|
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:
|
try:
|
||||||
stream.write(data_block)
|
stream.write(data_block)
|
||||||
except (IOError, OSError) as err:
|
except (IOError, OSError) as err:
|
||||||
|
@ -44,6 +44,12 @@ import sys
|
|||||||
import warnings
|
import warnings
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
|
try:
|
||||||
|
import xattr
|
||||||
|
xattr_available = True
|
||||||
|
except ImportError:
|
||||||
|
xattr_available = False
|
||||||
|
|
||||||
from .utils import *
|
from .utils import *
|
||||||
from .update import update_self
|
from .update import update_self
|
||||||
from .version import __version__
|
from .version import __version__
|
||||||
@ -306,6 +312,10 @@ def parseOpts(overrideArguments=None):
|
|||||||
filesystem.add_option('--write-thumbnail',
|
filesystem.add_option('--write-thumbnail',
|
||||||
action='store_true', dest='writethumbnail',
|
action='store_true', dest='writethumbnail',
|
||||||
help='write thumbnail image to disk', default=False)
|
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,
|
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)
|
date = DateRange.day(opts.date)
|
||||||
else:
|
else:
|
||||||
date = DateRange(opts.dateafter, opts.datebefore)
|
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,):
|
if sys.version_info < (3,):
|
||||||
# In Python 2, sys.argv is a bytestring (also note http://bugs.python.org/issue2128 for Windows systems)
|
# 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,
|
'min_filesize': opts.min_filesize,
|
||||||
'max_filesize': opts.max_filesize,
|
'max_filesize': opts.max_filesize,
|
||||||
'daterange': date,
|
'daterange': date,
|
||||||
|
'setxattrfilesize': opts.setxattrfilesize,
|
||||||
})
|
})
|
||||||
|
|
||||||
if opts.verbose:
|
if opts.verbose:
|
||||||
|
Loading…
Reference in New Issue
Block a user