#! /usr/bin/env python3 import calendar, datetime, getpass, json, requests, sys, time def api_request(method, url, auth_token, request_data): headers = {"Content-Type": "application/json; charset=utf-8"} #if request_data is not None: # headers["Content-Type"] = "application/json; charset=utf-8" if auth_token is not None: headers["Authorization"] = "Bearer " + auth_token if method == "get": func = requests.get elif method == "post": func = requests.post else: raise Exception("invalid method: " + method) response = func( "https://checkin.hs-offenburg.de/api/v1" + url, headers = headers, data = json.dumps(request_data) if request_data is not None else None, ) return response.json() def checkin(user, password, room): login = api_request("post", "/internal/login", None, { "username": user, "password": password, }) if "message" in login: print(login["message"], file=sys.stderr) exit(1) auth_token = login["token"] contact_details = api_request("get", "/contact-details", auth_token, None) checkin_data = { "payload": { "detailsId": contact_details["id"], "email": contact_details["email"], "entryType": "standard", "period": 90, "prename": contact_details["prename"], "seatNumber": "", "slug": room.lower(), "startDate": datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"), "surname": contact_details["surname"], "username": contact_details["username"], }, } checkin_result = api_request("post", "/internal/check-in", auth_token, checkin_data) if "error" in checkin_result: print(checkin_result["message"], file=sys.stderr) exit(1) utcEndTime = time.strptime(checkin_result["endTime"], "%Y-%m-%dT%H:%M:%S.%fZ") localEndTime = time.localtime(calendar.timegm(utcEndTime)) endTimeStr = time.strftime("%H:%M", localEndTime) print("Successfully checked in until " + endTimeStr) def main(): if len(sys.argv) != 2: print("Usage: checkin ", file=sys.stderr) exit(1) user = input("HSO username: ") password = getpass.getpass("HSO password: ") checkin(user, password, sys.argv[1]) if __name__ == '__main__': main()