Format specs for {autonumber} and {upload_date}

{autonumber} and {playlist_index} are now integers rather than strings, so {autonumber:02} works and you don't need --autonumber-size.
(Actually, they're "special integers" that get printed to a predefined fixed width if you leave the :02 out; see the _Int_digits class.)
{upload_date} is now a datetime.date, so {upload_date:%b %Y} will format as "Oct 2015" (default is "2015-10-19").
sanitize() gets applied only to string values, not to numeric ones (or dates or lists or...)
This commit is contained in:
cousteau 2015-10-19 01:50:25 +02:00
parent 736c60eaa5
commit f41604b253

View File

@ -29,6 +29,7 @@ if os.name == 'nt':
import ctypes
from .compat import (
compat_basestring,
compat_cookiejar,
compat_expanduser,
compat_get_terminal_size,
@ -90,6 +91,19 @@ from .postprocessor import (
from .version import __version__
class _Int_digits(int):
"""int-like type that gets formatted with a default fixed width
(to be used within YoutubeDL.prepare_filename() so that {autonumber:03}
prints 001 but {autonumber} defaults to 00001 rather than 1)
"""
def __new__(cls, value, digits=0):
obj = int.__new__(cls, value)
obj.digits = digits
return obj
def __str__(self):
return "{0:0{1}}".format(self, self.digits)
class YoutubeDL(object):
"""YoutubeDL class.
@ -546,9 +560,9 @@ class YoutubeDL(object):
autonumber_size = self.params.get('autonumber_size')
if autonumber_size is None:
autonumber_size = 5
template_dict['autonumber'] = '%0*d' % (autonumber_size, self._num_downloads)
template_dict['autonumber'] = _Int_digits(self._num_downloads, autonumber_size)
if template_dict.get('playlist_index') is not None:
template_dict['playlist_index'] = '%0*d' % (len(str(template_dict['n_entries'])), template_dict['playlist_index'])
template_dict['playlist_index'] = _Int_digits(template_dict['playlist_index'], len(str(template_dict['n_entries'])))
if template_dict.get('resolution') is None:
if template_dict.get('width') and template_dict.get('height'):
template_dict['resolution'] = '%dx%d' % (template_dict['width'], template_dict['height'])
@ -556,11 +570,16 @@ class YoutubeDL(object):
template_dict['resolution'] = '%sp' % template_dict['height']
elif template_dict.get('width'):
template_dict['resolution'] = '?x%d' % template_dict['width']
if template_dict.get('upload_date') is not None:
template_dict['upload_date'] = datetime.datetime.strptime(template_dict['upload_date'],"%Y%m%d").date()
sanitize = lambda k, v: sanitize_filename(
compat_str(v),
restricted=self.params.get('restrictfilenames'),
is_id=(k == 'id'))
def sanitize(k, v):
if isinstance(v, compat_basestring):
return sanitize_filename(compat_str(v),
restricted = self.params.get('restrictfilenames'),
is_id = (k == 'id') )
else:
return v # leave non-strings untouched
template_dict = dict((k, sanitize(k, v))
for k, v in template_dict.items()
if v is not None)