delojza/src/lib/util.py

44 lines
1.1 KiB
Python

import errno
import os
import re
import subprocess
import unicodedata
import youtube_dl
def ytdl_can(url: str):
ies = youtube_dl.extractor.gen_extractors()
for ie in ies:
if ie.suitable(url) and ie.IE_NAME != 'generic' \
and '/channel/' not in url:
# Site has dedicated extractor
return True
return False
def mkdir_p(path: str):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def _get_percent_filled(directory: str):
output = subprocess.check_output(["df", directory])
percents_re = re.search(r"[0-9]+%", output.decode('utf-8'))
if not percents_re:
raise RuntimeError
return int(percents_re.group(0)[:-1])
# https://github.com/django/django/blob/master/django/utils/text.py#L393
def sanitize(filepath: str):
if filepath is None:
return None
filepath = unicodedata.normalize('NFKD', filepath).encode('ascii', 'ignore').decode('ascii')
return re.sub(r'[^\w.()\[\]{}#-]', '_', filepath)