A mirror of dcurooms found at https://github.com/theycallmemac/dcurooms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.0 KiB

  1. import sys
  2. from mechanicalsoup import StatefulBrowser
  3. from bs4 import BeautifulSoup
  4. import utils
  5. if sys.version_info[0] < 3:
  6. from cookielib import LWPCookieJar
  7. else:
  8. from http.cookiejar import LWPCookieJar
  9. __author__ = "theycallmemac"
  10. __version__ = '2.0.0'
  11. __copyright__ = 'Copyright (c) 2018 theycallmemac'
  12. __license__ = 'GPL-3.0'
  13. class LookUp(object):
  14. week = ""
  15. day = ""
  16. hour = ""
  17. def __init__(self, week, day, hour):
  18. self.week = week
  19. self.day = day
  20. self.hour = hour
  21. def check_time(self, times):
  22. if self.hour not in times:
  23. print("Outside scheduled timetables. Please try again at 08:00.")
  24. sys.exit()
  25. for k, v in times.items():
  26. if k == self.hour:
  27. self.hour = v
  28. break
  29. else:
  30. pass
  31. def building_option(self, lst):
  32. for room in lst:
  33. status = LookUp.build_timetable(self, room)
  34. if len(status) <= 9:
  35. print("\033[1;92m{0}\033[00m".format(room) +
  36. ": " + "\033[1;97m{0}\033[00m".format(status))
  37. else:
  38. print("\033[1;91m{0}\033[00m".format(room) +
  39. ": " + "\033[1;90m{0}\033[00m".format(status))
  40. def room_option(self, room):
  41. status = LookUp.build_timetable(self, room)
  42. print(room + ": " + status)
  43. def build_timetable(self, room):
  44. browser = StatefulBrowser()
  45. cookie_jar = LWPCookieJar()
  46. browser.set_cookiejar(cookie_jar)
  47. browser.user_agent = [
  48. ("User-Agent",
  49. """Mozilla/5.0 (Windows NT 10.0; Win64; x64)
  50. AppleWebKit/537.36 (KHTML, like Gecko)
  51. Chrome/58.0.3029.110
  52. Safari/537.36""")]
  53. url = "http://www101.dcu.ie/timetables/feed.php?room=GLA." + \
  54. room + "&week1=" + self.week + \
  55. "&hour=" + str(self.hour) + \
  56. "&day=" + self.day + "&template=location"
  57. browser.open(url, verify=False)
  58. result = utils.check_room(url)
  59. return result