-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eb8d6a5
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.xml | ||
.idea | ||
.venv | ||
__pycache__ | ||
*.ics | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import argparse | ||
|
||
from convert import Convert | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--xml", type=str, help="Location of timetable xml file", required=True) | ||
parser.add_argument("--begin", type=str, help="Semester beginning date", required=True) | ||
parser.add_argument("--end", type=str, help="Semester ending date", required=True) | ||
args = parser.parse_args() | ||
|
||
c = Convert(args.xml) | ||
c.get_calendar(c.get_subjects(), args.begin, args.end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import datetime | ||
import os | ||
import tempfile | ||
import xml.etree.ElementTree as ElementTree | ||
from dateutil import parser | ||
from icalendar import Calendar, Event | ||
|
||
|
||
class Convert(): | ||
def __init__(self, filename): | ||
self.filename = filename | ||
|
||
def get_subjects(self): | ||
result = [] | ||
|
||
tree = ElementTree.parse(self.filename) | ||
root = tree.getroot() | ||
for subject in root.findall('subject'): | ||
name = subject.find("name").get("value") | ||
single_subject = {} | ||
|
||
single_subject["name"] = name | ||
single_subject["professor"] = subject.find("professor").get("value") | ||
single_subject["info"] = list(map( | ||
lambda x: { | ||
"day": x.get("day"), | ||
"place" : x.get("place"), | ||
"startAt": '{:02d}:{:02d}'.format(*divmod(int(x.get("starttime")) * 5, 60)), | ||
"endAt": '{:02d}:{:02d}'.format(*divmod(int(x.get("endtime")) * 5, 60)) | ||
}, subject.find("time").findall("data") | ||
) | ||
) | ||
result.append(single_subject) | ||
|
||
return result | ||
|
||
def get_calendar(self, timetable, start_date, end_date): | ||
cal = Calendar() | ||
|
||
for item in timetable: | ||
for time in item["info"]: | ||
event = Event() | ||
event.add('summary', item["name"]) | ||
event.add('dtstart', parser.parse("%s %s" % (self.get_nearest_date(start_date, time["day"]), time["startAt"]))) | ||
event.add('dtend', parser.parse("%s %s" % (self.get_nearest_date(start_date, time["day"]), time["endAt"]))) | ||
event.add('rrule', {'freq': 'WEEKLY', 'until': parser.parse(end_date)}) | ||
cal.add_component(event) | ||
|
||
f = open(os.path.join('', 'example.ics'), 'wb') | ||
f.write(cal.to_ical()) | ||
f.close() | ||
|
||
def get_nearest_date(self, start_date, weekday): | ||
start_date = parser.parse(start_date) | ||
weekday = int(weekday) | ||
|
||
if start_date.weekday() >= weekday: | ||
if start_date.weekday() > weekday: start_date += datetime.timedelta(days=7) | ||
start_date -= datetime.timedelta(start_date.weekday() - weekday) | ||
else: | ||
start_date += datetime.timedelta(weekday - start_date.weekday()) | ||
|
||
return start_date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
icalendar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import unittest | ||
|
||
from dateutil import parser | ||
|
||
from convert import Convert | ||
|
||
|
||
class TestConvert(unittest.TestCase): | ||
def test_get_subjects(self): | ||
c = Convert("table.xml") | ||
self.assertGreater(len(c.get_subjects()), 0) | ||
|
||
def test_get_calendar(self): | ||
c = Convert("table.xml") | ||
c.get_calendar(c.get_subjects(), "20180301", "20181231") | ||
|
||
def test_get_nearest_date(self): | ||
c = Convert("table.xml") | ||
self.assertEqual(c.get_nearest_date(start_date="20180301", weekday=4), parser.parse("20180302")) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |