Skip to content

Instantly share code, notes, and snippets.

@0x1d107
Last active September 4, 2023 03:07
Show Gist options
  • Select an option

  • Save 0x1d107/b37ff974e493668af35263f8f3786058 to your computer and use it in GitHub Desktop.

Select an option

Save 0x1d107/b37ff974e493668af35263f8f3786058 to your computer and use it in GitHub Desktop.
USATU schedule parser
beautifulsoup4==4.11.1
certifi==2022.12.7
charset-normalizer==3.0.1
icalendar==5.0.4
idna==3.4
lxml==4.9.2
python-dateutil==2.8.2
pytz==2022.7.1
requests==2.28.2
six==1.16.0
soupsieve==2.3.2.post1
urllib3==1.26.14
#!/bin/env python
import datetime as dt
import requests as rq
from bs4 import BeautifulSoup
import uuid
import icalendar
UUID_NS=uuid.UUID('d7440a20-0018-4292-bb37-0a6ae62e61c3')
API_URL =\
"https://isu.ugatu.su/api/new_schedule_api/?schedule_semestr_id=231&WhatShow=1&student_group_id=2384&weeks=0"
soup = BeautifulSoup(rq.get(API_URL).text,features='lxml')
def get_week_day(weeknum,weekday=0):
semester_start_year = 2023
first_sept = dt.datetime(semester_start_year,9,1)
first_week_monday = first_sept - dt.timedelta(days=first_sept.weekday())
return (first_week_monday + dt.timedelta(weeks=weeknum-1,days=weekday)).date()
day_of_week=-1
cal = icalendar.Calendar()
cal.add('prodid','-//USATU Schedule//NONSGML v1.0//RU')
cal.add('version','2.0')
for row in soup.tbody.find_all('tr'):
if 'dayheader' in row['class']:
day_of_week+=1
if 'noinfo' in row['class']:
continue
dow_name,time,weeks,subject,class_type,prof,lecture_hall,comments,*_=row.find_all('td')
for week_str in weeks.get_text().split():
weeknum = int(week_str)
start_str,end_str = time.get_text().split('-')
day = get_week_day(weeknum,day_of_week)
start_time = dt.time.fromisoformat(start_str)
end_time = dt.time.fromisoformat(end_str)
start_dt = dt.datetime.combine(day,start_time)
end_dt = dt.datetime.combine(day,end_time)
lesson_evt = icalendar.Event()
subj_name = subject.get_text()
if not subj_name:
subj_name = class_type.get_text()
summary = f"{subj_name} {class_type.get_text()} {lecture_hall.get_text()}"
lesson_evt.add('summary',summary)
lesson_evt.add('dtstart',start_dt)
lesson_evt.add('dtend',end_dt)
lesson_evt.add('uid',uuid.uuid3(UUID_NS,f"{subj_name} {start_dt}"))
cal.add_component(lesson_evt)
#print(summary)
with open('schedule.ics','wb') as f:
f.write(cal.to_ical())
print(cal.to_ical().decode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment