[utils] Improve parse_duration
authorSergey M․ <dstftw@gmail.com>
Sat, 30 Aug 2014 23:41:30 +0000 (06:41 +0700)
committerSergey M․ <dstftw@gmail.com>
Sat, 30 Aug 2014 23:41:30 +0000 (06:41 +0700)
test/test_utils.py
youtube_dl/utils.py

index 3d14f61fbf87ec61b9d7d14dd917c020232453aa..8d89979775c75db9f54c041716f5bfc92a77150a 100644 (file)
@@ -211,6 +211,9 @@ class TestUtil(unittest.TestCase):
         self.assertEqual(parse_duration('00:01:01'), 61)
         self.assertEqual(parse_duration('x:y'), None)
         self.assertEqual(parse_duration('3h11m53s'), 11513)
+        self.assertEqual(parse_duration('3h 11m 53s'), 11513)
+        self.assertEqual(parse_duration('3 hours 11 minutes 53 seconds'), 11513)
+        self.assertEqual(parse_duration('3 hours 11 mins 53 secs'), 11513)
         self.assertEqual(parse_duration('62m45s'), 3765)
         self.assertEqual(parse_duration('6m59s'), 419)
         self.assertEqual(parse_duration('49s'), 49)
index 4f02108720d5699a017a78fb25bb30ae3c23c831..e07750434807a48d1f036b999503409b9a9cfa73 100644 (file)
@@ -1332,8 +1332,10 @@ def parse_duration(s):
     if s is None:
         return None
 
+    s = s.strip()
+
     m = re.match(
-        r'(?:(?:(?P<hours>[0-9]+)[:h])?(?P<mins>[0-9]+)[:m])?(?P<secs>[0-9]+)s?(?::[0-9]+)?(?P<ms>\.[0-9]+)?$', s)
+        r'(?:(?:(?P<hours>[0-9]+)\s*(?:[:h]|hours?)\s*)?(?P<mins>[0-9]+)\s*(?:[:m]|mins?|minutes?)\s*)?(?P<secs>[0-9]+)(?P<ms>\.[0-9]+)?\s*(?:s|secs?|seconds?)?$', s)
     if not m:
         return None
     res = int(m.group('secs'))