Repository navigation
Expand file tree
/
Copy pathtrackClassAvailability.py
More file actions
executable file
·265 lines (233 loc) · 11.4 KB
/
Copy pathtrackClassAvailability.py
File metadata and controls
executable file
·265 lines (233 loc) · 11.4 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import urllib
from bs4 import BeautifulSoup
import smtplib
import sys
import getpass
#import schedule
#This loads the class information from the text file. If it can't open the ClassesToTrack.txt,
#it will then ask for information and create it. The program will then quit. The user has to re-run
#the program, then it will read in the appropriate information and create a list of lists
#(a list of all the classes and then in each sub-list is all the class information).
def load_class_information():
# Determines if there was a failure in opening the file
try:
classes = open('ClassesToTrack.txt', 'r')
except:
print('ClassesToTrack.txt not found or hasn\'t been created.')
url = str(raw_input('Please enter the URL to the direct section you want to track on WebTMS: '))
while 'webtms' not in url:
url = str(raw_input('Please enter the URL to the direct section you want to track on WebTMS: '))
opensOrCloses = str(raw_input('Would you like to be notified when a section Opens or Closes (Enter opens or closes): '))
while not (opensOrCloses.lower() == 'opens' or opensOrCloses.lower() == 'closes'):
opensOrCloses = str(raw_input('Would you like to be notified when a section Opens or Closes (Enter opens or closes): '))
email = str(raw_input('Enter an @drexel.edu or @gmail.com email address: '))
while not ('@drexel.edu' in email or '@gmail.com' in email):
email = str(raw_input('Enter an @drexel.edu or @gmail.com email address: '))
password = getpass.getpass(prompt='Enter the password for {0}: '.format(email))
stringToWrite = url + ',' + opensOrCloses + ',' + email + ',' + password
if not url and not opensOrCloses:
print('Please provide valid input or re-run the script')
classes = open('ClassesToTrack.txt', 'w')
classes.write(stringToWrite)
classes.close()
# We know the file had just been created, so open it instead of forcing the user to re-run
classes = open('ClassesToTrack.txt', 'r')
if not classes:
print "There are no classes to track. Please add classes then re-run the script"
sys.exit(0)
classesToTrack = []
for l in classes:
temp = []
splitLine = l.split(',')
try:
url = splitLine[0]
avail = splitLine[1]
email = splitLine[2]
password = splitLine[3]
except:
print 'Error getting necessary data from ClassesToTrack.txt.'
print 'Make sure all lines are in the correct format: url,closes,email,password or url,opens,email,password'
sys.exit(1)
temp.append(url)
temp.append(avail)
temp.append(email)
temp.append(password)
classesToTrack.append(temp)
classes.close()
return classesToTrack
def stop_tracking_classes(classesToStopTracking):
classesToTrack = open('ClassesToTrack.txt', 'r')
trackedClasses = classesToTrack.readlines()
classesToTrack.close()
classesToTrack = open('ClassesToTrack.txt', 'w')
newTrackedList = []
for c in trackedClasses:
newTrackedList.append(c)
for i in range(0, len(trackedClasses)):
for j in range(0, len(classesToStopTracking)):
if trackedClasses[i] == classesToStopTracking[j]:
newTrackedList.remove(classesToStopTracking[j])
elif trackedClasses[i] == classesToStopTracking[j].strip('\n'):
newTrackedList.remove(classesToStopTracking[j].strip('\n'))
for i in range(0, len(newTrackedList)):
if i == len(newTrackedList) - 1:
c = newTrackedList[i].strip('\n')
classesToTrack.write(c)
else:
if '\n' in newTrackedList[i]:
classesToTrack.write(newTrackedList[i])
else:
classesToTrack.write(newTrackedList[i] + '\n')
#This main scrapes the html for all the information you could ever dream of having about the class.
#More can be added, although I don't think there is anymore information. This then checks the status
#of the class and will then send an email with all the information it just gathered. The email will
#tell the user if the class is opened or closed.
def main():
classes = load_class_information()
classesToStopTracking = []
for i in range(0, len(classes)):
url = classes[i][0]
closeOrOpen = classes[i][1]
email = classes[i][2]
password = classes[i][3]
try:
urlData = urllib.urlopen(url)
except:
print('Couldn\'t connect to URL.')
data = str(urlData.readlines())
bs = BeautifulSoup(data)
# These variables are still in scope even though they are set in the
# for loop
for t in bs.findAll('td', attrs={'class': 'tableHeader'}):
if t.text == "CRN":
crn = t.findNext('td').text
if t.text == "Subject Code":
subj = t.findNext('td').text
if t.text == "Course Number":
courseNum = t.findNext('td').text
if t.text == "Section":
section = t.findNext('td').text
if t.text == "Credits":
creds = t.findNext('td').text
if t.text == "Title":
title = t.findNext('td').text
if t.text == "Instructor(s)":
instructor = t.findNext('td').text
if t.text == "Max Enroll":
maxEnrollNum = t.findNext('td').text
if t.text == "Enroll":
curEnrollNum = t.findNext('td').text
if t.text == "Section Comments":
sectionComments = t.findNext('td').findNext('table').findNext('tr').findNext('td').text
urlData.close()
if '@gmail.com' in email.lower():
if (curEnrollNum == 'CLOSED' and closeOrOpen == 'closes'):
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
password = password.strip('\n\t')
try:
server.login(email, password)
except:
print 'Invalid login information, please check the password and email for {0}.'.format(email)
break
subject = subj + ' ' + courseNum + ' ' + 'Section: ' + section + ' closed!'
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = email
msg['To'] = email
htmlList = htmlFile.readlines()
htmlUnfilledTable = "\n".join(htmlList)
html = htmlUnfilledTable.format(crn, subj, courseNum, section, creds, title, instructor, maxEnrollNum, curEnrollNum, sectionComments)
htmlTable = MIMEText(html, 'html')
msg.attach(htmlTable)
server.sendmail(email, email, msg.as_string())
server.close()
classesToStopTracking.append(url + ',' + closeOrOpen + ',' + email + ',' + password + '\n')
if (curEnrollNum != 'CLOSED' and closeOrOpen == 'opens'):
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
password = password.strip('\n\t')
try:
server.login(email, password)
except:
print 'Invalid login information, please check the password and email for {0}.'.format(email)
break
subject = subj + ' ' + courseNum + ' ' + 'Section: ' + section + ' opened!'
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = email
msg['To'] = email
htmlList = htmlFile.readlines()
htmlUnfilledTable = "\n".join(htmlList)
html = htmlUnfilledTable.format(crn, subj, courseNum, section, creds, title, instructor, maxEnrollNum, curEnrollNum, sectionComments)
htmlTable = MIMEText(html, 'html')
msg.attach(htmlTable)
server.sendmail(email, email, msg.as_string())
server.close()
classesToStopTracking.append(url + ',' + closeOrOpen + ',' + email + ',' + password + '\n')
if '@drexel.edu' in email.lower():
if (curEnrollNum == 'CLOSED' and closeOrOpen == 'closes'):
server = smtplib.SMTP('smtp.mail.drexel.edu:587')
server.ehlo()
server.starttls()
password = password.strip('\n\t')
try:
server.login(email, password)
except:
print 'Invalid login information, please check the password and email for {0}.'.format(email)
break
subject = subj + ' ' + courseNum + ' ' + 'Section: ' + section + ' closed!'
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = email
msg['To'] = email
try:
htmlFile = open('email-table.html', 'r')
except:
print 'Can\'t find email-table.html'
sys.exit(1)
htmlList = htmlFile.readlines()
htmlUnfilledTable = "\n".join(htmlList)
html = htmlUnfilledTable.format(crn, subj, courseNum, section, creds, title, instructor, maxEnrollNum, curEnrollNum, sectionComments)
htmlTable = MIMEText(html, 'html')
msg.attach(htmlTable)
server.sendmail(email, email, msg.as_string())
server.close()
classesToStopTracking.append(url + ',' + closeOrOpen + ',' + email + ',' + password + '\n')
if (curEnrollNum != 'CLOSED' and closeOrOpen == 'opens'):
server = smtplib.SMTP('smtp.mail.drexel.edu:587')
server.ehlo()
server.starttls()
password = password.strip('\n\t')
try:
server.login(email, password)
except:
print 'Invalid login information, please check the password and email for {0}.'.format(email)
break
subject = subj + ' ' + courseNum + ' ' + 'Section: ' + section + ' opened!'
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = email
msg['To'] = email
htmlList = htmlFile.readlines()
htmlUnfilledTable = "\n".join(htmlList)
html = htmlUnfilledTable.format(crn, subj, courseNum, section, creds, title, instructor, maxEnrollNum, curEnrollNum, sectionComments)
htmlTable = MIMEText(html, 'html')
msg.attach(htmlTable)
server.sendmail(email, email, msg.as_string())
server.close()
classesToStopTracking.append(url + ',' + closeOrOpen + ',' + email + ',' + password + '\n')
stop_tracking_classes(classesToStopTracking)
if __name__ == "__main__":
print __name__
main()
#The main could be run on a cron job (Linux and Mac OS X), on a Windows built in timer or on a
#in program timer like so:
#schedule.every(1).hour.do(main)
#while True:
# schedule.run_pending()
# time.sleep(1)