From 6d84552a2c6bc6e2b16567eb0a86c64da50f7eea Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 18 Feb 2018 20:28:58 +0100 Subject: [PATCH] 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. --- feed | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/feed b/feed index 0253dac..635dcca 100755 --- a/feed +++ b/feed @@ -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')