AO3-DL/python/install.py
2025-08-19 09:11:07 -07:00

167 lines
4.7 KiB
Python
Executable file

#!/usr/bin/env python3
import os.path
import shutil
import common
import download
import sql
common.init(
description="Initializes the database, downloads AO3's official CSS stylesheets, and installs the Web UI."
)
config = common.config
cautiousMode = False
def abspath(input: str) -> str:
return os.path.abspath(os.path.expanduser(input))
def getbool(prompt: str) -> bool:
yesValues = ["yes", "y", "true", "t", "go", "continue"]
noValues = ["no", "n", "false", "f", "cancel", "stop"]
if prompt[-1:] != "\n":
prompt += "\n"
loopno = 0
while True:
loopno += 1
text = input(prompt).lower()
if len(text) == 0:
text = " "
if text in yesValues:
return True
elif text in noValues:
return False
if loopno > 3:
print(f"Valid options that mean 'yes' are: {yesValues}")
print(f"Valid options that mean 'no' are: {noValues}")
for i in (
config["dirRaws"],
config["dirAo3Css"],
config["dirImg"],
config["dirWebUi"],
os.path.dirname(config["ao3UsernameFile"]),
os.path.dirname(config["ao3PasswordFile"]),
os.path.dirname(config["ao3SessionPickle"]),
):
os.makedirs(i, exist_ok=True)
copiedFiles = (
("list.css", "../webui/files/"),
("list.php", "../webui/files/"),
("work.css", "../webui/files/"),
("work.php", "../webui/files/"),
("index.html", "../webui/files/"),
)
needCopyFiles = False
for i in copiedFiles:
target = os.path.join(abspath(config["dirWebUi"]), i[0])
if not os.path.exists(target):
needCopyFiles = True
elif not os.path.getsize(target):
needCopyFiles = True
if needCopyFiles:
doCopyLoop = True
while doCopyLoop:
doCopy = getbool(
f"Will now copy the following files to {config['dirWebUi']}:\n{[x[0] for x in copiedFiles]}\nEnter Yes to continue or No to stop."
)
if doCopy:
doCopyLoop = False
else:
raise SystemExit("Operation canceled by user.")
for i in copiedFiles:
local = abspath(f"{i[1]}{i[0]}")
target = abspath(f"{config['dirWebUi']}/{i[0]}")
# print(f"Local: {local} Target: {target}")
if os.path.exists(local):
if local != target:
if os.path.exists(target):
print(f"{i[0]} Already installed, skipping...")
copyLoop = False
else:
if (not cautiousMode) or getbool("Installing"):
shutil.copyfile(src=local, dst=target)
else:
raise FileNotFoundError(
f"{i[0]} not found in expected location ({local}). It needs to be copied to {target}."
)
del local
del target
else:
print("Files already present, skipping...")
needDlAo3Css = False
missing = [
"01-core.css",
"02-elements.css",
"03-region-header.css",
"04-region-dashboard.css",
"05-region-main.css",
"06-region-footer.css",
"07-interactions.css",
"08-actions.css",
"09-roles-states.css",
"10-types-groups.css",
"11-group-listbox.css",
"12-group-meta.css",
"13-group-blurb.css",
"14-group-preface.css",
"15-group-comments.css",
"16-zone-system.css",
"17-zone-home.css",
"18-zone-searchbrowse.css",
"19-zone-tags.css",
"20-zone-translation.css",
"21-userstuff.css",
"22-system-messages.css",
"25-media-midsize.css",
"26-media-narrow.css",
"27-media-aural.css",
"28-media-print.css",
]
for filename in os.listdir(config["dirAo3Css"]):
if (filename in missing) and (
os.path.getsize(os.path.join(config["dirAo3Css"], filename))
):
missing.remove(filename)
if len(missing) != 0:
needDlAo3Css = True
else:
print("AO3 official site-wide CSS already downloaded, skipping...")
if needDlAo3Css:
doAO3CSSLoop = True
while doAO3CSSLoop:
print(f"You are missing the following ao3css files: {[x for x in missing]}")
doAO3CSS = getbool(
"Will now download AO3 CSS files, enter Yes to continue or No to stop."
)
if doAO3CSS:
doAO3CSSLoop = False
else:
raise SystemExit("Operation canceled by user.")
urlRoot = "https://raw.githubusercontent.com/otwcode/otwarchive/refs/heads/master/public/stylesheets/site/2.0/"
for sheet in missing:
url = f"{urlRoot}{sheet}"
with open(os.path.join(config["dirAo3Css"], sheet), "wb") as file:
file.write(download.getPage(url=url, logger=common.logger))
sql.init(config=common.config, logger=common.logger)
if needCopyFiles or needDlAo3Css:
print("AOMO successfully installed.")
else:
print("AOMO was already installed.")