-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_to_ics.py
79 lines (60 loc) · 2.7 KB
/
html_to_ics.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
#!/usr/bin/python
import datetime
import copy
# Global defaults
outputFileName = 'toCalendar.csi'
inputFileName = 'fromExcel.html'
lengthOfALectureHour = 50 # min
def printHowtoUse():
print("This will convert fromExcel.html file to toCalendar.csi file!")
def writeHeader():
f = open(outputFileName, 'w')
f.write('BEGIN:VCALENDAR\nVERSION:2.0\n')
f.close()
printHowtoUse()
startDateStr = raw_input('Start date of the courses(YYYY-MM-DD)\t: ') #'2019-02-10' #
endDateStr = raw_input('End date of the courses(YYYY-MM-DD)\t: ') #'2019-05-31' #
timeZone = raw_input('Time Zone(+4,-2)\t\t\t: ')
t = startDateStr.split('-')
f = endDateStr.split('-')
startDate = datetime.datetime(int(t[0]), int(t[1]), int(t[2]), 0, 0, 0) # Start date of the event, corresponding to the first(0th) column of the excel file
endDate = datetime.datetime(int(f[0]), int(f[1]), int(f[2]), 0, 0, 0) # End date of the event, when the final class hour will be held
currentDate = copy.copy(startDate)
if timeZone[0] == '-':
currentDate = currentDate + datetime.timedelta(seconds=3600*int(timeZone[1:]))
elif timeZone[0] == '+':
currentDate = currentDate - datetime.timedelta(seconds=3600*int(timeZone[1:]))
else:
print 'Time zone parse error!'
exit()
writeHeader()
inFile = open(inputFileName, 'r')
outFile = open(outputFileName, 'a')
inLines = inFile.readlines()
inLine = inLines[20]
hours = '' # the hours used to insert courses, the leftmost column
while currentDate < endDate:
# Here read according to the correct html tags, but time to sleep!! may be tomorrow...
for y, line in enumerate(inLine.split('<tr')[2:]): # if .split is called every time the loop exectures ??
for x, column in enumerate(line.split('<td')[1:]):
if column.find('<p>') == -1:
# Empty slot, continue
continue
data = column.split('<p>')[1].split('</p>')[0]
if(x == 0):
# Then it is hours column
hours = data
continue
outFile.write('BEGIN:VEVENT\n')
currentTime = currentDate + datetime.timedelta(days=x-1) + datetime.timedelta(seconds=60*int(hours.split('.')[1]) + 3600*int(hours.split('.')[0]))
outFile.write('DTSTART:' + currentTime.strftime('%Y%m%d') + 'T' + currentTime.strftime('%H%M') + '00Z\n')
currentTime = currentTime + datetime.timedelta(seconds=60*lengthOfALectureHour)
outFile.write('DTEND:' + currentTime.strftime('%Y%m%d') + 'T' + currentTime.strftime('%H%M') + '00Z\n')
outFile.write('SEQUENCE:0\n')
outFile.write('STATUS:CONFIRMED\n')
outFile.write('SUMMARY:' + data + '\n')
outFile.write('END:VEVENT\n')
currentDate = currentDate + datetime.timedelta(days=7)
outFile.write('END:VCALENDAR\n')
inFile.close()
outFile.close()