From 352d5da81219e2675ef8cac9383ab0dfbd161a19 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Fri, 24 Dec 2021 03:02:50 +0530 Subject: [PATCH] [utils] Improve `parse_count` --- test/test_utils.py | 7 +++++++ yt_dlp/utils.py | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/test/test_utils.py b/test/test_utils.py index 22dda4f37..4edf4e1b1 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1156,9 +1156,16 @@ class TestUtil(unittest.TestCase): self.assertEqual(parse_count('1000'), 1000) self.assertEqual(parse_count('1.000'), 1000) self.assertEqual(parse_count('1.1k'), 1100) + self.assertEqual(parse_count('1.1 k'), 1100) + self.assertEqual(parse_count('1,1 k'), 1100) self.assertEqual(parse_count('1.1kk'), 1100000) self.assertEqual(parse_count('1.1kk '), 1100000) + self.assertEqual(parse_count('1,1kk'), 1100000) + self.assertEqual(parse_count('100 views'), 100) + self.assertEqual(parse_count('1,100 views'), 1100) self.assertEqual(parse_count('1.1kk views'), 1100000) + self.assertEqual(parse_count('10M views'), 10000000) + self.assertEqual(parse_count('has 10M views'), 10000000) def test_parse_resolution(self): self.assertEqual(parse_resolution(None), {}) diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 2c5e6560a..cdc9a0ecf 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -2211,7 +2211,7 @@ def parse_count(s): if s is None: return None - s = s.strip() + s = re.sub(r'^[^\d]+\s', '', s).strip() if re.match(r'^[\d,.]+$', s): return str_to_int(s) @@ -2223,9 +2223,17 @@ def parse_count(s): 'M': 1000 ** 2, 'kk': 1000 ** 2, 'KK': 1000 ** 2, + 'b': 1000 ** 3, + 'B': 1000 ** 3, } - return lookup_unit_table(_UNIT_TABLE, s) + ret = lookup_unit_table(_UNIT_TABLE, s) + if ret is not None: + return ret + + mobj = re.match(r'([\d,.]+)(?:$|\s)', s) + if mobj: + return str_to_int(mobj.group(1)) def parse_resolution(s):