Browse Source

initial commit

pull/18/head
theycallmemac 3 years ago
parent
commit
e35d05e2a9
5 changed files with 317 additions and 2 deletions
  1. +5
    -2
      README.md
  2. +54
    -0
      book_lab.py
  3. +112
    -0
      book_room.py
  4. +35
    -0
      booking.sh
  5. +111
    -0
      check_availability.py

+ 5
- 2
README.md View File

@@ -1,2 +1,5 @@
# dcurooms
A command line tool used to display room information and book and request rooms.
# EventsOfficerScripts

The role of Redbrick Events Officer isn't even remotely considered a technical position. It's pretty administrative, but I've written a few scripts that help me get around the filling out of forms, sending emails and booking of rooms.

###

+ 54
- 0
book_lab.py View File

@@ -0,0 +1,54 @@
import smtplib
import sys
import random


def draft_email(user, password, recipient, subject, body):
gmail_user = user
gmail_password = password
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
BODY = body

message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, BODY)

return gmail_user, gmail_password, FROM, TO, message


def send_email(gmail_user, gmail_password, FROM, TO, message):
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_password)
server.sendmail(FROM, TO, message)
server.close()
print("Your email has been sent.")
except:
print("Email failed to send.")


def main():

details = raw_input("\nWhat days, times and rooms are you booking?: ")
template1 = "Hi Irene,\n\nJust wondering if you could book " + details + " on behalf of Redbrick.\n\nThanking you,\nJames McDermott.\nRedbrick Events Officer."
template2 = "Hello again,\n\nCould you please book " + details + " for Redbrick.\n\nThanks,\nJames McDermott.\nRedbrick Events Officer."
template3 = "Just wondering if you could book " + details + " for Redbrick.\n\nThank you,\nJames McDermott.\nRedbrick Events Officer."

templates = [template1,template2,template3]
body = random.choice(templates)

draft = draft_email("my-email","password","irene.mcevoy@dcu.ie","Lab Booking", body)
print("\n" + " ".join(draft[4:]))

confirmation = raw_input("\nIs this the correct input? (y/n): ")

if confirmation.lower() == "y":
sent = send_email(draft[0],draft[1],draft[2],draft[3],draft[4])
else:
print("Email cancelled and not sent.")

if __name__ == '__main__':
main()

+ 112
- 0
book_room.py View File

@@ -0,0 +1,112 @@
import sys
import cookielib
import mechanize


def enter_data():
date = raw_input("Enter date (1 - 31): ")
if int(date) in range(1,32):
pass
else:
print("Date not in range (1 - 31)")
sys.exit()

month = raw_input("Enter month (1 - 12): ")
if int(month) in range(1,13):
pass
else:
print("Month not in range (1 - 12)")
sys.exit()

year = raw_input("Enter year (2017 - 2019): ")
if int(year) in range(2012,2020):
pass
else:
print("Year not in range (2012 - 2019)")
sys.exit()

details = fill_form(date, month, year)

return details


def fill_form(date, month, year):
browser = mechanize.Browser()
cookie_jar = cookielib.LWPCookieJar()
browser.set_cookiejar(cookie_jar)

browser.set_handle_equiv(True)
browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)
browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
browser.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")]

browser.open("http://www.dcu.ie/registry/booking.shtml")
browser.select_form(nr=2)

browser.form["submitted[name_of_club_society]"] = "Society"
browser.form["submitted[name_of_person_making_booking]"] = "Name"
browser.form["submitted[contact_telephone_number]"] = "Number"
browser.form["submitted[date_room_required][day]"] = [date,]
browser.form["submitted[date_room_required][month]"] = [month,]
browser.form["submitted[date_room_required][year]"] = [year,]
browser.form["submitted[room_capacity]"] = "Capacity"
browser.form["submitted[description_of_event]"] = "Description"
browser.form["submitted[hours_requiredfrom_to]"] = "Start - End"
browser.form["submitted[building_room_reference]"] = "Room-Reference-Number"
browser.form["submitted[email_address]"] = "Email"

return browser


def format_form(lst):
print("\n<Browser visiting http://www.dcu.ie/registry/booking.shtml")
print("<post http://www.dcu.ie/registry/booking.shtml application/x-www-form-urlencoded")

i = 3
while i < len(lst) - 8:
if lst[i] == "ImageControl(":
pass
elif lst[i] == "None>=)>":
pass
else:
print("<" + lst[i].strip("\n selected form:\n"))
i += 1


def make_booking(form):
response = form.submit()
request = form.request
print("Header: " + request.header_items())
if response.code == 200:
return ("Response Code " + str(response.code) + ": form submitted successfully.")
else:
return ("Response Code " + str(response.code) + ": form not submitted.")


def main():
args = sys.argv[1:]
if len(args) == 0:
form = enter_data()
elif len(args) < 3 and len(details) >= 0:
print("Incorrect number of parameters.")
sys.exit()
else:
form = fill_form(args[0], args[1], args[2])

