-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
228 lines (154 loc) · 5.94 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
import os
import logging
import requests
import psycopg2
import pandas as pd
from dotenv import load_dotenv
from requests.exceptions import HTTPError, RequestException, Timeout
# Load the environment variables
load_dotenv()
API_KEY = os.getenv("API_KEY")
API_HOST = os.getenv("API_HOST")
LEAGUE_ID = os.getenv("LEAGUE_ID")
SEASON = os.getenv("SEASON")
DB_NAME = os.getenv("DB_NAME")
DB_USERNAME = os.getenv("DB_USERNAME")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_HOST = os.getenv("DB_HOST")
DB_PORT = os.getenv("DB_PORT")
# Set up the logger
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# Create a file handler
file_handler = logging.FileHandler('football_table_standings.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
# Create a console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
# Instantiate the logger object
logger = logging.getLogger()
# Add the file handler to the logger
logger.addHandler(file_handler)
# Add the console handler to the logger
logger.addHandler(console_handler)
url = "https://api-football-v1.p.rapidapi.com/v3/standings"
headers = {"X-RapidAPI-Key": API_KEY, "X-RapidAPI-Host": API_HOST}
query_string = {'season': SEASON,'league': LEAGUE_ID}
# response = requests.request("GET", url, headers=headers, params=query_string)
try:
api_response = requests.get(url, headers=headers, params=query_string, timeout=15)
api_response.raise_for_status()
except HTTPError as http_err:
logger.error(f'HTTP error occurred: {http_err}')
except Timeout:
logger.error('Request timed out after 15 seconds')
except RequestException as request_err:
logger.error(f'Request error occurred: {request_err}')
standings_data = api_response.json()['response']
# print(standings_data)
# Extracting the standings information
standings = standings_data[0]['league']['standings'][0]
# List to hold our extracted data
data_list = []
# Parsing through each team's standing
for team_info in standings:
rank = team_info['rank']
team_name = team_info['team']['name']
played = team_info['all']['played']
win = team_info['all']['win']
draw = team_info['all']['draw']
lose = team_info['all']['lose']
goals_for = team_info['all']['goals']['for']
goals_against = team_info['all']['goals']['against']
goals_diff = team_info['goalsDiff']
points = team_info['points']
data_list.append([rank, team_name, played, win, draw, lose, goals_for, goals_against, goals_diff, points])
# Create the dataFrame
columns = ['P', 'Team', 'GP', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts']
standings_df = pd.DataFrame(data_list, columns=columns)
# Display the dataFrame
print(standings_df.to_string(index=False))
# Set up Postgres database connection
postgres_connection = psycopg2.connect(
dbname=DB_NAME,
user=DB_USERNAME,
password=DB_PASSWORD,
host=DB_HOST,
port=DB_PORT
)
# Get a cursor from the database
cur = postgres_connection.cursor()
# Use SQL to create a table for the Premier League
create_table_sql_query = """
CREATE TABLE IF NOT EXISTS premier_league_standings_tbl (
position INT PRIMARY KEY,
team VARCHAR(255),
games_played INTEGER,
wins INTEGER,
draws INTEGER,
losses INTEGER,
goals_for INTEGER,
goals_against INTEGER,
goal_difference INTEGER,
points INTEGER
);
"""
# Run the SQL query
cur.execute(create_table_sql_query)
# Commit the transaction
postgres_connection.commit()
# Use SQL to insert data into the Premier League table
insert_data_sql_query = """
INSERT INTO public.premier_league_standings_tbl (
position, team, games_played, wins, draws, losses, goals_for, goals_against, goal_difference, points
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON CONFLICT (position) DO UPDATE SET
team = EXCLUDED.team,
games_played = EXCLUDED.games_played,
wins = EXCLUDED.wins,
draws = EXCLUDED.draws,
losses = EXCLUDED.losses,
goals_for = EXCLUDED.goals_for,
goals_against = EXCLUDED.goals_against,
goal_difference = EXCLUDED.goal_difference,
points = EXCLUDED.points
"""
for idx, row in standings_df.iterrows():
cur.execute(insert_data_sql_query,
(row['P'],
row['Team'],
row['GP'],
row['W'],
row['D'],
row['L'],
row['F'],
row['A'],
row['GD'],
row['Pts'])
)
# Commit the transaction
postgres_connection.commit()
# Use SQL to create a view that updates the table rankings
create_ranked_standings_view_sql_query = """
CREATE OR REPLACE VIEW public.premier_league_standings_vw AS
SELECT
RANK() OVER (ORDER BY points DESC, goal_difference DESC, goals_for DESC) as position
,team
,games_played
,wins
,draws
,losses
,goals_for
,goals_against
,goal_difference
,points
FROM
public.premier_league_standings_tbl;
"""
# Run the SQL query
cur.execute(create_ranked_standings_view_sql_query)
# Commit the transaction
postgres_connection.commit()