Browse Source

removed duplications

pull/24/head
theycallmemac 3 years ago
parent
commit
9f940ce01e
8 changed files with 85 additions and 59 deletions
  1. +2
    -13
      scripts/lab_booking.py
  2. +2
    -15
      scripts/lookup.py
  3. +15
    -4
      scripts/main.py
  4. +8
    -10
      scripts/now.py
  5. +8
    -5
      scripts/opts.py
  6. +0
    -12
      scripts/room_booking.py
  7. +25
    -0
      scripts/shared.py
  8. +25
    -0
      scripts/utils.py

+ 2
- 13
scripts/lab_booking.py View File

@@ -34,17 +34,6 @@ class LabBooking(object):
return (FROM, TO), message


def confirm(self):
if int(sys.version[0]) < 3:
conf = raw_input("Is this information correct? (y/n): ")
else:
conf - input("Is this information correct? (y/n): ")
if conf == "y":
return conf
else:
return "n"


def send(self, FROM, TO, message):
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
@@ -53,7 +42,7 @@ class LabBooking(object):
server.login(self.email, self.password)
server.sendmail(FROM, TO, message)
server.close()
print("Your email has been sent.")
return "Your email has been sent."
except BaseException:
print("Email failed to send.")
return "Email failed to send."


+ 2
- 15
scripts/lookup.py View File

@@ -2,6 +2,7 @@ import sys
from mechanicalsoup import StatefulBrowser
from requests import get
from bs4 import BeautifulSoup
import utils

if sys.version_info[0] < 3:
from cookielib import LWPCookieJar
@@ -23,13 +24,6 @@ class LookUp(object):
self.day = day
self.hour = hour

def check_args(self):
if int(self.week) not in range(1, 53) or int(self.day) not in range(1, 7):
print("Incorrect parameters passed.")
sys.exit()
else:
pass

def check_time(self, times):
if self.hour not in times:
print("Outside scheduled timetables. Please try again at 08:00.")
@@ -62,13 +56,6 @@ class LookUp(object):
Safari/537.36""")]
url = "https://www.dcu.ie/timetables/feed.php?room=GLA." + room + "&week1=" + self.week + "&hour=" + str(self.hour) + "&day=" + self.day + "&template=location"
browser.open(url)
result = LookUp.check_room(self, url)
result = utils.check_room(url)
return result

def check_room(self, url):
html = get(url)
soup = BeautifulSoup(html.text, "lxml")
tr = soup.select('tr')
return str(tr[12].getText().strip()) + " -> " + \
str(tr[14].getText().strip())


+ 15
- 4
scripts/main.py View File

@@ -52,17 +52,28 @@ def required():
times, c, g, details = get_data()
return parser, (options, arguments), (c, g), (times, details)

def main():
parser, (options, arguments), rooms, info = required()
def booking(options, info):
if options.book and info[1][0][0] == "L":
opts.booking_lab(info)
elif options.book and info[1][0][0] == "C":
opts.booking_room(info)

def lookup(options, rooms, info):
if options.lookup and (options.grattan or options.computing):
opts.lookup_building(rooms, info, options)
elif options.lookup:
opts.lookup_room(info)
if options.now:
opts.now(rooms, info, options)

def now(options, rooms, info):
opts.now(rooms, info, options)

def main():
parser, (options, arguments), rooms, info = required()
if options.book:
booking(options, info)
elif options.lookup:
lookup(options, rooms, info)
elif options.now:
now(options, rooms, info)
if __name__ == "__main__":
main()

+ 8
- 10
scripts/now.py View File

@@ -2,6 +2,7 @@ import sys
from mechanicalsoup import StatefulBrowser
from requests import get
from bs4 import BeautifulSoup
import utils

if sys.version_info[0] < 3:
from cookielib import LWPCookieJar
@@ -43,14 +44,17 @@ class Now(object):
self.minute = '00'

def check_time(self, times):
if self.hour not in times:
time = self.hour + self.minute
if time not in times:
print("Outside scheduled timetables. Please try again at 08:00.")
sys.exit()
for k, v in times.items():
if k == self.hour + self.minute:
self.hour = v
if k == self.hour:
time = v
break
else:
pass
return time

def get_status(self, options, room, status):
if options.available:
@@ -76,12 +80,6 @@ class Now(object):
Safari/537.36""")]
url = "https://www.dcu.ie/timetables/feed.php?room=GLA." + room + "&week1=" + self.week + "&hour=" + self.hour + "&day=" + self.day + "&template=location"
browser.open(url)
result = Now.check_room(self, url)
result = utils.check_room(url)
return result

