-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
143 lines (114 loc) · 4.74 KB
/
Copy pathmain.py
File metadata and controls
143 lines (114 loc) · 4.74 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
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
import requests
import os
# Load environment variables from a .env file if present (for local development)
try:
from dotenv import load_dotenv # type: ignore
load_dotenv()
except Exception:
# python-dotenv may not be installed in some environments (e.g., managed runtime)
pass
# API keys are read from environment variables
TICKETMASTER_API_KEY = os.getenv("TICKETMASTER_API_KEY")
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
IPINFO_API_KEY = os.getenv("IPINFO_API_KEY")
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
@app.route('/')
def index():
"""Serves the main HTML file for the application (the front end).
This function is necessary to resolve the 404 error when accessing the
root URL (/). It expects 'events.html' to be in a directory named 'templates'.
"""
return render_template('events.html')
@app.get("/search")
def search_events():
"""Proxy to Ticketmaster Event Search API.
Query params: keyword, radius, unit, geoPoint, segmentId
Returns the full JSON from Ticketmaster.
"""
base_url = "https://app.ticketmaster.com/discovery/v2/events.json"
# Collect only the allowed parameters
if not TICKETMASTER_API_KEY:
return jsonify({"error": "Server misconfiguration: TICKETMASTER_API_KEY is not set."}), 500
params = {
"apikey": TICKETMASTER_API_KEY,
}
# Map expected query params directly if provided
for key in ["keyword", "radius", "unit", "geoPoint", "segmentId"]:
val = request.args.get(key)
if val is not None and val != "":
params[key] = val
try:
tm_resp = requests.get(base_url, params=params, timeout=15)
tm_resp.raise_for_status()
data = tm_resp.json()
return jsonify(data), tm_resp.status_code
except requests.exceptions.RequestException as e:
# Note: 502 Bad Gateway is appropriate here for a failed proxy request
return jsonify({"error": str(e)}), 502
@app.get("/event-details/<event_id>")
def event_details(event_id: str):
"""Proxy to Ticketmaster Event Details API.
Path param: event_id
Returns the full JSON from Ticketmaster.
"""
if not TICKETMASTER_API_KEY:
return jsonify({"error": "Server misconfiguration: TICKETMASTER_API_KEY is not set."}), 500
base_url = f"https://app.ticketmaster.com/discovery/v2/events/{event_id}"
params = {"apikey": TICKETMASTER_API_KEY}
try:
tm_resp = requests.get(base_url, params=params, timeout=15)
tm_resp.raise_for_status()
data = tm_resp.json()
return jsonify(data), tm_resp.status_code
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 502
@app.get("/venue-details")
def venue_details():
"""Proxy to Ticketmaster Venue Search API.
Query param: keyword (venue name)
Returns the full JSON from Ticketmaster.
"""
base_url = "https://app.ticketmaster.com/discovery/v2/venues"
keyword = request.args.get("keyword", default="")
if not TICKETMASTER_API_KEY:
return jsonify({"error": "Server misconfiguration: TICKETMASTER_API_KEY is not set."}), 500
params = {
"apikey": TICKETMASTER_API_KEY,
}
if keyword:
params["keyword"] = keyword
try:
tm_resp = requests.get(base_url, params=params, timeout=15)
tm_resp.raise_for_status()
data = tm_resp.json()
return jsonify(data), tm_resp.status_code
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 502
@app.get("/geocode")
def geocode():
"""Proxy to Google Geocoding API using server-side API key.
Query param: address
Returns minimal JSON: { location: { lat, lng }, raw: <full google response> }
"""
if not GOOGLE_API_KEY:
return jsonify({"error": "Server misconfiguration: GOOGLE_API_KEY is not set."}), 500
address = request.args.get("address", "")
if not address:
return jsonify({"error": "Missing required query parameter 'address'"}), 400
url = "https://maps.googleapis.com/maps/api/geocode/json"
try:
resp = requests.get(url, params={"address": address, "key": GOOGLE_API_KEY}, timeout=15)
resp.raise_for_status()
data = resp.json()
first = (data.get("results") or [{}])[0]
geometry = first.get("geometry", {})
location = geometry.get("location")
return jsonify({"location": location, "raw": data}), resp.status_code
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 502
if __name__ == "__main__":
# Run development server; in production use a WSGI server
app.run(host="0.0.0.0", port=5000, debug=True)