moodle-import-worker/worker.py
2022-09-12 16:55:01 +00:00

54 lines
1.7 KiB
Python

from pathlib import Path
from configparser import ConfigParser
import re
import subprocess
import shutil
config = ConfigParser()
config.read("config.ini")
source = Path(config["IMPORTER"]["SourcePath"])
completed = source / "completed"
completed.mkdir(exist_ok=True)
courses = dict()
mbzre = re.compile(r"course_(?P<courseid>\d+)_(?P<categoryid>\d+)_(?P<timestamp>\d+).mbz")
def call_php(courseid: int, categoryid: int, timestamp: int):
response = subprocess.run(["php", config["IMPORTER"]["PHPScript"], f"--courseid={courseid}", f"--categoryid={categoryid}", f"--timestamp={timestamp}"], cwd=Path(config["IMPORTER"]["PHPScript"]).parent, capture_output=True, encoding="utf8")
print(response.stdout)
print(response.stderr)
return (not response.stderr) and (not "!!!" in response.stdout)
for mbz in source.glob("course_*_*_*.mbz"):
meta = mbzre.search(str(mbz)).groupdict()
if not meta["courseid"] in courses.keys():
courses[int(meta["courseid"])] = list()
courses[int(meta["courseid"])].append((int(meta["categoryid"]), int(meta["timestamp"])))
for course, data in sorted(courses.items(), key = lambda x: x[0]):
current = config.getint("STATUS", str(course), fallback=0)
byage = sorted(data, key = lambda x: -x[1])
if byage[0][1] > current:
if not call_php(course, byage[0][0], byage[0][1]):
print(f"Something went wrong while importing {course}.")
continue
config["STATUS"][str(course)] = str(byage[0][1])
for categoryid, timestamp in byage:
shutil.move(source / f"course_{course}_{categoryid}_{timestamp}.mbz", completed)
with open("config.ini", "w") as configfile:
config.write(configfile)