A mirror of dcurooms found at https://github.com/theycallmemac/dcurooms
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

103 linhas
3.1 KiB

  1. import sys
  2. from mechanicalsoup import StatefulBrowser
  3. from requests import get
  4. from bs4 import BeautifulSoup
  5. import urllib3
  6. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  7. import utils
  8. if sys.version_info[0] < 3:
  9. from cookielib import LWPCookieJar
  10. else:
  11. from http.cookiejar import LWPCookieJar
  12. __author__ = "theycallmemac"
  13. __version__ = '2.0.0'
  14. __copyright__ = 'Copyright (c) 2018 theycallmemac'
  15. __license__ = 'GPL-3.0'
  16. class Now(object):
  17. week = ""
  18. day = ""
  19. hour = ""
  20. minute = ""
  21. def __init__(self, week, day, hour, minute):
  22. self.week = week
  23. self.day = day
  24. self.hour = hour
  25. self.minute = minute
  26. def check_args(self):
  27. if int(
  28. self.week) not in range(
  29. 1,
  30. 53) or int(
  31. self.day) not in range(
  32. 1,
  33. 7):
  34. print("\033[1;91m{0}\033[00m".format(
  35. "Incorrect parameters passed."))
  36. sys.exit()
  37. else:
  38. pass
  39. def round_it(self):
  40. if int(self.hour) < 8 or int(self.hour) >= 23:
  41. print("\033[1;93m{0}\033[00m".format(
  42. "Outside scheduled timetables. Try again at 08:00."))
  43. sys.exit()
  44. if int(self.minute) >= 30:
  45. self.minute = '30'
  46. else:
  47. self.minute = '00'
  48. def check_time(self, times):
  49. time = "0" + self.hour + self.minute
  50. if time not in times:
  51. print("\033[1;93m{0}\033[00m".format(
  52. "Outside scheduled timetables. Please try again at 08:00."))
  53. sys.exit()
  54. for k, v in times.items():
  55. if k == time:
  56. self.hour = v
  57. break
  58. else:
  59. pass
  60. return self.hour
  61. def get_status(self, options, room, status):
  62. if options.available:
  63. if len(status) <= 9:
  64. print("\033[1;92m{0}\033[00m".format(room) +
  65. ": " + "\033[1;97m{0}\033[00m".format(status))
  66. elif len(status) > 9:
  67. print("\033[1;91m{0}\033[00m".format(room) +
  68. ": " + "\033[1;90m{0}\033[00m".format(status))
  69. else:
  70. print("\033[1;92m{0}\033[00m".format(room) +
  71. ": " + "\033[1;97m{0}\033[00m".format(status))
  72. def building_option(self, lst, options):
  73. for room in lst:
  74. status = Now.build_timetable(self, room)
  75. Now.get_status(self, options, room, status)
  76. def build_timetable(self, room):
  77. browser = StatefulBrowser()
  78. cookie_jar = LWPCookieJar()
  79. browser.set_cookiejar(cookie_jar)
  80. browser.user_agent = [
  81. ("User-Agent",
  82. """Mozilla/5.0 (Windows NT 10.0; Win64; x64)
  83. AppleWebKit/537.36 (KHTML, like Gecko)
  84. Chrome/58.0.3029.110
  85. Safari/537.36""")]
  86. url = "http://www101.dcu.ie/timetables/feed.php?room=GLA." + room + "&week1=" + \
  87. self.week + "&hour=" + self.hour + "&day=" + self.day + "&template=location"
  88. browser.open(url, verify=False)
  89. result = utils.check_room(url)
  90. return result