From 9573a244cf61036aaf12f40daea5d6b5a5f68e3f Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Thu, 26 Oct 2023 15:06:56 +0300 Subject: [PATCH] added itmo script --- itmo.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 itmo.py diff --git a/itmo.py b/itmo.py new file mode 100644 index 0000000..a3af297 --- /dev/null +++ b/itmo.py @@ -0,0 +1,94 @@ +import os.path +import zoneinfo + +import requests + +from dateutil import parser +from ics import Calendar, Event + +token = input("token: ") + +params = { + "date_start": "2023-10-01", + "date_end": "2023-11-01", +} + +headers = { + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0", + "Accept": "application/json, text/plain, */*", + "Accept-Language": "ru", + # 'Accept-Encoding': 'gzip, deflate, br', + "Authorization": token, + "DNT": "1", + "Connection": "keep-alive", + "Referer": "https://my.itmo.ru/schedule", + "Sec-Fetch-Dest": "empty", + "Sec-Fetch-Mode": "cors", + "Sec-Fetch-Site": "same-origin", + # Requests doesn't support trailers + # 'TE': 'trailers', +} + + +response = requests.get( + "https://my.itmo.ru/api/schedule/schedule/personal", + params=params, + headers=headers, +) + +if response.status_code != 200: + raise ValueError(response.status_code) + +data = response.json()["data"] +c = Calendar() +for d_data in data: + date = d_data["date"] + lessons = d_data["lessons"] + if lessons: + for lesson in lessons: + t_from = parser.parse(f'{date}T{lesson["time_start"]}').replace( + tzinfo=zoneinfo.ZoneInfo("Europe/Moscow") + ) + t_end = parser.parse(f'{date}T{lesson["time_end"]}').replace( + tzinfo=zoneinfo.ZoneInfo("Europe/Moscow") + ) + name = lesson["subject"] + desc = [] + + if lesson["building"]: + if lesson["room"]: + desc.append(lesson["building"] + " " + lesson["room"]) + else: + desc.append(lesson["building"]) + elif lesson["room"]: + desc.append(lesson["room"]) + + if lesson["work_type"]: + desc.append(lesson["work_type"]) + + if lesson["note"]: + desc.append(f"Заметка: {lesson['note']}") + e = Event() + e.name = name + e.begin = t_from + e.end = t_end + + if lesson["teacher_name"]: + e.organizer = lesson["teacher_name"] + if lesson["zoom_url"]: + url = lesson["zoom_url"] + if lesson["zoom_password"] and "?pwd" not in url: + url += f"?pwd={lesson['zoom_password']}" + e.url = url + + if lesson["type"]: + e.categories = [lesson["type"]] + + e.description = "\n".join(desc) + c.events.add(e) + +if os.path.exists("itmo.ics"): + os.remove("itmo.ics") + +with open("itmo.ics", "w") as file: + file.writelines(c.serialize_iter())