Skip to content

Instantly share code, notes, and snippets.

@brianjp93
Created March 2, 2014 12:50
Show Gist options
  • Save brianjp93/9306109 to your computer and use it in GitHub Desktop.
Save brianjp93/9306109 to your computer and use it in GitHub Desktop.
"""
class_check.py
Code written by Brian Perrett on February 11th, 2014
Use - To check available slots in a desired class in the class schedule,
available at classes.uoregon.edu
How it works
If a class' available slots==0, you may decide to use this code. It will
check every 5 minutes if the available slots==0. If it isn't,
it will send you a text message.
"""
############################################## importing stuff
import urllib.request
from bs4 import *
import time
import smtplib
import sys
##############################################
def class_check():
"""
arguments:
None, but takes user input for several variables, gmail, password, phone number, class, url...
returns:
Nothing, but texts and/or emails you when a class of your choosing is available.
"""
double_check = 0
while double_check == 0:
#CREATES A DICTIONARY OF CLASSES AND URL'S. CREATES A LIST OF CLASSES.
#############################################
class_and_url = {} #initialize class and url dictionary
loops = input("How many classes will you be checking? - ")
loops = int(loops) #make sure this is an int, not a str
classes = [] #initialize class list so we can reference it in the for loop below
for i in range(loops):
class_1 = input('What is the name of the class you want to check? - ')
classes.append(class_1)
url_1 = input('What is the direct url for ' + classes[i] + '? - ')
class_and_url[class_1] = url_1
##############################################
##############################################
# USER INPUT STAGE
gmail = input("What is your gmail account? (Include @gmail.com) - ")
username = gmail[0:-10]
password = input("What is your password for " + gmail + "? - ")
provider_dic = {'v' : '@vtext.com', 's' : '@messaging.sprintpcs.com', 't' : '@tmomail.net', 'a' : '@txt.att.net'}
do_text = True #set boolean
toaddr = input("What is your phone number? (10-digit, no dashes. Do not include country code) *If you do not want to recieve a text notification, just hit enter. - ")
if toaddr == '':
do_text = False
if do_text == True:
provider = input('Who is your phone provider? (v=verizon, a=att, t=t-mobile, s=sprint) - ')
provider = provider_dic[provider]
toaddr = toaddr+provider
print ("Is this Correct? Yes = 'y', No = 'n'")
time.sleep(.5)
print ("-You want to check "+ str(classes))
time.sleep(.5)
print ("-gmail = "+ gmail)
time.sleep(.5)
print ("-password = " +password)
time.sleep(.5)
if do_text:
if toaddr != '':
print ("-cell number = "+ toaddr[0:10])
time.sleep(.5)
ans = input("Yes or No (y,n) - ")
if ans == 'y' or ans == 'Y':
double_check = 1
################################################
#####################################################################################
#WORKING PHASE
blah_count = 0
for blah in range(500):
time_left = ((500-blah_count)*5)/60 #an approximate time calculation
print (str(500 - blah_count) + (' more cycles. About '+str(time_left)+' hours'))
blah_count +=1
if len(classes)<=0:
sys.exit("All classes found!")
for n in range(len(classes)): #for each class, do this stuff
to_remove = []
##############################################
response = urllib.request.urlopen(class_and_url[classes[n]])
html = BeautifulSoup(response.read())
avail = [] #initialize list
counter = 0 #initialize counter to print every other 'td'
##############################################
##############################################
#scraping website for data. Using conditions td.get() to find the open class slots within site
for td in html.findAll('td'): #go through html and find all 'td' tags
if td.get('width') == '30' and td.get('rowspan') == '1': #conditions. Width==30, rowspan==1
if counter % 2 == 0: #only append list if 'td' is displaying available slots (not max slots)
open_spots = td.text
avail.append(open_spots)
counter += 1 #self explanatory
###############################################
#TESTING BLOCK
#print(avail)
#
###############################################
if avail[0] != '0': #change index to the number of the class you are interested in is in the list. If this returns True, a text message will be sent.
if do_text == False:
print ('Sending Email')
m = ("There are/is " + str(open_spots) + " spot(s) available in " + classes[n])
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
if do_text == True:
print ("Sending text and Email")
server.sendmail(gmail, toaddr, m)
server.sendmail(gmail, gmail, m)
server.quit()
to_remove.append(classes[n])
loops -= 1 #lessen loop
if loops <= 0:
sys.exit("Yay! All classes found!")
if len(to_remove) != 0:
for num in to_remove:
del class_and_url[classes[n]] #del found class from dictionary
classes.remove(classes[n]) #delete found class from class list
time.sleep(300) #wait 5 minutes for next loop
##########################################################################################
class_check()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment