2021-11-02 15:53:13 +08:00
|
|
|
from configparser import ConfigParser
|
|
|
|
from pathlib import Path
|
|
|
|
from json import dump
|
2021-11-02 16:52:50 +08:00
|
|
|
import subprocess
|
2021-11-02 15:53:13 +08:00
|
|
|
|
|
|
|
config = ConfigParser()
|
|
|
|
|
|
|
|
conf = config.read("config.ini")
|
|
|
|
|
|
|
|
options = {"ftp": ["host", "user", "pass", "lftp"], "file": ["local", "remote"]}
|
|
|
|
|
|
|
|
|
|
|
|
def check_config_valid(config: ConfigParser, options: dict[str, list[str]]) -> bool:
|
|
|
|
for sec in options:
|
|
|
|
if not config.has_section(sec):
|
|
|
|
return False
|
|
|
|
for opt in options[sec]:
|
|
|
|
if not config.has_option(sec, opt):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def default_config(config: ConfigParser, options: dict[str, list[str]]) -> None:
|
|
|
|
config.clear()
|
|
|
|
for sec in options:
|
|
|
|
config.add_section(sec)
|
|
|
|
for opt in options[sec]:
|
|
|
|
config[sec][opt] = ""
|
|
|
|
with open("config.ini", "w") as f:
|
|
|
|
config.write(f)
|
|
|
|
|
|
|
|
|
|
|
|
if not check_config_valid(config, options):
|
|
|
|
default_config(config, options)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_db(config: ConfigParser) -> None:
|
|
|
|
local_dir = Path(config["file"]["local"])
|
|
|
|
categories = Path(local_dir, "categories")
|
|
|
|
output = []
|
|
|
|
for category in categories.iterdir():
|
|
|
|
if not category.is_dir():
|
|
|
|
continue
|
|
|
|
output_category = {"name": category.name, "files": []}
|
|
|
|
for file in category.iterdir():
|
|
|
|
output_category["files"].append(file.name)
|
|
|
|
output.append(output_category)
|
|
|
|
with open(Path(local_dir, "db.json"), "w") as f:
|
|
|
|
dump(output, f)
|
|
|
|
|
|
|
|
|
|
|
|
generate_db(config)
|
2021-11-02 16:52:50 +08:00
|
|
|
|
|
|
|
sync_all_command = [
|
|
|
|
config["ftp"]["lftp"],
|
|
|
|
f'-e mirror -R -e --parallel=10 {config["file"]["local"]} {config["file"]["remote"]}; exit',
|
|
|
|
"-u",
|
|
|
|
f'{config["ftp"]["user"]},{config["ftp"]["pass"]}',
|
|
|
|
config["ftp"]["host"],
|
|
|
|
]
|
|
|
|
|
|
|
|
sync_files_command = [
|
|
|
|
config["ftp"]["lftp"],
|
|
|
|
f'-e mirror -R -e --parallel=10 {config["file"]["local"]}/categories {config["file"]["remote"]}/categories; exit',
|
|
|
|
"-u",
|
|
|
|
f'{config["ftp"]["user"]},{config["ftp"]["pass"]}',
|
|
|
|
config["ftp"]["host"],
|
|
|
|
]
|
|
|
|
|
|
|
|
print(subprocess.run(sync_files_command))
|