-
Notifications
You must be signed in to change notification settings - Fork 0
/
testudo.py
164 lines (133 loc) · 4.28 KB
/
testudo.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
159
160
161
162
163
164
import requests
from bs4 import BeautifulSoup
from time import sleep, strftime, gmtime
from random import randint
#returns the unique semester identifier
def getSemester():
#start a new web scraping session
s = requests.session()
#download the main page of classes
html = s.get("https://ntst.umd.edu/soc")
#parse the html of the class page
options = BeautifulSoup(html.text,"html.parser")
options = options.find("select", {"id":"term-id-input"})
options = str(options).split("</option>")
#find the option with the semester code in it
for option in options:
if '"selected"' in option:
semester = option
#extract the semester code
semester = semester[semester.index('value="')+7:]
semester = semester[:semester.index('"')]
#close the session
s.close()
return semester
#returns a list of sections
def getSections(course):
#start a new web scraping session
s = requests.session()
#begin composing the url
url = "https://ntst.umd.edu/soc/search"
url += "?courseId=" + course
url += "§ionId="
url += "&termId="+getSemester()
url += "&_openSectionsOnly=on"
url += "&creditCompare="
url += "&credits="
url += "&courseLevelFilter=ALL"
url += "&instructor="
url += "&_facetoface=on"
url += "&_blended=on"
url += "&_online=on"
url += "&courseStartCompare="
url += "&courseStartHour="
url += "&courseStartMin="
url += "&courseStartAM="
url += "&courseEndHour="
url += "&courseEndMin="
url += "&courseEndAM="
url += "&teachingCenter=ALL"
url += "&_classDay1=on"
url += "&_classDay2=on"
url += "&_classDay3=on"
url += "&_classDay4=on"
url += "&_classDay5=on"
#download the list of classes
html = s.get(url).text
#parse the html with bs4
courses = BeautifulSoup(html,"html.parser").find_all("div", {"class":"section"})
#make an empty list to contain all sections
sections = []
#loop through every section in the course list
for course in courses:
#declare a blank list to hold section and time info
section = []
times = []
#get the times avaiable
slots = course.find("div", {"class":"class-days-container"})
slots = slots.find_all("div", {"class":"row"})
#loops thorugh and add all time to the list
for slot in slots:
time = slot.find("div", {"class":"section-day-time-group"})
time = " ".join(time.text.strip().split("\n"))
times.append(time)
#get the name of the course
name = str(course.find("div", {"class":"section-action-links-container"}))
name = name[name.index('value="')+7:]
name = name[:name.index('"')]
#append the name of the course to the list
section.append(name)
#get the amount of open seats
openSeatsCount = int(course.find("span", {"class":"open-seats-count"}).text)
#say whether class is open
if openSeatsCount > 0:
section.append("open")
else:
section.append("closed")
#get the section number, and the instructor
section.append(course.find("span", {"class":"section-id"}).text.strip())
section.append(course.find("span", {"class":"section-instructor"}).text)
#add the section information and the times
sections.append(section)
section.append(times)
#close the current session
s.close()
#return all sections
return sections
#returns if a section is open
def isOpen(section):
if section[1] != "open":
return False
else:
return True
#main function, continuously checks for openings
def testudo(course):
#get all sections for the course
sections = getSections(course)
#loop through and list all sections
for index, value in enumerate(sections):
if index < 9:
print("(0"+str(index+1)+") "+str(value))
else:
print("("+str(index+1)+") "+str(value))
#get the section wanted, and check if open
section = int(input("Type the list number for the section wanted: "))-1
output = isOpen(sections[section])
#if section not open, continuously check
while output == False:
output = isOpen(getSections(course)[section])
print("["+strftime("%Y-%m-%d %H:%M:%S", gmtime())+"] (section closed)")
sleep(randint(35, 45))
if output == True:
print("["+strftime("%Y-%m-%d %H:%M:%S", gmtime())+"] (section open)")
'''
Place custom notification code in this area!
'''
#define the command line arguments
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
sys.stderr.write('usage: python3 testudo.py <course>\n')
sys.exit(1)
else:
testudo(sys.argv[1].lower())