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): octoprint_client = OctoRest(url=printer["url"], apikey=printer["apikey"]) current_job = octoprint_client.job_info() 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"]} | Digital Fabrication and Electronics Lab - {printer["name"]}' calendar.insert_or_update_event( f'OctoPrint Reservations: {printer["name"]}', event_name, start, estimated_end, ) else: print("No active job") 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()