-
Notifications
You must be signed in to change notification settings - Fork 2
/
subtitle_utils.py
90 lines (80 loc) · 2.9 KB
/
subtitle_utils.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from config import get_config
import re
config = get_config()
# CHS
CHS_LIST = config["Subtitle"]["Keyword"]["CHS"]
# CHT
CHT_LIST = config["Subtitle"]["Keyword"]["CHT"]
# JPN and CHS
JP_SC_LIST = config["Subtitle"]["Keyword"]["JP_SC"]
# JPN and CHT
JP_TC_LIST = config["Subtitle"]["Keyword"]["JP_TC"]
# JPN
JP_LIST = config["Subtitle"]["Keyword"]["JP"]
# RUS
RU_LIST = config["Subtitle"]["Keyword"]["RU"]
ALLOWED_FONT_EXTENSIONS = config["Font"]["AllowedExtensions"]
def subtitle_info_checker(subtitle_file_name: str) -> dict:
"""
Check the subtitle file name and analyze language and group information
:param subtitle_file_name: subtitle file name (path)
:return: a dictionary of language and group information, empty string if not found
"""
user_default_language = config["Subtitle"]["DefaultLanguage"]
is_default_language = False
if any(indicator in subtitle_file_name.lower() for indicator in JP_SC_LIST):
language = "jp_sc"
mkv_language = "chi"
ietf_language = "zh-Hans"
elif any(indicator in subtitle_file_name.lower() for indicator in JP_TC_LIST):
language = "jp_tc"
mkv_language = "chi"
ietf_language = "zh-Hant"
elif any(indicator in subtitle_file_name.lower() for indicator in CHS_LIST):
language = "chs"
mkv_language = "chi"
ietf_language = "zh-Hans"
elif any(indicator in subtitle_file_name.lower() for indicator in CHT_LIST):
language = "cht"
mkv_language = "chi"
ietf_language = "zh-Hant"
elif any(indicator in subtitle_file_name.lower() for indicator in JP_LIST):
language = "jpn"
mkv_language = "jpn"
ietf_language = "ja"
elif any(indicator in subtitle_file_name.lower() for indicator in RU_LIST):
language = "rus"
mkv_language = "rus"
ietf_language = "ru"
else:
language = ""
mkv_language = ""
ietf_language = ""
if language == user_default_language:
is_default_language = True
elif language == "jp_sc" and user_default_language == "chs":
is_default_language = True
elif language == "jp_tc" and user_default_language == "cht":
is_default_language = True
sub_author = re.search(r'(^\[)(\w|\d|-|_|&|\.|!)+(]+?)', subtitle_file_name)
if sub_author is not None:
sub_author = sub_author.group(0)
else:
sub_author = ""
return {
"language": language,
"sub_author": sub_author.replace("[", "").replace("]", ""),
"default_language": is_default_language,
"mkv_language": mkv_language,
"ietf_language": ietf_language
}
def is_font_file(f: str) -> bool:
"""
Check the file extension if is a font
:param f: file name (path)
:return: true if is a font file, false if not
"""
if any(f.lower().endswith(ext) for ext in ALLOWED_FONT_EXTENSIONS):
return True
else:
return False