-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
62 lines (48 loc) · 1.41 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import re
import subprocess
COLORS = {
"red": "\x1b[31m",
"green": "\x1b[32m",
"yellow": "\x1b[33m",
"blue": "\x1b[34m",
"purple": "\x1b[35m"
}
END = "\x1b[0m"
def cprint(text, color=None):
if not color or color not in COLORS:
print(text)
return
print(f"{COLORS[color]}{text}{END}")
def run_command(command):
cprint(command, "blue")
proc = subprocess.Popen(command, shell=True)
proc.communicate()
def escape(contents):
contents = contents.replace('\\', '\\\\').replace('"', '\\"')
contents = contents.replace('`', '\\`').replace('$', '\\$')
return contents
def question(question, valid_options):
result = None
while result not in valid_options:
result = input(f"{COLORS['purple']}{question}?{END} ").lower().strip()
return result
def list_movies(location, allow_mp4=True):
if os.path.isfile(location):
return [{
"dir": os.path.dirname(location),
"file": os.path.basename(location)
}]
if not os.path.isdir(location):
return []
return [
{
"dir": location, "file": f
} for f in sorted(os.listdir(location))
if f.endswith(".mkv") or (f.endswith(".mp4") and allow_mp4)
]
def mkv_property(track, property_name):
result = re.search(fr"\| \+ {property_name}: (.*)", track)
if result:
return result.group(1)
return ""