-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_funcs.py
158 lines (128 loc) · 5.24 KB
/
gui_funcs.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import httplib2
import os
from bs4 import BeautifulSoup as soup
import re
import pytz
from datetime import datetime as dt
from datetime import timedelta
from datetime import date
from dateutil.relativedelta import relativedelta
from googleapiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'UKY Schedule Webscrape'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'uky-web-scrape-calendar.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def getService():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
return discovery.build('calendar', 'v3', http=http)
def addToCalendar(fileName, calendarId):
output = ""
page_html = open(fileName, 'r').read()
page_soup = soup(page_html, "html.parser")
service = getService()
six_months = date.today() + relativedelta(months=+6)
year = str(six_months.year)
timeZone = "America/New_York"
courses = page_soup.findAll("div", {"class": "table-thin-row small"})
for course in courses:
title = " ".join(course.find("strong", {"class": "text-dark"}).text.split())
section = course.findAll("div")[4].text
courseType = course.findAll("div")[5].text.strip()
p = re.compile(r'Section ')
newSection = p.sub("", section)
summary = title + " - " + newSection
weekdays = course.findAll("div")[7].text
if weekdays == "TBD":
continue
hour = course.findAll("div")[8].text
dates = course.findAll("div")[9].text.strip()
hourList = hour.split(" - ")
datesList = dates.split("-")
googleDateTimeList = []
for i in range(0, len(datesList)):
h = ''.join(hourList[i].split(" "))
new_time = dt.strptime(h, '%I:%M%p').strftime("%H:%M")
new_time += ":00"
unformattedTime = datesList[0] + " " + year + " " + new_time
naive = dt.strptime(unformattedTime, '%b %d %Y %H:%M:%S')
local = pytz.timezone(timeZone)
local_dt = local.localize(naive, is_dst=None)
googleDateTimeList.append(local_dt.strftime("%Y-%m-%d"+"T"+"%H:%M:%S"+"-05:00"))
googleDateTimeStart = googleDateTimeList[0]
googleDateTimeEnd = googleDateTimeList[1]
classEndTime = ''.join(hourList[1].split(" "))
new_time = dt.strptime(classEndTime, '%I:%M%p').strftime("%H:%M")
new_time += ":00"
unformattedTime = datesList[1] + " " + year + " " + new_time
naive = dt.strptime(unformattedTime, '%b %d %Y %H:%M:%S')
naive += timedelta(hours=48) # make two days ahead because recurrence is inclusive of end time
local = pytz.timezone(timeZone)
local_dt = local.localize(naive, is_dst=None)
until = local_dt.strftime("%Y%m%d"+"T"+"%H%M%S"+"Z")
weekdays = list(weekdays)
days = ""
for char in weekdays:
if char == 'M': days += "MO"
elif char == 'T': days += "TU"
elif char == 'W': days += "WE"
elif char == 'R': days += "TH"
else: days += "FR"
days += ","
days = days[:-1] # remove last comma
recurrence = "RRULE:FREQ=WEEKLY;UNTIL={};BYDAY={}".format(until, days)
building = course.findAll("div")[11].text
room = course.findAll("div")[12].text
location = building + " " + room
instuctor = course.findAll("div")[13].text.strip()
description = courseType + "\n" + instuctor
event = {
'summary': summary,
'location': location,
'description': description,
'start': {
'dateTime': googleDateTimeStart,
'timeZone': timeZone,
},
'end': {
'dateTime': googleDateTimeEnd,
'timeZone': timeZone,
},
'recurrence': [
recurrence
]
}
event = service.events().insert(calendarId=calendarId, body=event).execute()
output += 'Event created for {}\n'.format(title)
return output.rstrip("\n")