aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/utils.py
diff options
context:
space:
mode:
authorSergey M․ <dstftw@gmail.com>2016-03-13 16:27:20 +0600
committerSergey M․ <dstftw@gmail.com>2016-03-13 16:27:20 +0600
commitfb47597b09cf8474931a4509bebc510403b30e25 (patch)
treebb2bdf376cade1839848ef30be52a798692d5ab2 /youtube_dl/utils.py
parent450b233cc293e9a1fb63daf0ee0deb48a809dfd9 (diff)
[bbc] Generalize unit table lookup and add parse_count
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r--youtube_dl/utils.py37
1 files changed, 30 insertions, 7 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index 22a39a0ab..1d6ee9964 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -1316,6 +1316,17 @@ def format_bytes(bytes):
return '%.2f%s' % (converted, suffix)
+def lookup_unit_table(unit_table, s):
+ units_re = '|'.join(re.escape(u) for u in unit_table)
+ m = re.match(
+ r'(?P<num>[0-9]+(?:[,.][0-9]*)?)\s*(?P<unit>%s)' % units_re, s)
+ if not m:
+ return None
+ num_str = m.group('num').replace(',', '.')
+ mult = unit_table[m.group('unit')]
+ return int(float(num_str) * mult)
+
+
def parse_filesize(s):
if s is None:
return None
@@ -1359,16 +1370,28 @@ def parse_filesize(s):
'Yb': 1000 ** 8,
}
- units_re = '|'.join(re.escape(u) for u in _UNIT_TABLE)
- m = re.match(
- r'(?P<num>[0-9]+(?:[,.][0-9]*)?)\s*(?P<unit>%s)' % units_re, s)
- if not m:
+ return lookup_unit_table(_UNIT_TABLE, s)
+
+
+def parse_count(s):
+ if s is None:
return None
- num_str = m.group('num').replace(',', '.')
- mult = _UNIT_TABLE[m.group('unit')]
- return int(float(num_str) * mult)
+ s = s.strip()
+
+ if re.match(r'^[\d,.]+$', s):
+ return str_to_int(s)
+
+ _UNIT_TABLE = {
+ 'k': 1000,
+ 'K': 1000,
+ 'm': 1000 ** 2,
+ 'M': 1000 ** 2,
+ 'kk': 1000 ** 2,
+ 'KK': 1000 ** 2,
+ }
+ return lookup_unit_table(_UNIT_TABLE, s)
def month_by_name(name):
""" Return the number of a month by (locale-independently) English name """