This repository has been archived on 2025-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
metro-schedule-parser/get_schedule.py

52 lines
1.5 KiB
Python
Raw Normal View History

2023-06-15 19:54:14 -04:00
import requests
import json
2023-06-15 22:57:26 -04:00
from datetime import datetime
2023-06-15 19:54:14 -04:00
def get_id():
with open("id.txt", "r") as file:
return file.read()
def get_shift(schedule_day):
shift = schedule_day["DailyShift"][0]
if (shift == "-"):
return
2023-06-15 22:57:26 -04:00
date_obj = datetime.strptime(schedule_day["StartDate"], "%Y-%m-%dT%H:%M:%S")
2023-06-15 19:54:14 -04:00
split_shift = shift.split("-", 1)
2023-06-15 22:57:26 -04:00
time = datetime.strptime(split_shift[0], "%H:%M")
start_time_obj = date_obj.replace(hour = time.hour, minute = time.minute)
2023-06-15 19:54:14 -04:00
split_shift = split_shift[1].split(" ", 1)
2023-06-15 22:57:26 -04:00
time = datetime.strptime(split_shift[0], "%H:%M")
end_time_obj = date_obj.replace(hour = time.hour, minute = time.minute)
2023-06-15 19:54:14 -04:00
dept = split_shift[1]
2023-06-15 22:57:26 -04:00
return (start_time_obj.strftime("%Y%m%dT%H%M%S"), end_time_obj.strftime("%Y%m%dT%H%M%S"), dept)
2023-06-15 19:54:14 -04:00
sess = requests.Session()
employee_id = get_id()
sess.get("https://myschedule.metro.ca/api/Login/" + employee_id)
total_schedule = sess.get("https://myschedule.metro.ca/api/Employee/" + employee_id).json()
with open("calendar.ics", "w") as file:
file.write("BEGIN:VCALENDAR\n")
file.write("VERSION:2.0\n")
file.write("PRODID:SuperNovaa41\n")
2023-06-17 16:53:19 -04:00
for i in range(-1, -8, -1):
res = get_shift(total_schedule["WorkTime"][i])
if (res is None):
continue
2023-06-15 19:54:14 -04:00
file.write("BEGIN:VEVENT\n")
file.write("DTSTART:" + res[0] + "\n")
file.write("DTEND:" + res[1] + "\n")
file.write("SUMMARY: Work (" + res[2] + ")\n")
file.write("END:VEVENT\n")
file.write("END:VCALENDAR\n")