#!/usr/bin/env python3 import sys import requests import csv from io import StringIO from hashlib import md5 import os from common import * # mapping of member levels to schedules memberLevels = {"CMS Staff": "7x24", "CMS Weekends Only": "Weekends Only", "CMS Weekdays Only": "Weekdays Only", "CMS Unlimited": "Unlimited", "CMS Nights & Weekends": "Nights and Weekends"} def getMembershipworksData(): """ Pull the members csv from the membershipworks api """ BASE_URL = "https://api.membershipworks.com/v1/" # login r = requests.post(BASE_URL + 'usr', data={"_st": "all", "eml": MEMBERSHIPWORKS_USERNAME, "org": "10000", "pwd": MEMBERSHIPWORKS_PASSWORD}) if r.status_code != 200 or 'SF' not in r.json(): print("MembershipWorks Login Error: ", r.status_code, r.reason) print(r.text) sys.exit(1) SFtoken = r.json()['SF'] # get list of member/staff IDs r = requests.get(BASE_URL + "ylp", params={"SF": SFtoken, "lbl": "5ae37979f033bfe8534f8799,5771675edcdf126302a2f6b9", # members and staff "org": "15475", # unknown "var": "_id,nam,ctc"}) if r.status_code != 200 or 'usr' not in r.json(): print("MembershipWorks User Listing Error: ", r.status_code, r.reason) print(r.text) sys.exit(1) ids = [user['_id'] for user in r.json()['usr']] # get members CSV # TODO: maybe can just use previous get instead? would return JSON r = requests.post(BASE_URL + "csv", params={"SF": SFtoken}, data={"_rt": "946702800", # unknown "mux": "", # unknown "tid": ",".join(ids), # ids of members to get "var": "lvl,xws,xms,xsc,xas,xfd,xac,phn,eml,lbl,xcf,nam"}) # which columns to get if r.status_code != 200: print("MembershipWorks CSV Generation Error: ", r.status_code, r.reason) print(r.text) sys.exit(1) return r.text 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 = {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(doorName, doorData, members, prevHash): """Create a CSV for the given door""" outString = StringIO() writer = csv.DictWriter(outString, fieldnames) writer.writeheader() for member in members: member = makeMember(member, "Access " + doorData["access"] + "?") if member is not None: writer.writerow(member) print(outString.getvalue()) outString.seek(0) doorHash = md5(bytes(outString.getvalue(), 'utf8')).hexdigest() if doorHash == prevHash: print("Door", doorName, "not changed, not updating") else: doCSVImport(doorData["ip"], outString) return doorHash def main(): data = getMembershipworksData() reader = csv.DictReader(StringIO(data)) members = list(reader) if os.path.exists('/tmp/doorUpdaterLastHash'): with open('/tmp/doorUpdaterLastHash', 'r') as f: hashes = json.load(f) else: hashes = {} for doorName, doorData in config["doors"].items(): print(doorName, doorData) hashes[doorName] = makeDoor(doorName, doorData, members, hashes.get(doorName, "")) # write out hash if we sucessfully got here with open('/tmp/doorUpdaterLastHash', 'w') as f: json.dump(hashes, f) if __name__ == '__main__': main()