47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
from datetime import datetime, timedelta
|
|
|
|
from octorest import OctoRest
|
|
from ruamel.yaml import YAML
|
|
|
|
import google_calendar
|
|
|
|
|
|
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"]}'
|
|
|
|
google_calendar.insert_or_update_event(
|
|
calendar,
|
|
f'OctoPrint Reservations: {printer["name"]}',
|
|
event_name,
|
|
start,
|
|
estimated_end,
|
|
)
|
|
|
|
else:
|
|
print("No active job")
|
|
|
|
|
|
def main():
|
|
creds = google_calendar.authenticate()
|
|
calendar = google_calendar.get_service(creds)
|
|
|
|
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()
|