-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar_importer.py
121 lines (96 loc) · 4.35 KB
/
calendar_importer.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import mechanize
import requests
import time
import datetime
import json
import getpass
from bs4 import BeautifulSoup
class CalendarImporter:
def getAuthInfo(self):
self.__STUDENT_NUMBER = raw_input("What is your student number? ")
self.__STUDENT_MY_TCD_PASSWORD = getpass.getpass("What is your my.tcd.ie password?")
self.__GOOGLE_EMAIL = raw_input("What is your Google email or username? ")
self.__GOOGLE_PASSWORD = getpass.getpass("What is your Google account password? (If using 2FA, use app specific password.)")
print "Wait like 30-40s"
self.__MY_TCD_LOGIN_URI = "https://my.tcd.ie/urd/sits.urd/run/siw_lgn"
self.__GOOGLE_LOGIN_ENDPOINT = "https://www.google.com/accounts/ClientLogin"
self.__GOOGLE_CALENDAR_EVENT_ENDPOINT = "https://www.googleapis.com/calendar/v3/calendars/primary/events"
self.__getMyTcdCalendar()
def __getMyTcdCalendar(self):
self.appointments = []
br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_refresh(False)
br.set_handle_redirect(True)
br.open(self.__MY_TCD_LOGIN_URI)
br.select_form("f")
username = br.form.find_control("MUA_CODE.DUMMY.MENSYS.1")
password = br.form.find_control("PASSWORD.DUMMY.MENSYS.1")
username.value = self.__STUDENT_NUMBER
password.value = self.__STUDENT_MY_TCD_PASSWORD
br.submit()
br.open(br.click_link(text="here"))
br.open(br.click_link(text="My Timetable"))
br.open(br.click_link(text="View My Own Student Timetable"))
soup = BeautifulSoup(br.response().read())
appointment_soup = soup.find_all("td", "fullBorder")
for appointment in appointment_soup:
appointment_text = appointment.get_text()
tooltip_text = appointment['onmouseover']
appointment = {
"size": int(self.__parseAppointmentText(appointment_text, "Size:")),
"group": self.__parseAppointmentText(appointment_text, "Group:"),
"activity": self.__parseAppointmentText(appointment_text, "Activity:"),
"room": self.__parseAppointmentText(appointment_text, "Room:", index=1),
"module": self.__parseAppointmentText(tooltip_text, "Module:", "<br/>"),
"day": self.__parseAppointmentText(tooltip_text, "Date:", "<br/>"),
"time": self.__parseAppointmentText(tooltip_text, "Time:", "<br/>")
}
self.appointments.append(appointment)
self.__createAppointmentsOnGoogleCal()
def __createAppointmentsOnGoogleCal(self):
self.__getGoogleAuthToken()
for appointment in self.appointments:
start_and_end_times = appointment["time"].split("-")
start_time = self.__createTimeDelimiter(start_and_end_times[0], appointment["day"])
end_time = self.__createTimeDelimiter(start_and_end_times[1], appointment["day"])
event = {
"summary": appointment["module"] + " " + appointment["activity"],
"location": appointment["room"],
"description": "Activity: "+appointment["activity"]+", "+str(appointment["size"])+" Students"+" "+appointment["group"],
"start": {
"dateTime": start_time.isoformat(),
"timeZone": "Europe/Dublin"
},
"end": {
"dateTime": end_time.isoformat(),
"timeZone": "Europe/Dublin"
},
"recurrence": [
"RRULE:FREQ=WEEKLY;UNTIL=20150403T160000Z"
]
}
request_headers = {
"Authorization": "GoogleLogin auth="+self.__GOOGLE_AUTH_TOKEN,
"Content-Type": "application/json",
"Accept": "application/json"
}
request_params = {
"key": "AIzaSyDLt2lWCNKnSyJNz3qB4OJjT0QwB4ExG50"
}
requests.post(self.__GOOGLE_CALENDAR_EVENT_ENDPOINT, data=json.dumps(event), headers=request_headers, params=request_params)
print "kthxbomb"
def __getGoogleAuthToken(self):
req = requests.post(self.__GOOGLE_LOGIN_ENDPOINT, {
"accountType": "HOSTED_OR_GOOGLE",
"Email": self.__GOOGLE_EMAIL,
"Passwd": self.__GOOGLE_PASSWORD,
"service": "cl"
})
self.__GOOGLE_AUTH_TOKEN = req.text.split("Auth=")[1].replace("\n", "")
def __parseAppointmentText(self, text, key, needle="\n", index=0):
return text.split(key)[1].split(needle)[index].strip() # (this should be less ew)
def __createTimeDelimiter(self, time, day):
return datetime.datetime.strptime("2015 2 " + day.strip() + " " + time.strip(), '%Y %W %A %H:%M')
importer = CalendarImporter()
importer.getAuthInfo()