OctoprintReservations/main.py

52 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
from datetime import datetime, timedelta
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":
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():
calendar = CalendarService()
yaml = YAML(typ="safe")
with open("printers.yaml") as f:
printers = yaml.load(f)
for printer in printers:
get_printer_job(calendar, printer)
if __name__ == "__main__":
main()