Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Advanced .torrent file output directory settings #72

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions default.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[backend]
## name of a file in templates/ dir
default_template = fakestash-v2
## where torrents are placed
torrent_directory = /torrents
## Comma-separated list of directories where torrents are placed.
## If more than one directory is specified, the first is the one
## that will be used for uploading to EMP.
torrent_directories = /torrents
## port that the backend listens on
port = 9932
# jinja template for title
Expand Down
47 changes: 29 additions & 18 deletions emp_stash_fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@
# CONFIG #
##########

def renameKey(section: str, oldkey: str, newkey: str) -> None:
if conf[section].has_option(oldkey):
conf[section][oldkey].key = newkey
conf.update_file()
logger.info(f"Key '{oldkey}' renamed to '{newkey}'")

conf = configupdater.ConfigUpdater()
default_conf = configupdater.ConfigUpdater()

Expand Down Expand Up @@ -150,10 +156,8 @@

logger.info(f"Reading config from {config_file}")
conf.read(config_file)
if conf["backend"].has_option("date_default"):
conf["backend"]["date_default"].key = "date_format"
conf.update_file()
logger.info("Key 'date_default' renamed to 'date_format'")
renameKey("backend", "date_default", "date_format")
renameKey("backend", "torrent_directory", "torrent_directories")
default_conf.read("default.ini")
skip_sections = ["empornium", "empornium.tags"]
for section in default_conf.sections():
Expand Down Expand Up @@ -242,18 +246,24 @@ def mapPath(f: dict) -> dict:
assert STASH_URL is not None
PORT = args.port[0] if args.port else int(getConfigOption(conf, "backend", "port", "9932")) # type: ignore
DEFAULT_TEMPLATE = getConfigOption(conf, "backend", "default_template", "fakestash-v2")
TORRENT_DIR = (
args.torrentdir[0]
TORRENT_DIRS = (
[args.torrentdir[0]]
if args.torrentdir
else getConfigOption(conf, "backend", "torrent_directory", str(pathlib.Path.home()))
else [x.strip() for x in getConfigOption(conf, "backend", "torrent_directories", str(pathlib.Path.home())).split(",")]
)
assert TORRENT_DIR is not None
if not os.path.isdir(TORRENT_DIR):
if os.path.isfile(TORRENT_DIR):
logger.critical(f"Cannot use {TORRENT_DIR} for torrents, path is a file")
exit(1)
logger.info(f"Creating directory {TORRENT_DIR}")
os.makedirs(TORRENT_DIR)
assert TORRENT_DIRS is not None and len(TORRENT_DIRS) > 0
for dir in TORRENT_DIRS:
if not os.path.isdir(dir):
if os.path.isfile(dir):
logger.error(f"Cannot use {dir} for torrents, path is a file")
TORRENT_DIRS.remove(dir)
exit(1)
logger.info(f"Creating directory {dir}")
os.makedirs(dir)
logger.debug(f"Torrent directories: {TORRENT_DIRS}")
if len(TORRENT_DIRS) == 0:
logger.critical("No valid output directories found")
exit(1)
TITLE_FORMAT = None
TITLE_TEMPLATE = None
if conf["backend"].has_option("title_default"):
Expand Down Expand Up @@ -605,7 +615,7 @@ def generate():
# logger.debug(f"Sanitized filename: {basename}")
# basename = stash_file["basename"]
temppath = os.path.join(tempdir.name, basename + ".torrent")
torrent_path = os.path.join(TORRENT_DIR, stash_file["basename"] + ".torrent")
torrent_paths = [os.path.join(dir, stash_file["basename"] + ".torrent") for dir in TORRENT_DIRS]
logger.debug(f"Saving torrent to {temppath}")
cmd = [
"mktorrent",
Expand All @@ -626,9 +636,10 @@ def generate():
if process.returncode != 0:
tempdir.cleanup()
return error("mktorrent failed, command: " + " ".join(cmd), "Couldn't generate torrent")
shutil.move(temppath, torrent_path)
for path in torrent_paths:
shutil.copy(temppath, path)
tempdir.cleanup()
logger.debug(f"Moved torrent to {torrent_path}")
logger.debug(f"Moved torrent to {torrent_paths}")

#############
# MEDIAINFO #
Expand Down Expand Up @@ -793,7 +804,7 @@ def generate():
"cover": cover_remote_url,
"tags": " ".join(tags.tags),
"description": description,
"torrent_path": torrent_path,
"torrent_path": torrent_paths[0],
"file_path": stash_file["path"],
},
},
Expand Down