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.

69 lines
2.1 KiB

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