This repository has been archived on 2024-02-23. You can view files and clone it, but cannot push or open issues or pull requests.
memberPlumbing/common.py

88 lines
3.4 KiB
Python
Raw Permalink Normal View History

import csv
2018-05-27 17:17:36 -04:00
import json
import urllib3
2018-08-13 15:46:00 -04:00
import os
2018-06-03 14:26:58 -04:00
import sys
from io import StringIO
import requests
2018-08-16 14:01:42 -04:00
from hid.DoorController import DoorController
from pyunifi.controller import Controller as UnifiController
from passwords import *
# it's fine, ssl certs are for losers anyway
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
2018-08-16 12:52:25 -04:00
try:
config = json.load(
open(os.path.dirname(os.path.abspath(__file__)) + "/config.json"))
except NameError:
config = json.load(open("config.json"))
2018-08-16 14:01:42 -04:00
doors = {doorName: DoorController(doorData['ip'],
DOOR_USERNAME, DOOR_PASSWORD,
name=doorName, access=doorData['access'],
mac=doorData['mac'])
2018-08-16 14:01:42 -04:00
for doorName, doorData in config["doors"].items()}
unifiController = UnifiController(config['unifi-controller']['host'],
UNIFI_USERNAME, UNIFI_PASSWORD,
ssl_verify = False)
# 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",
"CMS Day Pass": "Unlimited"}
def getMembershipworksData(folders, columns):
""" Pull the members csv from the membershipworks api
folders: a list of the names of the folders to get
(see folder_map in this function for mapping to ids)
columns: which columns to get"""
BASE_URL = "https://api.membershipworks.com/v1/"
folder_map = {'members': '5ae37979f033bfe8534f8799',
'staff': '5771675edcdf126302a2f6b9',
'misc': '5b69ee9bf033bf8e7346c434'}
# 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)
login_data = r.json()
# get list of member/staff IDs
r = requests.get(BASE_URL + "ylp",
params={"SF": login_data['SF'],
"lbl": ",".join([folder_map[f] for f in folders]),
"org": login_data['org'],
"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['uid'] 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": login_data['SF']},
data={"_rt": "946702800", # unknown
"mux": "", # unknown
"tid": ",".join(ids), # ids of members to get
"var": columns})
if r.status_code != 200:
print("MembershipWorks CSV Generation Error: ", r.status_code, r.reason)
print(r.text)
sys.exit(1)
return list(csv.DictReader(StringIO(r.text)))