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.

89 lines
2.8 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 Now(object):
  15. week = ""
  16. day = ""
  17. hour = ""
  18. minute = ""
  19. def __init__(self, week, day, hour, minute):
  20. self.week = week
  21. self.day = day
  22. self.hour = hour
  23. self.minute = minute
  24. def check_args(self):
  25. if int(self.week) not in range(1, 53) or int(self.day) not in range(1, 7):
  26. print("\033[1;91m{0}\033[00m".format("Incorrect parameters passed."))
  27. sys.exit()
  28. else:
  29. pass
  30. def round_it(self):
  31. if int(self.hour) < 8 or int(self.hour) >= 23:
  32. print("\033[1;93m{0}\033[00m".format("Outside scheduled timetables. Try again at 08:00."))
  33. sys.exit()
  34. if int(self.minute) >= 30:
  35. self.minute = '30'
  36. else:
  37. self.minute = '00'
  38. def check_time(self, times):
  39. time = self.hour + self.minute
  40. if time not in times:
  41. print("\033[1;93m{0}\033[00m".format("Outside scheduled timetables. Please try again at 08:00."))
  42. sys.exit()
  43. for k, v in times.items():
  44. if k == self.hour:
  45. time = v
  46. break
  47. else:
  48. pass
  49. return time
  50. def get_status(self, options, room, status):
  51. if options.available:
  52. if len(status) <= 9:
  53. print("\033[1;92m{0}\033[00m".format(room) + ": " +"\033[1;97m{0}\033[00m".format(status))
  54. elif len(status) > 9:
  55. print("\033[1;91m{0}\033[00m".format(room) + ": " +"\033[1;90m{0}\033[00m".format(status))
  56. else:
  57. print("\033[1;92m{0}\033[00m".format(room) + ": " +"\033[1;97m{0}\033[00m".format(status))
  58. def building_option(self, lst, options):
  59. for room in lst:
  60. status = Now.build_timetable(self, room)
  61. Now.get_status(self, options, room, status)
  62. def build_timetable(self, room):
  63. browser = StatefulBrowser()
  64. cookie_jar = LWPCookieJar()
  65. browser.set_cookiejar(cookie_jar)
  66. browser.user_agent = [
  67. ("User-Agent",
  68. """Mozilla/5.0 (Windows NT 10.0; Win64; x64)
  69. AppleWebKit/537.36 (KHTML, like Gecko)
  70. Chrome/58.0.3029.110
  71. Safari/537.36""")]
  72. url = "https://www.dcu.ie/timetables/feed.php?room=GLA." + room + "&week1=" + self.week + "&hour=" + self.hour + "&day=" + self.day + "&template=location"
  73. browser.open(url)
  74. result = utils.check_room(url)
  75. return result