forked from sookcha/every2cal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
80 lines (64 loc) ยท 2.76 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf8 -*-
__author__ = "Hoseong Son <me@sookcha.com>"
import datetime
import os
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 = []
try:
tree = ElementTree.parse(self.filename)
root = tree.getroot()
except:
tree = ElementTree.fromstring(self.filename)
root = tree
for subject in root.iter('subject'):
name = subject.find("name").get("value")
single_subject = {"name": name, "professor": subject.find("professor").get("value"), "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, id):
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)})
if time["place"] != "":
event.add('location', time["place"])
cal.add_component(event)
if len(str(cal.to_ical())) <= 39:
return None
else:
f = open(os.path.join('/', 'tmp', f'{id}.ics'), 'w+')
f.write("BEGIN:VCALENDAR\nVERSION:2.0")
f.close()
f = open(os.path.join('/', 'tmp', f'{id}.ics'), 'ab')
f.write(cal.to_ical()[15:])
f.close()
print("์์
์๋ฃ!๐")
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