OctoprintReservations/octoprint_reservations/main.py

66 lines
1.8 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
import argparse
from datetime import datetime, timedelta
import pathlib
from octorest import OctoRest
from ruamel.yaml import YAML
from .google_calendar import CalendarService
def get_printer_job(calendar, printer):
try:
octoprint_client = OctoRest(url=printer["url"], apikey=printer["apikey"])
current_job = octoprint_client.job_info()
except Exception as e:
print(f"Failed to get current job for {printer['name']}: {e}")
return
if (
current_job["state"] == "Printing"
and current_job["progress"]["printTimeLeft"] is not None
):
now = datetime.utcnow()
start = now - timedelta(seconds=current_job["progress"]["printTime"])
estimated_end = now + timedelta(
seconds=current_job["progress"]["printTimeLeft"]
)
print(current_job["job"]["file"]["name"], start, estimated_end)
event_name = (
f'{current_job["job"]["user"]} | {printer["shop"]} - {printer["name"]}'
)
calendar.insert_or_update_event(
printer["calendar_id"],
f'OctoPrint Reservations: {printer["name"]}',
event_name,
start,
estimated_end,
)
def main():
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument(
"--config",
type=pathlib.Path,
default="printers.yaml",
help="A yaml file defining printers to query (default: %(default)s)",
)
args = parser.parse_args()
calendar = CalendarService()
yaml = YAML(typ="safe")
with open(args.config) as f:
printers = yaml.load(f)
for printer in printers:
get_printer_job(calendar, printer)
if __name__ == "__main__":
main()