#!/usr/bin/env python3 import requests from xml.etree import ElementTree as ET import csv from passwords import * #credentialRanges = {"6": {"min": "20176", "max": "20350"}} memberLevels = ["CMS Staff", "CMS Weekends Only", "CMS Weekdays Only", "CMS Unlimited", "CMS Nights & Weekends"] ET.register_namespace("hid", "http://www.hidglobal.com/VertX") ET.register_namespace("hid", "http://www.hidcorp.com/VertX") XML = ET.Element("VertXMessage") #TODO: both those might need more stuff: # recordOffset="0" recordCount="4" moreRecords="false" cardholders = ET.SubElement(XML, "hid:Cardholders", attrib={"action": "AD", "recordOffset": "0"}) credentials = ET.SubElement(XML, "hid:Credentials", attrib={"action": "AD"}) def doRequest(xml): return requests.get( 'https://172.18.51.11/cgi-bin/vertx_xml.cgi', params={'XML': b'' + ET.tostring(xml)}, auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD), verify=False) def makeRoleSet(roleSetID, scheduleID): roleSet = ET.SubElement(XML, "hid:RoleSet", attrib={"action": "UD", "roleSetID": str(roleSetID)}) roles = ET.SubElement(roleSet, "hid:Roles") ET.SubElement(roles, "hid:Role", attrib={"roleID": str(roleSetID), "scheduleID": str(scheduleID), "resourceID": "0"}) def makeCardHolder(id, fname, lname): attrib={"cardhlderID": str(id), "forename": fname, "surname": lname, # "email": "", #TODO # "phone": "", #TODO "roleSetID": str(id)} ET.SubElement(cardholders, "hid:Cardholder", attrib=attrib) def makeCredential(cardNum, cardHolderID): ET.SubElement(credentials, "hid:Credential", attrib={"isCard": "true", #TODO: needed? "cardNumber": str(cardNum), "cardholderID": str(cardHolderID), "formatID": "6"}) def handleRow(index, row): makeCardHolder(index, row["First Name"], row["Last Name"]) memberLevel = [roleSetID for roleSetID, name in enumerate(memberLevels, 1) if row[name] != ""] if len(memberLevel) == 1: makeRoleSet(index, memberLevel[0]) else: print(row["First Name"], row["Last Name"], "has no/too many member levels!") if row["Access Card Number"] != "": makeCredential(row["Access Card Number"], index) def deleteStuff(): delXML = ET.Element("VertXMessage") queryXML = ET.Element("VertXMessage") ET.SubElement(queryXML, "hid:Credentials", attrib={"action": "LR"}) r = doRequest(queryXML) respXML = ET.XML(r.text) for cred in respXML[0]: ET.SubElement(delXML, "hid:Credentials", attrib={"action": "DD", "isCard": "true", "rawCardNumber": cred.attrib["rawCardNumber"]}) for ii in range(1, 300): ET.SubElement(delXML, "hid:Cardholders", attrib={"action": "DD", "cardholderID": str(ii)}) r = doRequest(delXML) print(r.text) def main(): deleteStuff() # Include schedules.xml fragment # with open("schedules.xml") as f: # root = ET.XML(f.read()) # for e in root: # XML.append(e) with open("export-16.csv") as f: reader = csv.DictReader(f) for index, row in enumerate(reader, 1): handleRow(index, row) with open("/home/adam/scratch/test.xml", "wb") as f: f.write(ET.tostring(XML)) r = doRequest(XML) print(r.status_code, r.text) # if __name__ == '__main__': # main() main()