args: audio length can be human-friendly

--min-len and --max-len accept argument like 15m for 15 minutes or 2h
for two hours.

The old "number of seconds" (ie: 3600 for 1 hour) is still supported
and not deprecated.
This commit is contained in:
boyska 2018-02-18 20:28:58 +01:00
parent 324fdf9d96
commit 6d84552a2c

24
feed
View file

@ -24,11 +24,23 @@ import requests
from pytimeparse.timeparse import timeparse
def DurationType(arg):
if arg.isdecimal():
secs = int(arg)
else:
secs = timeparse(arg)
if secs is None:
raise ArgumentTypeError('%r is not a valid duration' % arg)
return secs
def TimeDeltaType(arg):
secs = timeparse(arg)
if secs is None:
raise ArgumentTypeError('%r is not a valid time range' % arg)
return datetime.timedelta(seconds=timeparse(arg))
if arg.isdecimal():
secs = int(arg)
else:
secs = timeparse(arg)
if secs is None:
raise ArgumentTypeError('%r is not a valid time range' % arg)
return datetime.timedelta(seconds=secs)
def weighted_choice(values, weights):
@ -229,14 +241,14 @@ def get_parser():
filters = p.add_argument_group('filters', 'Select only items that match '
'these conditions')
filters.add_argument('--max-len', default=0, type=int,
filters.add_argument('--max-len', default=0, type=DurationType,
help='Exclude any audio that is longer '
'than MAX_LEN seconds')
filters.add_argument('--sort-by', default='no', type=str,
choices=('random', 'date'))
filters.add_argument('--reverse', default=False,
action='store_true', help='Reverse list order')
filters.add_argument('--min-len', default=0, type=int,
filters.add_argument('--min-len', default=0, type=DurationType,
help='Exclude any audio that is shorter '
'than MIN_LEN seconds')