form_lst = str(form).split("<")
format_form(form_lst)

confirmation = raw_input("\nIs this the correct input? (y/n): ")

if confirmation == "y":
room_booked = make_booking(form)
print(room_booked)
else:
print("Form submission withdrawn.")

if __name__ == '__main__':
main()

+ 35
- 0
booking.sh View File

@@ -0,0 +1,35 @@
OPTION=$1
ARG1=$2


DIR=$HOME/Documents/Redbrick/EventScripts/RoomBooking/
cd $DIR

if [ $OPTION == '-h' ] || [ $OPTION == '--help' ];then
echo -e "NAME"
echo -e " booking - interacts with scripts to automate events officer tasks\n"
echo -e "DESCRIPTION"
echo -e " -r To book a room using DCU registry booking forms:- booking -r\n"
echo -e " -l To book a lab by emailing the DCU School of Computing secretary:- booking -l LG25\n"
echo -e " -c Checks a rooms availability during a given timeslot:- booking -c\n"
echo -e " -h Brings up these options:- booking -h\n"
echo -e "AUTHOR"
echo -e " James McDermott (theycallmemac)\n"
exit 0
fi


if [ $OPTION == '-c' ] || [ $OPTION == '--check' ];then
python check_availability.py $ARG1
fi


if [ $OPTION == '-l' ] || [ $OPTION == '--lab' ];then
python book_lab.py $ARG1
fi


if [ $OPTION == '-r' ] || [ $OPTION == '--room' ];then
python book_room.py $ARG1
fi


+ 111
- 0
check_availability.py View File

@@ -0,0 +1,111 @@
#!/usr/bin/python
import sys
import cookielib
import mechanize
import requests
from bs4 import BeautifulSoup


def enter_data():
room = raw_input("Enter room code (example: LG25): ")
room = "GLA." + room
if type(room) == str:
pass
else:
print("Not a string.)")
sys.exit()

week = raw_input("Enter week (1 - 52): ")
if int(week) in range(1,53):
pass
else:
print("Week not in range (1 - 52)")
sys.exit()

day = raw_input("Enter day (1[Mon] - 6[Sat]: ")
if int(day) in range(1,7):
pass
else:
print("Day not in range (1 - 6)")
sys.exit()

time = raw_input("Enter starting time (1 - 20): ")
if int(time) in range(1,21):
pass
else:
print("time not in range (1 - 20)")
sys.exit()

details = build_timetable(room, week, day, time)
return details


def check_args(room, week, day, hour):
if type(room) != str:
print("First argument must be a string.")
sys.exit()
elif int(week) not in range(1,53):
print("Second argument not in range")
sys.exit()
elif int(day) not in range(1,7):
print("Third argument not in range")
sys.exit()
elif int(hour) not in range(1,21):
print("Fourth argument not in range")
sys.exit()


def build_timetable(room, week, day, hour):
browser = mechanize.Browser()
cookie_jar = cookielib.LWPCookieJar()
browser.set_cookiejar(cookie_jar)

browser.set_handle_equiv(True)
browser.set_handle_gzip(True)
browser.set_handle_redirect(True)
browser.set_handle_referer(True)
browser.set_handle_robots(False)

browser.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
browser.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")]
url = "https://www101.dcu.ie/timetables/feed.php?room=" + room + "&week1=" + week + "&hour=" + str(hour) + "&day=" + day + "&template=location"
browser.open(url)

return browser, url


def check_room(timetable_url):
html = requests.get(timetable_url)
soup = BeautifulSoup(html.text, "lxml")
tr = soup.select('tr')

return str(tr[12].getText().strip()) + " -> " + str(tr[14].getText().strip())


def main():
args = sys.argv[1:]

if len(args) == 0:
print("1 - 8:00, 2 - 8:30, 3 - 9:00, 4 - 9:30, 5 - 10:00,\n6 - 10:30, 7 - 11:00, 8 - 11:30, 9 - 12:00, 10 - 12:30,\n11 - 13:00, 12 - 13:30, 13 - 14:00, 14 - 14:30, 15 - 15:00,\n16 - 15:30, 17 - 16:00, 18 - 16:30, 19 - 17:00, 20 - 17:30\n")
timetable, url = enter_data()
elif len(args) < 3 and len(details) >= 0:
print("Incorrect number of parameters.")
sys.exit()
else:
check_args("GLA." + str(args[0]), args[1], args[2], args[3])
timetable, url = build_timetable("GLA." + str(args[0]), args[1], args[2], args[3])

print(timetable)
confirmation = raw_input("\nIs this the correct input? (y/n): ")

if confirmation.lower() == "y":
status = check_room(url)
if status == "":
print("That room is free.")
else:
print(status)
else:
print("Action cancelled.")

if __name__ == '__main__':
main()

Loading…
Cancel
Save