Skip to content

Commit

Permalink
Resolve #5
Browse files Browse the repository at this point in the history
- Add support of `unrar`
- Refine code
  • Loading branch information
Masterain98 committed Jul 17, 2023
1 parent 9503e56 commit 1546533
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
5 changes: 3 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import json

REQUIRED_CONFIG = ["TaskSettings", "Font", "Subtitle"]
REQUIRED_CONFIG = ["TaskSettings", "Font", "Subtitle", "mkvmerge"]


def make_default_config():
Expand All @@ -14,7 +14,8 @@ def make_default_config():
"OutputSuffixName": "_Plex"
},
"Font": {
"AllowedExtensions": [".ttf", ".otf", ".ttc"]
"AllowedExtensions": [".ttf", ".otf", ".ttc"],
"Unrar_Path": "C:\\Program Files\\WinRAR\\UnRAR.exe"
},
"Subtitle": {
"Keyword": {
Expand Down
39 changes: 23 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pymkv import MKVFile, MKVTrack
import py7zr
import re
import patoolib
from config import get_config
from compressed import unzip
from subtitle_utils import subtitle_info_checker, is_font_file
Expand All @@ -12,12 +13,14 @@
try:
config = get_config()
except ValueError:
raise ValueError("Config file error")
input("Config file error, press ENTER to exit")
exit(0)
DELETE_FONTS = config["TaskSettings"]["DeleteFonts"]
DELETE_ORIGINAL_MKV = config["TaskSettings"]["DeleteOriginalMKV"]
DELETE_ORIGINAL_MKA = config["TaskSettings"]["DeleteOriginalMKA"]
DELETE_SUB = config["TaskSettings"]["DeleteSubtitle"]
SUFFIX_NAME = config["TaskSettings"]["OutputSuffixName"]
UNRAR_PATH = config["Font"]["Unrar_Path"]
if SUFFIX_NAME == "":
SUFFIX_NAME = "_Plex"
if config["mkvmerge"]["path"] != "":
Expand All @@ -26,7 +29,6 @@
print("mkvmerge path not set, using mkvmerge.exe in the working directory")
MKVMERGE_PATH = "mkvmerge"


if __name__ == '__main__':
delete_list = []
move_list = []
Expand All @@ -46,25 +48,30 @@
unfiltered_font_list = []
for file_name in folder_list:
# if there is a zipped fonts file, unzip it
if "font" in file_name.lower() and ".zip" in file_name:
# zip extension
print("Find font package file: " + file_name)
if not os.path.exists("Fonts"):
os.makedirs("Fonts")
print("Fonts sub-directory created")
unfiltered_font_list = unzip(file_name, "cp437")
print("Unzipped to /Fonts: " + str(unfiltered_font_list))
print("=" * 20)
elif "Font" in file_name and ".7z" in file_name:
# 7z extension
if "font" in file_name.lower():
print("Find font package file: " + file_name)
if not os.path.exists("Fonts"):
os.makedirs("Fonts")
print("Fonts sub-directory created")
with py7zr.SevenZipFile(file_name, mode='r') as z:
z.extractall("Fonts")
unfiltered_font_list = z.getnames()
if ".zip" in file_name:
# zip extension
unfiltered_font_list = unzip(file_name, "utf-8")
print("Unzipped to /Fonts: " + str(unfiltered_font_list))
elif ".7z" in file_name:
# 7z extension
with py7zr.SevenZipFile(file_name, mode='r') as z:
z.extractall("Fonts")
unfiltered_font_list = z.getnames()
print("Unzipped to /Fonts: " + str(unfiltered_font_list))
elif ".rar" in file_name:
if UNRAR_PATH != "":
patoolib.extract_archive(file_name, outdir="./Fonts/", program=UNRAR_PATH)
unfiltered_font_list = os.listdir("Fonts")
else:
print("Unrar path not set, please manually unrar the file")
else:
print("%s is an unrecognized compressed file format, please manually unzip the file" % file_name)
print("=" * 20)
# Filter fonts file
font_list = list(filter(is_font_file, unfiltered_font_list))
print("Loading fonts: " + str(font_list))
Expand Down
Binary file modified requirements.txt
Binary file not shown.
33 changes: 17 additions & 16 deletions subtitle_utils.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
from config import get_config
import re

config = get_config()
# zh-CN
CHS_LIST = config["Subtitle"]["Keyword"]["CHS"]
# zh-TW or zh-HK
CHT_LIST = config["Subtitle"]["Keyword"]["CHT"]
# Jpn and zh-CN
JP_SC_LIST = config["Subtitle"]["Keyword"]["JP_SC"]
# Jpn and zh-TW/zh-HK
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
"""
config = get_config()

user_default_language = config["Subtitle"]["DefaultLanguage"]
is_default_language = False
# zh-CN
CHS_LIST = config["Subtitle"]["Keyword"]["CHS"]
# zh-TW or zh-HK
CHT_LIST = config["Subtitle"]["Keyword"]["CHT"]
# Jpn and zh-CN
JP_SC_LIST = config["Subtitle"]["Keyword"]["JP_SC"]
# Jpn and zh-TW/zh-HK
JP_TC_LIST = config["Subtitle"]["Keyword"]["JP_TC"]
# Jpn
JP_LIST = config["Subtitle"]["Keyword"]["JP"]
# Rus
RU_LIST = config["Subtitle"]["Keyword"]["RU"]

if any(indicator in subtitle_file_name.lower() for indicator in JP_SC_LIST):
language = "jp_sc"
Expand Down Expand Up @@ -56,7 +59,7 @@ def subtitle_info_checker(subtitle_file_name: str) -> dict:
if language == user_default_language:
is_default_language = True

sub_author = re.search(r'(^\[)(\w|\d|\-|\_|\&|\.|\!)+(\]+?)', subtitle_file_name)
sub_author = re.search(r'(^\[)(\w|\d|-|_|&|\.|!)+(]+?)', subtitle_file_name)
if sub_author is not None:
sub_author = sub_author.group(0)
else:
Expand All @@ -77,8 +80,6 @@ def is_font_file(f: str) -> bool:
:param f: file name (path)
:return: true if is a font file, false if not
"""
config = get_config()
ALLOWED_FONT_EXTENSIONS = config["Font"]["AllowedExtensions"]
if any(f.lower().endswith(ext) for ext in ALLOWED_FONT_EXTENSIONS):
return True
else:
Expand Down

0 comments on commit 1546533

Please sign in to comment.