canvas-student-data-export/module/singlefile.py

38 lines
1.2 KiB
Python
Raw Normal View History

from pathlib import Path
2023-10-27 16:24:52 -06:00
from subprocess import run
2024-01-26 08:55:17 -07:00
from .const import global_consts
2023-10-27 16:24:52 -06:00
SINGLEFILE_BINARY_PATH = "./node_modules/single-file/cli/single-file"
# TODO: have this be specified by a required arg.
CHROME_PATH = "/usr/bin/google-chrome"
2023-10-27 16:24:52 -06:00
def add_quotes(s):
return "\"" + str(s).strip("\"") + "\""
2023-10-27 16:24:52 -06:00
2024-01-26 08:55:17 -07:00
def download_page(url, output_path, output_name_template=""):
2023-10-27 16:30:16 -06:00
# TODO: we can probably safely exclude pages that match the regex r'/external_tools/retrieve\?'
if output_name_template and Path(output_path, output_name_template).exists():
print('exists')
return
2023-10-27 16:24:52 -06:00
args = [
add_quotes(SINGLEFILE_BINARY_PATH),
"--browser-executable-path=" + add_quotes(CHROME_PATH.strip("\"")),
2024-01-26 08:55:17 -07:00
"--browser-cookies-file=" + add_quotes(global_consts.COOKIES_PATH),
2023-10-27 16:24:52 -06:00
"--output-directory=" + add_quotes(output_path),
add_quotes(url)
]
if output_name_template != "":
args.append("--filename-template=" + add_quotes(output_name_template))
try:
run("node " + " ".join(args), shell=True)
except Exception as e:
print("Was not able to save the URL " + url + " using singlefile. The reported error was " + e.strerror)