diff --git a/doorUpdater.py b/doorUpdater.py deleted file mode 100755 index f3833a4..0000000 --- a/doorUpdater.py +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env python3 -import sys -import requests -import csv -from collections import OrderedDict -from io import StringIO -from hashlib import md5 -import os - -from common import * -from hid.DoorController import fieldnames - -def makeMember(member, doorAuth): - """Create an output CSV row for the member""" - if member["Access Card Number"] == "": - #print(member["First Name"], member["Last Name"], " has no card number, ignoring") - return - - out = {"Forename": member["First Name"], - "Surname": member["Last Name"], - "Initial": "", - "CardNumber": member["Access Card Number"], - "CardFormat": "A901146A-" + member["Access Card Facility Code"], - "PinRequired": "0", - "ExtendedAccess": "0", - "ExpiryDate": "", - "Email": member["Email"], - "Phone": member["Phone"]} - - if member[doorAuth] == "Y" \ - and not member["Account on Hold"] == "Account on Hold": - levels = OrderedDict(sorted([(k, v) for k, v in memberLevels.items() if member[k] == k])) - out["Custom1"] = "|".join(levels.keys()).replace("&", "and") - for index, schedule in enumerate(levels.values(), 1): - #TODO: error if people have more than 8? - out["Schedule" + str(index)] = schedule - - return out - -def makeDoor(door, members, hashes): - """Create a CSV for the given door""" - outString = StringIO() - writer = csv.DictWriter(outString, fieldnames) - writer.writeheader() - for member in members: - member = makeMember(member, "Access " + door.access + "?") - if member is not None: - writer.writerow(member) - - import datetime as DT - timestamp = DT.datetime.now().strftime("%Y-%m-%d %H:%M:%S") - with open("/tmp/" + door.name + timestamp + ".csv", "w") as f: - f.write(outString.getvalue()) - - outString.seek(0) - doorHash = md5(bytes(outString.getvalue(), 'utf8')).hexdigest() - if doorHash == hashes.get(door.name): - print("Door", door.name, "not changed, not updating") - else: - print("Door", door.name, "changed, trying to update") - hashes[door.name] = doorHash - door.doCSVImport(outString) - # write out hash if we sucessfully updated this door - with open('/tmp/doorUpdaterLastHash', 'w') as f: - json.dump(hashes, f) - -def main(): - members = getMembershipworksData( - ['members', 'staff', 'misc'], - "lvl,xws,xms,xsc,xas,xfd,xac,phn,eml,lbl,xcf,nam") - members.sort(key=lambda x: x['Last Name']) - - if os.path.exists('/tmp/doorUpdaterLastHash'): - with open('/tmp/doorUpdaterLastHash', 'r') as f: - hashes = json.load(f) - else: - hashes = {} - - for door in doors.values(): - print(door.name, door.ip) - makeDoor(door, members, hashes) - -if __name__ == '__main__': - main() diff --git a/old_xml_based.py b/old_xml_based.py deleted file mode 100755 index 6693d35..0000000 --- a/old_xml_based.py +++ /dev/null @@ -1,110 +0,0 @@ -#!/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"] - -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()