commit 73600db7f64c617d9cf55af25c4aca8ee9c1cbee Author: Adam Goldsmith Date: Mon May 7 18:02:04 2018 -0400 Initial Commit, mostly working diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..08b3f43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/__pycache__/ +/passwords.py diff --git a/doorUpdater.py b/doorUpdater.py new file mode 100644 index 0000000..0bcd2f1 --- /dev/null +++ b/doorUpdater.py @@ -0,0 +1,117 @@ +import requests +import csv +from io import StringIO +import urllib3 + +from passwords import * + +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + +fieldnames = "CardNumber,CardFormat,PinRequired,PinCode,ExtendedAccess,ExpiryDate,Forename,Initial,Surname,Email,Phone,Custom1,Custom2,Schedule1,Schedule2,Schedule3,Schedule4,Schedule5,Schedule6,Schedule7,Schedule8".split(",") + +memberLevels = {"CMS Staff": "7x24", + "CMS Weekends Only": "Weekends Only", + "CMS Weekdays Only": "Weekdays Only", + "CMS Unlimited": "Unlimited", + "CMS Nights & Weekends": "Nights and Weekends"} + +doors = { + "Rental Studio": ["172.18.51.11"], + "Storage Closet": ["172.18.51.12"], + "Metal Shop": ["172.18.51.13"], + "Wood Shop": ["172.18.51.14", "172.18.51.15"] +} + +def getMembershipworksData(): + BASE_URL = "https://api.membershipworks.com/v1/" + + sess = requests.Session() + r = sess.post(BASE_URL + 'usr', + data={"_st": "all", + "eml": MEMBERSHIPWORKS_USERNAME, + "org": "10000", + "pwd": MEMBERSHIPWORKS_PASSWORD}) + SFtoken = r.json()['SF'] + + r = sess.get(BASE_URL + "ylp", + params={"SF": SFtoken, + "lbl": "5ae37979f033bfe8534f8799,5771675edcdf126302a2f6b9", + "org": "15475", + "var": "_id,nam,ctc"}) + ids = [user['_id'] for user in r.json()['usr']] + + r = sess.post(BASE_URL + "csv", + params={"SF": SFtoken}, + data={"_rt": "946702800", + "mux": "", + "tid": ",".join(ids), + "var": "lvl,xws,xms,xsc,xrs,xfd,xac,phn,eml,lbl,nam"}) + return r.text + + +def makeMember(member, doorAuth): + 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-241", + "PinRequired": "0", + "ExtendedAccess": "0", + "ExpiryDate": "", + "Email": member["Email"], + "Phone": member["Phone"]} + + if doorAuth: + memberLevel = [v for k, v in memberLevels.items() if member[k] == k] + if len(memberLevel) == 1: + out["Schedule1"] = memberLevel[0] + else: + print(member["First Name"], member["Last Name"], "has no/too many member levels!") + + return out + +def doRequest(ip, params=None, files=None): + r = requests.post( + 'https://' + ip + '/cgi-bin/import.cgi', + params=params, + files=files, + auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD), + verify=False) + print(r.text) + +def updateDoor(doorIP, csv): + doRequest(doorIP, {"task": "importInit"}) + doRequest(doorIP, + {"task": "importCardsPeople", "name": "cardspeopleschedule.csv"}, + {"importCardsPeopleButton": ("cardspeopleschedule.csv", csv, 'text/csv')}) + doRequest(doorIP, {"task": "importDone"}) + +def makeDoor(doorName, doorIPs, members): + outString = StringIO() + writer = csv.DictWriter(outString, fieldnames) + writer.writeheader() + for member in members: + member = makeMember(member, member["Access " + doorName + "?"] != "") + if member is not None: + writer.writerow(member) + + print(outString.getvalue()) + for doorIP in doorIPs: + outString.seek(0) + updateDoor(doorIP, outString) + pass + +def main(): + data = getMembershipworksData() + reader = csv.DictReader(StringIO(data)) + members = list(reader) + + for doorName, doorIP in doors.items(): + makeDoor(doorName, doorIP, members) + +if __name__ == '__main__': + main()