-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
318 lines (270 loc) · 10.4 KB
/
main.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import pysmashgg
from geopy.geocoders import Nominatim
from geopy.exc import GeocoderUnavailable
from geopy.distance import geodesic
from datetime import datetime
from time import sleep
from flask import Flask, redirect, url_for, request
# Initialize Flask app
app = Flask(__name__)
future_tournaments = []
# Initialize the SmashGGPy class
smash = pysmashgg.SmashGG("be71a5a727a0ec1ff01980dffcfb2958")
geolocator = Nominatim(user_agent="MyApp")
# Function to fetch tournament information
def tournament_info(location, radius):
try:
address = geolocator.geocode(location)
page_num = 1
tournaments_by_radius = smash.tournament_show_by_radius(
f'{address.latitude},{address.longitude}', radius, page_num)
while True:
for x in tournaments_by_radius:
if datetime.fromtimestamp(int(x["startTimestamp"])) > datetime.now():
future_tournaments.append(x)
if len(future_tournaments) % len(tournaments_by_radius) == 0:
page_num += 1
tournaments_by_radius = smash.tournament_show_by_radius(
f'{address.latitude},{address.longitude}', '150mi', page_num)
else:
break
# Add a delay between requests to avoid overwhelming the SmashGG API
sleep(1)
except GeocoderUnavailable as e:
# Handle geocoding service being unavailable
print(f"GeocoderUnavailable: {e}")
# Route to display tournament results with pagination
@app.route('/results')
def display_tournaments():
result = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 20px auto;
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.title-container {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #4CAF50; /* Green */
color: white;
padding: 10px;
border-radius: 10px;
margin-bottom: 10px;
}
.title {
font-size: 24px;
font-weight: bold;
margin-bottom: 15px;
}
.tournament {
border: 3px solid black;
border-radius: 4%;
aspect-ratio: 1 / 0.9;
padding: 10px;
text-align: center;
margin-bottom: 10px;
background-color: #f9f9f9; /* Light gray */
}
a {
text-decoration: none;
color: inherit;
}
.back-button {
padding: 10px;
background-color: #3498db; /* Blue */
color: white;
text-align: center;
text-decoration: none;
border-radius: 5px;
}
.pagination {
display: flex;
justify-content: space-between;
margin-top: 10px;
}
.pagination button {
padding: 10px;
background-color: #3498db; /* Blue */
color: white;
text-align: center;
text-decoration: none;
border-radius: 5px;
cursor: pointer;
}
.pagination button:disabled {
background-color: #a5a5a5; /* Gray */
cursor: not-allowed;
}
</style>
<title>Nearest Tournaments</title>
</head>
<body>
<div class="container">
<div class="title-container">
<div class="title">Nearest Tournaments</div>
<a href="/" class="back-button">Back to Search</a>
</div>
'''
page_num = int(request.args.get('page', 1))
# Displays 10 tournaments per page.
tournaments_per_page = 10
start_index = (page_num - 1) * tournaments_per_page
end_index = start_index + tournaments_per_page
original_location = geolocator.geocode(request.args.get('location'))
for tournament in future_tournaments[start_index:end_index]:
time = datetime.fromtimestamp(int(tournament["startTimestamp"]))
try:
tournament_location = geolocator.geocode(tournament["city"] + ", " + tournament["state"], timeout=10)
result += '<div class="tournament">'
result += f'<h2>{tournament["name"]}</h2>'
result += f'<p><strong>Location:</strong> {tournament["city"]}, {tournament["state"]}</p>'
result += f'<p><strong>Entrants:</strong> {tournament["entrants"]}</p>'
result += f'<p><strong>Date:</strong> {time.strftime("%I:%M%p %m-%d-%Y")}</p>'
if tournament_location is not None:
tournament_latitude = float(tournament_location.latitude)
tournament_longitude = float(tournament_location.longitude)
distance = geodesic((original_location.latitude, original_location.longitude),
(tournament_latitude, tournament_longitude)).miles
result += f'<p><strong>Distance:</strong> {distance:.2f} miles</p>'
else:
result += '<p><strong>Distance:</strong> Not Available</p>'
result += f'<a href="https://www.start.gg/tournament/{tournament["slug"]}">More info</a>'
result += '</div>'
except GeocoderUnavailable as e:
# Handle geocoding service being unavailable
print(f"GeocoderUnavailable: {e}")
result += (
'<div class="tournament">'
f'<p><strong>Name:</strong> {tournament["name"]}</p>'
f'<p><strong>Location:</strong> {tournament["city"]}, {tournament["state"]}</p>'
f'<p><strong>Entrants:</strong> {tournament["entrants"]}</p>'
f'<p><strong>Date:</strong> {time.strftime("%I:%M%p %m-%d-%Y")}</p>'
'<p><strong>Distance:</strong> Not Available (Geocoder Unavailable)</p>'
'</div>'
)
# Add a delay between requests to avoid overwhelming the geocoding service
sleep(0.5)
# Add pagination section
result += f'''
<div class="pagination">
<form action="/results" method="get">
<input type="hidden" name="location" value="{request.args.get('location')}">
<input type="hidden" name="page" value="{page_num - 1}" {'' if page_num > 1 else 'disabled'}>
<button type="submit" {'' if page_num > 1 else 'disabled'}>Previous</button>
</form>
<form action="/results" method="get">
<input type="hidden" name="location" value="{request.args.get('location')}">
<input type="hidden" name="page" value="{page_num + 1}" {'' if (page_num + 1) * tournaments_per_page < len(future_tournaments) else 'disabled'}>
<button type="submit" {'' if (page_num + 1) * tournaments_per_page < len(future_tournaments) else 'disabled'}>Next</button>
</form>
</div>
</div>
</body>
</html>
'''
return result
# Route for the search page
@app.route('/', methods=['GET', 'POST'])
def search_page():
if request.method == 'POST':
try:
location = request.form['location']
radius = request.form['radius']
if not location or not radius:
return "Please enter both a location and a radius."
future_tournaments.clear()
tournament_info(location, radius)
return redirect(url_for('display_tournaments', location=location))
except Exception as e:
return f"An error occurred: {e}"
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
form {
background-color: #e6e6e6; /* Lighter gray */
padding: 20px;
border-radius: 10px;
max-width: 400px;
margin: auto;
}
.title {
text-align: center;
font-size: 24px;
font-weight: bold;
color: #333;
margin-bottom: 20px;
background-color: #3498db; /* Blue */
color: white;
padding: 10px;
border-radius: 10px;
}
label {
color: #8e44ad; /* Purple */
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
box-sizing: border-box;
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
input[type="submit"] {
width: 100%;
box-sizing: border-box;
border-radius: 5px;
padding: 10px;
background-color: #3498db; /* Blue */
color: white;
cursor: pointer;
}
@media (min-width: 768px) {
form {
max-width: 600px;
}
}
</style>
<title>Tournament Finder</title>
</head>
<body>
<div class="title">Tournament Finder</div>
<form method="post">
<label for="location">Location:</label>
<input type="text" id="location" name="location">
<label for="radius">Radius:</label>
<input type="text" id="radius" name="radius">
<input type="submit" value="Submit">
</form>
</body>
</html>
'''
# Run the Flask app
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)