2019-01-29 13:46:24 +00:00
|
|
|
import re
|
|
|
|
import unicodedata
|
|
|
|
|
|
|
|
|
2020-04-11 12:05:23 +00:00
|
|
|
def _format_size(value, digits, unit):
|
|
|
|
if digits > 0:
|
|
|
|
return "{{:.{}f}}{}".format(digits, unit).format(value)
|
|
|
|
else:
|
|
|
|
return "{{:d}}{}".format(unit).format(value)
|
|
|
|
|
|
|
|
|
|
|
|
def format_size(bytes_, digits=1):
|
2020-04-11 11:08:42 +00:00
|
|
|
if bytes_ < 1024:
|
2020-04-11 12:05:23 +00:00
|
|
|
return _format_size(bytes_, digits, "B")
|
2020-04-11 11:08:42 +00:00
|
|
|
|
|
|
|
kilo = bytes_ / 1024
|
|
|
|
if kilo < 1024:
|
2020-04-11 12:05:23 +00:00
|
|
|
return _format_size(kilo, digits, "kB")
|
2020-04-11 11:08:42 +00:00
|
|
|
|
|
|
|
mega = kilo / 1024
|
|
|
|
if mega < 1024:
|
2020-04-11 12:05:23 +00:00
|
|
|
return _format_size(mega, digits, "MB")
|
2020-04-11 11:08:42 +00:00
|
|
|
|
2020-04-11 12:05:23 +00:00
|
|
|
return _format_size(mega / 1024, digits, "GB")
|
2020-04-11 11:08:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_duration(total_seconds):
|
|
|
|
total_seconds = int(total_seconds)
|
|
|
|
hours = total_seconds // 3600
|
|
|
|
remainder = total_seconds % 3600
|
|
|
|
minutes = remainder // 60
|
|
|
|
seconds = total_seconds % 60
|
|
|
|
|
|
|
|
if hours:
|
|
|
|
return "{} h {} min".format(hours, minutes)
|
|
|
|
|
|
|
|
if minutes:
|
|
|
|
return "{} min {} sec".format(minutes, seconds)
|
|
|
|
|
|
|
|
return "{} sec".format(seconds)
|
|
|
|
|
|
|
|
|
|
|
|
def read_int(msg, min, max, default):
|
|
|
|
msg = msg + " [default {}]: ".format(default)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
val = input(msg)
|
|
|
|
if not val:
|
|
|
|
return default
|
|
|
|
if min <= int(val) <= max:
|
|
|
|
return int(val)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-01-29 13:46:24 +00:00
|
|
|
def slugify(value):
|
2022-01-23 08:14:40 +00:00
|
|
|
value = unicodedata.normalize('NFKC', str(value))
|
|
|
|
value = re.sub(r'[^\w\s_-]', '', value)
|
|
|
|
value = re.sub(r'[\s_-]+', '_', value)
|
|
|
|
return value.strip("_").lower()
|
|
|
|
|
|
|
|
|
|
|
|
def titlify(value):
|
|
|
|
value = unicodedata.normalize('NFKC', str(value))
|
|
|
|
value = re.sub(r'[^\w\s\[\]().-]', '', value)
|
|
|
|
value = re.sub(r'\s+', ' ', value)
|
|
|
|
return value.strip()
|
2021-01-14 20:38:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
VIDEO_PATTERNS = [
|
|
|
|
r"^(?P<id>\d+)?$",
|
|
|
|
r"^https://(www.)?twitch.tv/videos/(?P<id>\d+)(\?.+)?$",
|
|
|
|
]
|
|
|
|
|
|
|
|
CLIP_PATTERNS = [
|
2021-02-13 01:44:51 +00:00
|
|
|
r"^(?P<slug>[A-Za-z0-9]+(?:-[A-Za-z0-9_-]{16})?)$",
|
|
|
|
r"^https://(www.)?twitch.tv/\w+/clip/(?P<slug>[A-Za-z0-9]+(?:-[A-Za-z0-9_-]{16})?)(\?.+)?$",
|
|
|
|
r"^https://clips.twitch.tv/(?P<slug>[A-Za-z0-9]+(?:-[A-Za-z0-9_-]{16})?)(\?.+)?$",
|
2021-01-14 20:38:56 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def parse_video_identifier(identifier):
|
|
|
|
"""Given a video ID or URL returns the video ID, or null if not matched"""
|
|
|
|
for pattern in VIDEO_PATTERNS:
|
|
|
|
match = re.match(pattern, identifier)
|
|
|
|
if match:
|
|
|
|
return match.group("id")
|
|
|
|
|
|
|
|
|
|
|
|
def parse_clip_identifier(identifier):
|
|
|
|
"""Given a clip slug or URL returns the clip slug, or null if not matched"""
|
|
|
|
for pattern in CLIP_PATTERNS:
|
|
|
|
match = re.match(pattern, identifier)
|
|
|
|
if match:
|
|
|
|
return match.group("slug")
|