-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckCourse.py
executable file
·112 lines (85 loc) · 3.09 KB
/
checkCourse.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
#!/usr/bin/python3
import sys
import os
import json
import yaml
import requests
from lxml import html
from twilio.rest import Client
PAGE_URL = 'http://www.adm.uwaterloo.ca/cgi-bin/cgiwrap/infocour/salook.pl'
API_URL = 'https://api.uwaterloo.ca/v2/terms/'
API_URL_2 = 'https://api.uwaterloo.ca/v2/courses/'
def check_api_terms(sess, subject, cournum):
resp = requests.get(API_URL + sess + '/' + subject + '/enrollment.json?key=' + API_KEY)
data = resp.json()['data']
sections = [course for course in data if course['catalog_number'] == cournum and course['section'][:3] == 'LEC']
isRoom = False
for course in sections:
print(str(course['enrollment_total']) + ' / ' + str(course['enrollment_capacity']))
if (course['enrollment_total'] < course['enrollment_capacity']):
isRoom = True
return isRoom
def scrape_webpage(sess, subject, cournum):
isRoom = False
payload = {
'level': 'under',
'sess': sess,
'subject': subject,
'cournum': cournum
}
page = requests.post(PAGE_URL, data=payload)
tree = html.fromstring(page.content)
secType = tree.xpath('//table/tr[3]/td[2]/table/tr/td[2]/text()')
cap = tree.xpath('//table/tr[3]/td[2]/table/tr/td[7]/text()')
enrolled = tree.xpath('//table/tr[3]/td[2]/table/tr/td[8]/text()')
print(secTypei)
print(cap)
print(enrolled)
for i in range(len(secType)):
if (secType[i][:3] == 'LEC'):
if (int(cap[i]) - int(enrolled[i]) > 0):
isRoom = True
return isRoom
def check_api_courses(sess, subject, cournum):
isRoom = False
payload = {
'term': sess,
'key': API_KEY
}
resp = requests.get(API_URL_2 + subject + '/' + cournum + '/schedule.json', params=payload)
data = resp.json()['data']
sections = [section for section in data if section['section'][:3] == 'LEC']
for section in sections:
spaces = section['enrollment_capacity'] - section['enrollment_total']
if (spaces <= 0):
continue
reserves = section['reserves']
reservedSpaces = 0
for group in reserves:
groupSpaces = group['enrollment_capacity'] - group['enrollment_total']
if (groupSpaces > 0):
reservedSpaces += groupSpaces
if (spaces > reservedSpaces):
isRoom = True
break
if isRoom:
send_sms(subject, cournum)
return isRoom
def send_sms(subject, cournum):
client = Client(creds['client']['accountSID'], creds['client']['authToken'])
client.messages.create(
to=creds['message']['recipient'],
from_=creds['message']['sender'],
body="Space available in " + subject + " " + cournum + "!"
)
if __name__ == '__main__':
try:
with open(os.path.expanduser('~/.config/armadillo/credentials.yml')) as cf:
creds = yaml.load(cf)
except IOError:
sys.exit("You must provide a config file")
API_KEY = creds['api_key']
sess = sys.argv[1]
subject = sys.argv[2]
cournum = sys.argv[3]
check_api_courses(sess, subject, cournum)