From: Frédéric Péters Date: Sun, 15 May 2016 13:36:36 +0000 (+0200) Subject: redo path_info parsing to allow for different time formats for start and end X-Git-Url: https://git.0d.be/?p=pige-extractor.git;a=commitdiff_plain;h=5e1ab29cc9e5215d1a6a096a73c60b96b99fea6f redo path_info parsing to allow for different time formats for start and end --- diff --git a/download.cgi b/download.cgi index e3b1a69..c12134b 100755 --- a/download.cgi +++ b/download.cgi @@ -27,27 +27,37 @@ syslog.openlog('pigebox') d = cgi.parse() if os.environ.get('PATH_INFO'): - # filename has to be of the following form: %Y%m%d-%Hh%M-%Hh%M.%ext + # filename has to be of date-start-end.extension + # - date must be %Y%m%d + # - start and end must be one of %Hh%Mm%S.%f, %Hh%Mm%S, %Hh%Mm, %Hh%M + # - extension must be one of wav, ogg, flac + # + # ex: 20160515-13h45-14h00m05.flac filename = os.path.basename(os.environ.get('PATH_INFO')) - date = filename[:8] - start = '%s:%s' % (filename[9:11], filename[12:14]) - seconds = 0 - if filename[14] == 'm': # support for seconds - seconds += 3 - start += ':' + filename[15:17] - else: - start += ':00' - end = '%s:%s' % (filename[15+seconds:17+seconds], filename[18+seconds:20+seconds]) - if seconds: - end += ':' + filename[24:26] - else: - end += ':00' - ext = filename.rsplit('.')[-1] - - start = datetime.datetime.strptime('%s %s' % (date, start), - '%Y%m%d %H:%M:%S') - end = datetime.datetime.strptime('%s %s' % (date, end), - '%Y%m%d %H:%M:%S') + basename, ext = filename.rsplit('.', 1) + date_str, start_str, end_str = basename.split('-') + + date = datetime.datetime.strptime(date_str, '%Y%m%d') + + start_time = None + end_time = None + for time_format in ('%Hh%Mm%S.%f', '%Hh%Mm%S', '%Hh%Mm', '%Hh%M'): + if start_time is None: + try: + start_time = datetime.datetime.strptime(start_str, time_format) + except ValueError: + pass + if end_time is None: + try: + end_time = datetime.datetime.strptime(end_str, time_format) + except ValueError: + pass + start = datetime.datetime(date.year, date.month, date.day, + start_time.hour, start_time.minute, start_time.second, + start_time.microsecond) + end = datetime.datetime(date.year, date.month, date.day, + end_time.hour, end_time.minute, end_time.second, + end_time.microsecond) elif d: end = '%s:%s' % (d.get('end_hour')[0], d.get('end_min')[0])