2018-06-19 18:24:15 -04:00
|
|
|
import csv
|
2019-12-20 18:42:06 -05:00
|
|
|
from ruamel.yaml import YAML
|
2018-05-12 09:49:51 -04:00
|
|
|
import urllib3
|
2018-08-13 15:46:00 -04:00
|
|
|
import os
|
2018-06-03 14:26:58 -04:00
|
|
|
import sys
|
2018-06-19 18:24:15 -04:00
|
|
|
from io import StringIO
|
2018-05-12 09:49:51 -04:00
|
|
|
import requests
|
2018-08-16 14:01:42 -04:00
|
|
|
from hid.DoorController import DoorController
|
2018-05-12 09:49:51 -04:00
|
|
|
|
2019-12-20 18:42:06 -05:00
|
|
|
from passwords import (DOOR_USERNAME, DOOR_PASSWORD,
|
|
|
|
MEMBERSHIPWORKS_USERNAME, MEMBERSHIPWORKS_PASSWORD)
|
2018-05-12 09:49:51 -04:00
|
|
|
|
|
|
|
# it's fine, ssl certs are for losers anyway
|
|
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
|
2018-08-16 12:52:25 -04:00
|
|
|
try:
|
2019-12-20 18:42:06 -05:00
|
|
|
with open(os.path.dirname(os.path.abspath(__file__)) + "/config.yaml") as f:
|
|
|
|
config = YAML().load(f)
|
2018-08-16 12:52:25 -04:00
|
|
|
except NameError:
|
2019-12-20 18:42:06 -05:00
|
|
|
with open("config.yaml") as f:
|
|
|
|
config = YAML().load(f)
|
2018-05-14 22:36:24 -04:00
|
|
|
|
2018-08-16 14:01:42 -04:00
|
|
|
doors = {doorName: DoorController(doorData['ip'],
|
|
|
|
DOOR_USERNAME, DOOR_PASSWORD,
|
|
|
|
name=doorName, access=doorData['access'])
|
|
|
|
for doorName, doorData in config["doors"].items()}
|
|
|
|
|
2019-12-20 18:42:06 -05:00
|
|
|
memberLevels = config['memberLevels']
|
2018-05-12 09:49:51 -04:00
|
|
|
|
2018-08-07 18:33:12 -04:00
|
|
|
def getMembershipworksData(folders, columns):
|
2018-06-19 18:24:15 -04:00
|
|
|
""" Pull the members csv from the membershipworks api
|
2018-08-07 18:33:12 -04:00
|
|
|
folders: a list of the names of the folders to get
|
|
|
|
(see folder_map in this function for mapping to ids)
|
2018-06-19 18:24:15 -04:00
|
|
|
columns: which columns to get"""
|
|
|
|
BASE_URL = "https://api.membershipworks.com/v1/"
|
2018-08-07 18:33:12 -04:00
|
|
|
folder_map = {'members': '5ae37979f033bfe8534f8799',
|
|
|
|
'staff': '5771675edcdf126302a2f6b9',
|
|
|
|
'misc': '5b69ee9bf033bf8e7346c434'}
|
2018-06-19 18:24:15 -04:00
|
|
|
|
|
|
|
# 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)
|
2018-09-18 14:02:58 -04:00
|
|
|
login_data = r.json()
|
2018-06-19 18:24:15 -04:00
|
|
|
|
|
|
|
# get list of member/staff IDs
|
|
|
|
r = requests.get(BASE_URL + "ylp",
|
2018-09-18 14:02:58 -04:00
|
|
|
params={"SF": login_data['SF'],
|
2018-08-07 18:33:12 -04:00
|
|
|
"lbl": ",".join([folder_map[f] for f in folders]),
|
2018-09-18 14:02:58 -04:00
|
|
|
"org": login_data['org'],
|
2018-06-19 18:24:15 -04:00
|
|
|
"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)
|
2018-09-18 14:02:34 -04:00
|
|
|
ids = [user['uid'] for user in r.json()['usr']]
|
2018-06-19 18:24:15 -04:00
|
|
|
|
|
|
|
# get members CSV
|
|
|
|
# TODO: maybe can just use previous get instead? would return JSON
|
|
|
|
r = requests.post(BASE_URL + "csv",
|
2018-09-18 14:02:58 -04:00
|
|
|
params={"SF": login_data['SF']},
|
2018-06-19 18:24:15 -04:00
|
|
|
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)))
|