capen_inkplate/conf_room/ub_sched.py

104 lines
3.1 KiB
Python

# Room Schedule Lookup
# - Oliver Kennedy okennedy@buffalo.edu
# - Adapted from a shell script by Grant Iraci
# https://gist.github.com/iracigt/181d8f0feafb07ec39b26d4869bffd0d
import urequests as request
import json
import time
# Other useful ROOM_IDs:
ROOM_DAVIS_113A = 1768
ROOM_DAVIS_338A = 1769
ROOM_DAVIS_113Y = 3199
ROOM_DAVIS_310 = 2941
ROOM_DAVIS_337 = 3065
ROOM_DAVIS_101 = 611
ROOM_DAVIS_1ST_FLOOR_ATRIUM = 2409
ROOM_DAVIS_2ND_FLOOR_LOUNGE = 2410
ROOM_CAPEN_212A = 3215
ROOM_ANY = -1
# Other useful BLDG_IDs:
BUILDING_DAVIS_HALL = 74
BUILDING_ANY_BUILDING = -1
UB_SCHED_URL = "https://spacerequest.buffalo.edu/EVNTWebApp/AnonymousServersApi.aspx/CustomBrowseEvents"
def dateoct_to_iso(dateoct):
(year, month, mday, hour, minute, second, weekday, yearday) = dateoct
return "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}".format(year, month, mday, hour, minute, second)
def iso_to_datehex(iso):
(iday, itime) = iso.split("T")
(year, month, mday) = iday.split("-")
(hour, minute, second) = itime.split(":")
return (int(year), int(month), int(mday), int(hour), int(minute), int(second))
class EventTime:
def __init__(self, iso):
(iday, itime) = iso.split("T")
(year, month, mday) = iday.split("-")
(hour, minute, second) = itime.split(":")
self.year = int(year)
self.month = int(month)
self.mday = int(mday)
self.hour = int(hour)
self.minute = int(minute)
self.second = int(second)
def is_date(self, year, month, mday):
# print("{} == {} / {} == {} / {} == {}".format(self.year, year, self.month, month, self.mday, mday))
return (int(self.year) == int(year)) and (int(self.month) == int(month)) and (int(self.mday) == int(mday))
class Event:
def __init__(self, e):
self.event = e["EventName"]
self.start = EventTime(e["TimeBookingStart"])
self.end = EventTime(e["TimeBookingEnd"])
self.room = e["Room"]
self.creator = e["GroupName"]
class RoomSchedule:
def __init__(self, room, building = BUILDING_ANY_BUILDING):
self.BLDG_ID = building
self.ROOM_ID = room
def fetch(self):
req = {
"date": dateoct_to_iso(time.localtime()),
"data": {
"BuildingId": self.BLDG_ID,
"GroupTypeId": -1,
"GroupTypeId": -1,
"GroupId":-1,
"EventTypeId":-1,
"RoomId": self.ROOM_ID,
"StatusId":-1,
"ZeroDisplayOnWeb":0,
"HeaderUrl":"",
"Title":"",
"Format":1,
"Rollup":0,
"EncryptD":"https://spacerequest.buffalo.edu/EVNTWebApp/CustomBrowseEvents.aspx",
"PageSize":1000,
"DropEventsInPast":False
}
}
resp = request.post(
url=UB_SCHED_URL,
json=req,
headers={
"Accept": "application/json, text/javascript"
}
)
wrapper = resp.json()["d"]
bookings = [
Event(e)
for e in json.loads(wrapper)["MonthlyBookingResults"]
]
return bookings
if __name__ == '__main__':
room = RoomSchedule(ROOM_CAPEN_212A)
print(room.fetch())