-
Notifications
You must be signed in to change notification settings - Fork 3
/
bruinClassAlert.py
102 lines (90 loc) · 4.11 KB
/
bruinClassAlert.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
#-----------------------------------------------------------------------------
# Name: bruinClassAlert.py
# Purpose: alerts students for open schedules
#
# Author: <Samping Chuang>
# Email : [email protected]
#
# Created: 2013/01/18
# Copyright: (c) 2013
# Licence:
#-----------------------------------------------------------------------------
import sys
import getBruinSchedule
import json
import smtplib
import time
from email.mime.text import MIMEText
from time import gmtime, strftime
#opening config file
config = {}
execfile("setting.py", config)
def sendAlert(text):
if config['USE_EMAIL']:
sendMsg(config['EMAIL'], text)
if config['USE_PHONE']:
sendMsg(config['PHONE'] + config[config['CARRIER']], text)
#sending an email message
def sendMsg(to, text):
# Send the message via our own SMTP server, but don't include the
# envelope headers
gmail_user = config['GMAIL_USER']
gmail_pwd = config['GMAIL_PWD']
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_pwd)
print "sending alert..."
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n'
msg = header + '\n This is Bruin Class Alert \n\n' + text
print msg
#send mail!
print "\n`"
smtpserver.sendmail(gmail_user, to, msg)
smtpserver.close()
if __name__ == '__main__':
while config['CLASSES_ALERT']:
print ("checking...")
for C_ALERTS in config['CLASSES_ALERT']:
try:
c_info = getBruinSchedule.get_class_info(C_ALERTS['term'], \
C_ALERTS['major'], \
C_ALERTS['course'])
except:
print("Error reading the schedule: ")
print "Check again in ... ", config['CHECK_EVERY_SEC'], "seconds"
time.sleep(config['CHECK_EVERY_SEC'])
continue
#check if it's the lecture number we want to alert
for lec in c_info["lectures"]:
if int(lec["sec"]) == C_ALERTS['lec_num']:
#parse based on lecture enrollment
if C_ALERTS['sec_num'] == "all":
#section is open
if int(lec["enroll_total"]) < int(lec["enroll_cap"]):
msg = strftime("%Y-%m-%d %H:%M:%S", gmtime()) + "\n"\
" Class: " +C_ALERTS['major']+' '+C_ALERTS["course"]+ \
" Lecture: " + str(C_ALERTS['lec_num']) + " Section: " + C_ALERTS['sec_num']+\
" is open! \n Status: " + str(lec["enroll_total"]) + " / " + str(lec["enroll_cap"])
sendAlert(msg)
config['CLASSES_ALERT'].remove(C_ALERTS)
else:
print "not open"
#parse based on section enrollment
else:
#check if it's the section number we want to alert
for sec in lec["class_sections"]:
if sec["sec"] == C_ALERTS['sec_num']:
#section is open
if int(sec["enroll_total"]) < int(sec["enroll_cap"]):
msg = strftime("%Y-%m-%d %H:%M:%S", gmtime()) + \
" Class: " +C_ALERTS['major']+' '+C_ALERTS["course"]+ \
" Lecture: " + str(C_ALERTS['lec_num']) + " Section: " + C_ALERTS['sec_num']+\
" is open! \n Status: " + str(sec["enroll_total"]) + " / " + str(sec["enroll_cap"])
sendAlert(msg)
config['CLASSES_ALERT'].remove(C_ALERTS)
else:
print "not open"
#check every 15 seconds
time.sleep(config['CHECK_EVERY_SEC'])