I work for a non-profit that runs acoustic music camps for adults. This script queries the API for Regfox, the event registration software we use, and prints for each camp a table showing the number of spots available for each of the accommodation types we offer.
All comments appreciated, especially how to make the code more pythonic.
import requests
from collections import namedtuple
import credentials # local file
# =========================================================================
# THIS INFO CHANGES AT START OF EACH CAMP SEASON, OTHERWISE IMMUTABLE
# ------------------------------------------------------------------------
CampInfo = namedtuple("CampInfo", ["full_name", "regfox_page_id"])
MANDO_CAMP = CampInfo("2023 Mandolin Camp North", credentials.MC_REGFOX_PAGE_ID)
BANJO_CAMP = CampInfo("2023 Banjo Camp North", credentials.BC_REGFOX_PAGE_ID)
ColumnWidth = namedtuple("ColumnWidth", ["name", "width"])
name = ColumnWidth("Name", 47)
supply = ColumnWidth("Supply", 7) # these three are number of chars + 1
sold = ColumnWidth("Sold", 5)
available = ColumnWidth("Available", 10)
ROOM_TYPES = (
"Single room, private bath, Hilltop campus",
"Single room, shared bath, Pondside campus",
"Double room, private bath, Hilltop campus",
"Double room, shared bath, Pondside campus",
"Quad room, shared bath, Pondside campus",
"Double room, [NON-PARTICIPATING CAMPER], private bath, Hilltop campus",
"Double room, [NON-PARTICIPATING CAMPER], shared bath, Pondside campus",
"Commuter / RV",
"Commuter / RV, [NON-PARTICIPATING CAMPER]",
"Friday",
"Saturday",
"Sunday",
"Friday evening",
"Saturday evening",
)
# =========================================================================
def print_row(a, b, c, d):
"""Print a row in a table"""
for text, align, width in zip(
[a, b, c, d],
"<>>>",
[name.width, supply.width, sold.width, available.width],
):
print(
"{text:{align}{width}}".format(text=text, align=align, width=width),
end="",
)
print()
def print_header(camp):
"""Print a header for a camp"""
separator_length = 69
print()
print("=" * separator_length)
print(camp.full_name)
print("-" * separator_length)
print_row("Room type", "Supply", "Sold", "Available")
print("-" * separator_length)
def get_inventory(regfox_page_id):
"""Get data for each kind of room via Regfox API"""
url_string = (
"https://api.webconnex.com/v2/public/forms/" + regfox_page_id + "/inventory"
)
try:
r = requests.get(
url=url_string,
headers={
"apiKey": credentials.API_KEY,
},
)
return r.json()
except requests.exceptions.RequestException:
print("HTTP Request failed")
def create_table(camp):
"""Create a table for one camp"""
inventory_raw = get_inventory(camp.regfox_page_id)
inventory = inventory_raw["data"]
for room_type in ROOM_TYPES:
for i in inventory:
if i["name"] == room_type:
available = i["quantity"] - i["sold"]
# shorten long names
if "NON-PARTICIPATING" in i["name"]:
i["name"] = i["name"].replace(
"[NON-PARTICIPATING CAMPER]", "NON-PART"
)
i["name"] = i["name"].replace(" campus", "")
print_row(i["name"], i["quantity"], i["sold"], available)
def main():
for camp in [MANDO_CAMP, BANJO_CAMP]:
print_header(camp)
create_table(camp)
print()
if __name__ == main():
main()