-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpinball_query.py
More file actions
150 lines (120 loc) · 4.69 KB
/
pinball_query.py
File metadata and controls
150 lines (120 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
"""
Pinball Map API Query Tool
Query pinball tables at a specific location in Washington State
"""
import requests
import json
import sys
from typing import Optional, Dict, List
BASE_URL = "https://pinballmap.com/api/v1"
# Washington is split into regions: Seattle (3) and Spokane (14)
WA_REGIONS = ["seattle", "spokane"]
def get_all_wa_locations() -> List[Dict]:
"""
Fetch all locations from Washington regions
"""
all_locations = []
try:
for region in WA_REGIONS:
url = f"{BASE_URL}/region/{region}/locations.json"
response = requests.get(url)
response.raise_for_status()
locations = response.json().get("locations", [])
all_locations.extend(locations)
return all_locations
except requests.RequestException as e:
print(f"Error fetching locations: {e}", file=sys.stderr)
return []
def search_locations(location_name: str, locations: List[Dict]) -> Optional[Dict]:
"""
Search for a location by name from the provided list
Case-insensitive partial matching
"""
search_term = location_name.lower()
# First try exact match (case-insensitive)
for loc in locations:
if loc.get("name", "").lower() == search_term:
return loc
# Then try substring match
for loc in locations:
if search_term in loc.get("name", "").lower():
return loc
return None
def get_location_machines(location_id: int) -> Optional[List[Dict]]:
"""
Get all machines (pinball tables) at a specific location
Uses the location_machine_xrefs endpoint which is more reliable
"""
all_machines = []
try:
for region in WA_REGIONS:
url = f"{BASE_URL}/region/{region}/location_machine_xrefs.json"
response = requests.get(url)
response.raise_for_status()
xrefs = response.json().get("location_machine_xrefs", [])
# Filter for the specific location
for xref in xrefs:
if xref.get("location_id") == location_id:
machine = xref.get("machine", {})
all_machines.append({
"machine_name": machine.get("name"),
"machine_year": machine.get("year"),
"machine_manufacturer": machine.get("manufacturer"),
"machine_type": machine.get("machine_type"),
})
return all_machines
except requests.RequestException as e:
print(f"Error fetching machines: {e}", file=sys.stderr)
return None
def format_machines_output(machines: List[Dict], location_name: str) -> str:
"""
Format machine data for display
"""
if not machines:
return f"No pinball tables found at {location_name}"
output = f"\nPinball tables at {location_name}:\n"
output += "=" * 50 + "\n"
for i, machine in enumerate(machines, 1):
name = machine.get("machine_name", "Unknown")
year = machine.get("machine_year", "Unknown")
manufacturer = machine.get("machine_manufacturer", "Unknown")
output += f"{i}. {name}\n"
output += f" Year: {year} | Manufacturer: {manufacturer}\n"
output += "=" * 50 + f"\nTotal: {len(machines)} tables\n"
return output
def main():
if len(sys.argv) < 2:
print("Usage: python pinball_query.py <location_name>")
print("Example: python pinball_query.py 'Ice Box Arcade'")
sys.exit(1)
location_name = " ".join(sys.argv[1:])
print(f"Searching for '{location_name}' in Washington...")
print("Fetching locations from Seattle and Spokane regions...")
# Fetch all Washington locations
locations = get_all_wa_locations()
if not locations:
print("Failed to fetch locations from API", file=sys.stderr)
sys.exit(1)
# Search for the location
location = search_locations(location_name, locations)
if not location:
print(f"Location '{location_name}' not found in Washington")
print("\nTip: Try searching with partial names. Available locations include:")
# Show first 10 locations as suggestions
for loc in locations[:10]:
print(f" - {loc.get('name')}")
sys.exit(1)
location_id = location.get("id")
actual_name = location.get("name", location_name)
print(f"Found: {actual_name}")
print("Fetching machines...")
# Get machines at the location
machines = get_location_machines(location_id)
if machines is None:
print("Failed to fetch machines", file=sys.stderr)
sys.exit(1)
# Display results
print(format_machines_output(machines, actual_name))
if __name__ == "__main__":
main()