def check_room(self, url):
html = get(url)
soup = BeautifulSoup(html.text, "lxml")
tr = soup.select('tr')
return str(tr[12].getText().strip()) + " -> " + str(tr[14].getText().strip())


+ 8
- 5
scripts/opts.py View File

@@ -5,6 +5,7 @@ from room_booking import RoomBooking
from lookup import LookUp
from now import Now
import utils
import shared

__author__ = "theycallmemac"
__version__ = '2.0.0'
@@ -22,9 +23,10 @@ def booking_lab(info):
lab = LabBooking(email, password, your_name, society, info[1])
emails, message = lab.draft()
print(message)
conf = lab.confirm()
conf = shared.confirm()
if conf == "y":
result = lab.send(emails[0], emails[1], message)
print(result)
else:
"Booking cancelled."

@@ -32,9 +34,10 @@ def booking_room(info):
email, number, name, society = get_room_credentials()
room = RoomBooking(email, number, name, society, info[1])
form = room.fill()
conf = room.confirm()
conf = shared.confirm()
if conf == "y":
result = room.submit(form)
print(result)
else:
"Booking cancelled."

@@ -42,14 +45,14 @@ def lookup_building(rooms, info, options):
lst = utils.get_lst(rooms[0], rooms[1], options)
week, day, hour = info[1]
look = LookUp(week, day, hour)
look.check_args()
shared.check_args(week, day)
look.check_time(info[0])
look.building_option(lst)

def lookup_room(info):
room, week, day, hour = info[1]
look = LookUp(week, day, hour)
look.check_args()
utils.check_args(week, day)
look.check_time(info[0])
look.room_option(room)

@@ -57,7 +60,7 @@ def now(rooms, info, options):
week, day, hour, minute = utils.get_current_time(datetime.datetime.now())
now = Now(week, day, hour, minute)
now.round_it()
now.check_args()
utils.check_args(week, day)
now.check_time(info[0])
lst = utils.get_lst(rooms[0], rooms[1], options)
now.building_option(lst, options)


+ 0
- 12
scripts/room_booking.py View File

@@ -47,18 +47,6 @@ class RoomBooking(object):
browser["submitted[email_address]"] = self.email
return browser

def confirm(self):
if int(sys.version[0]) < 3:
conf = raw_input("Is this information correct? (y/n): ")
else:
conf - input("Is this information correct? (y/n): ")

if conf == "y":
return conf
else:
return "n"


def submit(self, form):
request = form.request
response = form.submit_selected()


+ 25
- 0
scripts/shared.py View File

@@ -0,0 +1,25 @@
import sys
import smtplib


__author__ = "theycallmemac"
__version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'

def confirm():
if int(sys.version[0]) < 3:
conf = raw_input("Is this information correct? (y/n): ")
else:
conf - input("Is this information correct? (y/n): ")
if conf == "y":
return conf
else:
return "n"

def check_args(week, day):
if int(week) not in range(1, 53) or int(day) not in range(1, 7):
print("Incorrect parameters passed.")
sys.exit()
else:
pass

+ 25
- 0
scripts/utils.py View File

@@ -1,11 +1,36 @@
import datetime
import sys
from requests import get
from bs4 import BeautifulSoup

__author__ = "theycallmemac"
__version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'

def confirm():
if int(sys.version[0]) < 3:
conf = raw_input("Is this information correct? (y/n): ")
else:
conf = input("Is this information correct? (y/n): ")
if conf == "y":
return conf
else:
return "n"


def check_args(week, day):
if int(week) not in range(1, 53) or int(day) not in range(1, 7):
print("Incorrect parameters passed.")
sys.exit()
else:
pass

def check_room(timetable_url):
html = get(timetable_url)
soup = BeautifulSoup(html.text, "lxml")
tr = soup.select('tr')
return str(tr[12].getText().strip()) + " -> " + str(tr[14].getText().strip())

def get_lst(c, g, options):
if options.computing == True: lst = c


Loading…
Cancel
Save