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.

147 lines
5.1 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. import sys
  5. from optparse import OptionParser
  6. from cookielib import LWPCookieJar
  7. from mechanize import Browser, _http
  8. from requests import get
  9. from bs4 import BeautifulSoup
  10. __author__ = "theycallmemac"
  11. __version__ = '0.2.1'
  12. __copyright__ = 'Copyright (c) 2017 theycallmemac'
  13. __license__ = 'GPL-3.0'
  14. def get_current_time(date):
  15. day = date.weekday()
  16. hour = date.hour
  17. minute = date.minute
  18. year, week_no, weekday = date.isocalendar()
  19. start = 36
  20. if week_no >= start:
  21. offset = -start
  22. else:
  23. offset = 52 - start
  24. week = week_no + offset - 1
  25. return str(week), str(day + 1), str(hour), str(minute)
  26. def build_timetable(room, week, day, hour):
  27. browser = Browser()
  28. cookie_jar = LWPCookieJar()
  29. browser.set_cookiejar(cookie_jar)
  30. browser.set_handle_equiv(True)
  31. browser.set_handle_gzip(True)
  32. browser.set_handle_redirect(True)
  33. browser.set_handle_referer(True)
  34. browser.set_handle_robots(False)
  35. browser.set_handle_refresh(_http.HTTPRefreshProcessor(), max_time=1)
  36. browser.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")]
  37. url = "https://www101.dcu.ie/timetables/feed.php?room=" + room + "&week1=" + week + "&hour=" + str(hour) + "&day=" + day + "&template=location"
  38. browser.open(url)
  39. return browser, url
  40. def check_room(timetable_url):
  41. html = get(timetable_url)
  42. soup = BeautifulSoup(html.text, "lxml")
  43. tr = soup.select('tr')
  44. return str(tr[12].getText().strip()) + " -> " + str(tr[14].getText().strip())
  45. def main():
  46. parser = OptionParser(description='Displays and books rooms around the DCU campus via provided timetable/module details', prog='dcurooms', version='%prog ' + __version__, usage='%prog [option]')
  47. parser.add_option("-f", "--free", action="store_true", help="returns only the rooms/labs that are free in a building")
  48. parser.add_option("-n", "--now", action="store_true", help="show the status of each room/lab as it is at the current time of checking")
  49. parser.add_option("-c", "--computing", action="store_true", help="displays the status of the labs in the School of Computing")
  50. parser.add_option("-g", "--grattan", action="store_true", help="displays the status of rooms in the Henry Grattan building")
  51. (options, arguments) = parser.parse_args()
  52. times = {'0800':'1',
  53. '0830':'2',
  54. '0900':'3',
  55. '0930':'4',
  56. '1000':'5',
  57. '1030':'6',
  58. '1100':'7',
  59. '1130':'8',
  60. '1200':'9',
  61. '1230':'10',
  62. '1300':'11',
  63. '1330':'12',
  64. '1400':'13',
  65. '1430':'14',
  66. '1500':'15',
  67. '1530':'16',
  68. '1600':'17',
  69. '1630':'18',
  70. '1700':'19',
  71. '1730':'20',
  72. '1800':'21',
  73. '1830':'22',
  74. '1900':'23',
  75. '1930':'24',
  76. '2000':'25',
  77. '2030':'26',
  78. '2100':'27',
  79. '2130':'28',
  80. '2200':'29',
  81. '2230':'30'}
  82. if options.now == True:
  83. c = ['LG25','LG26','LG27','L101','L114','L125','L128']
  84. g = ['CG01', 'CG02','CG03','CG04','CG05','CG06','CG11','CG12','CG20','CG68','C166']
  85. week, day, hour, minute = get_current_time(datetime.datetime.now())
  86. if int(hour) < 8 or int(hour) >= 23:
  87. print("Outside scheduled timetables. Try again at 08:00.")
  88. sys.exit()
  89. if int(minute) >= 30:
  90. minute = '30'
  91. else:
  92. minute = '00'
  93. current_time = hour + minute
  94. for k,v in times.items():
  95. if k == current_time:
  96. current_time = v
  97. break
  98. else:
  99. pass
  100. if options.computing == True:
  101. if options.free == True:
  102. for room in c:
  103. timetable, url = build_timetable("GLA." + room, week, day, current_time)
  104. status = check_room(url)
  105. if len(status) <= 9:
  106. print(room + ": " + status)
  107. else:
  108. for room in c:
  109. timetable, url = build_timetable("GLA." + room, week, day, current_time)
  110. status = check_room(url)
  111. print(room + ": " + status)
  112. elif options.grattan == True:
  113. if options.free == True:
  114. for room in g:
  115. timetable, url = build_timetable("GLA." + room, week, day, current_time)
  116. status = check_room(url)
  117. if len(status) <= 9:
  118. print(room + ": " + status)
  119. else:
  120. for room in g:
  121. timetable, url = build_timetable("GLA." + room, week, day, current_time)
  122. status = check_room(url)
  123. print(room + ": " + status)
  124. else:
  125. parser.print_help()
  126. else:
  127. parser.print_help()
  128. if __name__ == '__main__':
  129. main()