Created
February 17, 2009 09:21
-
-
Save blakesmith/65658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib | |
import urllib2 | |
import getpass | |
import sys | |
import datetime | |
import time | |
class BlockReserve(object): | |
valid_machines = {'mac01': 71, 'mac02': 46, 'mac03': 75, 'mac04': 76, 'mac05': 48, 'mac06': 77} | |
URL = "https://www.dlc.purdue.edu/computer/component/reserve.cfc?method=AddMaintenance&dateDisplayed=%s&timeDisplayed=%s&dlcComputerId=%s&startDate={ts%%20%%272009-02-15%%2000:00:00%%27}&dlcComputerOs=mac&time=" | |
LOGIN_URL = 'https://www.dlc.purdue.edu/login/action.cfc?method=LoginProcess' | |
LOCATION = '/computer/manage' | |
MINUTES_DELTA = 30 #30 minute blocks | |
def __init__(self): | |
self.start_date = None | |
self.end_date = None | |
self.login_data = None | |
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) | |
urllib2.install_opener(self.opener) | |
def get_login(self): | |
self.username = raw_input("Enter your career account username: ") | |
self.password = getpass.getpass("Enter your career account password: ") | |
def do_login(self): | |
self.params = urllib.urlencode({'Username': self.username, 'Password': self.password, 'location': self.LOCATION}) | |
f = self.opener.open(self.LOGIN_URL, self.params) | |
if f: | |
self.login_data = f.read() | |
f.close() | |
else: | |
print("Request timed out while trying to reach the server") | |
def is_authenticated(self): | |
if not self.login_data: | |
return False | |
if self.login_data[129:159] == "ITaP: DLC Computer Reservation": | |
return True | |
else: | |
return False | |
def get_machine(self): | |
print("Please select what machine you'd like to block off:") | |
for i in self.valid_machines.iterkeys(): | |
print("%s" % i) | |
selected_machine = raw_input("Enter: ") | |
if selected_machine not in self.valid_machines: | |
print("Not a valid machine name, please try again.") | |
self.get_machine() | |
else: | |
self.selected_machine = self.valid_machines[selected_machine] | |
def get_ranges(self): | |
def sanitize_date(in_date, char, length, in_pos): | |
if in_date == None: | |
return False | |
in_date = in_date.rsplit(char) | |
if len(in_date) == length: | |
for i, j in enumerate(in_date): | |
try: | |
in_date[i] = int(j) | |
except: | |
raise TypeError("Something went wrong with the data you entered, please try again") | |
if in_pos == "start": | |
if char == "/": | |
self.start_date = datetime.datetime(year=in_date[2], month=in_date[0], day=in_date[1]) | |
return True | |
elif char == ":": | |
self.start_date = self.start_date.replace(hour=in_date[0], minute=in_date[1]) | |
return True | |
elif in_pos == "end": | |
if char == "/": | |
self.end_date = datetime.datetime(year=in_date[2], month=in_date[0], day=in_date[1]) | |
return True | |
elif char == ":": | |
self.end_date = self.end_date.replace(hour=in_date[0], minute=in_date[1]) | |
return True | |
else: | |
print("You didn't enter the date using the proper format!") | |
return False | |
start_date = None | |
start_time = None | |
end_date = None | |
end_time = None | |
while not sanitize_date(start_date, "/", 3, "start"): | |
start_date = raw_input("Please enter the starting date in the form of M/DD/YYYY (EG: 2/19/2009): ") | |
while not sanitize_date(start_time, ":", 2, "start"): | |
start_time = raw_input("Please enter the starting time in the form of HH:MM 24 hour time. (EG: 17:00): ") | |
while not sanitize_date(end_date, "/", 3, "end"): | |
end_date = raw_input("Please enter the ending date in the form of M/DD/YYYY (EG: 2/19/2009): ") | |
while not sanitize_date(end_time, ":", 2, "end"): | |
end_time = raw_input("Please enter the ending time in the form of HH:MM 24 hour time.(EG: 17:00): ") | |
def do_block(self): | |
def get_url(url): | |
try: | |
f = self.opener.open(url) | |
f.close() | |
if f: | |
print("%s successfully blocked." % i) | |
time.sleep(1) | |
except: | |
print("Failed. Retrying again...") | |
get_url(url) | |
delta_min = datetime.timedelta(0, 60*self.MINUTES_DELTA) | |
i = self.end_date | |
while i >= self.start_date: | |
time_param = i.strftime("%I:%M:00%%20%p") | |
if time_param[0] == '0': | |
time_param = time_param[1:] | |
date_param = i.strftime("%m/%d/%Y") | |
if date_param[0] == '0': | |
date_param = date_param[1:] | |
build_url = self.URL % (date_param, time_param, self.selected_machine) | |
get_url(build_url) | |
i = i - delta_min | |
if __name__ == "__main__": | |
block_reserve = BlockReserve() | |
print("DLC Equipment range block") | |
block_reserve.get_login() | |
block_reserve.do_login() | |
if block_reserve.is_authenticated(): | |
print("Login successfull!\n") | |
block_reserve.get_machine() | |
print("Machine selected. Retrieve date ranges") | |
block_reserve.get_ranges() | |
print("Begin blocking ranges now...") | |
block_reserve.do_block() | |
print("Done!") | |
else: | |
print("Login failed\n") | |
sys.exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment