-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (46 loc) · 1.85 KB
/
app.py
File metadata and controls
55 lines (46 loc) · 1.85 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
from flask import Flask, jsonify
import requests
import os
app = Flask(__name__)
# SerpAPI base URL and API key
SERP_API_URL = "https://serpapi.com/search.json"
SERP_API_KEY = os.getenv("SPORTS_API_KEY")
@app.route('/sports', methods=['GET'])
def get_nfl_schedule():
#Fetches the NFL schedule from SerpAPI and returns it as JSON
try:
# Query SerpAPI
params = {
"engine": "google",
"q": "nfl schedule",
"api_key": SERP_API_KEY
}
response = requests.get(SERP_API_URL, params=params)
response.raise_for_status()
data = response.json()
# Extract games from sports_results
games = data.get("sports_results", {}).get("games", [])
if not games:
return jsonify({"message": "No NFL schedule available.", "games": []}), 200
# Format the schedule into JSON
formatted_games = []
for game in games:
teams = game.get("teams", [])
if len(teams) == 2:
away_team = teams[0].get("name", "Unknown")
home_team = teams[1].get("name", "Unknown")
else:
away_team, home_team = "Unknown", "Unknown"
game_info = {
"away_team": away_team,
"home_team": home_team,
"venue": game.get("venue", "Unknown"),
"date": game.get("date", "Unknown"),
"time": f"{game.get('time', 'Unknown')} ET" if game.get("time", "Unknown") != "Unknown" else "Unknown"
}
formatted_games.append(game_info)
return jsonify({"message": "NFL schedule fetched successfully.", "games": formatted_games}), 200
except Exception as e:
return jsonify({"message": "An error occurred.", "error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)