-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_scripts.py
More file actions
180 lines (129 loc) · 6.16 KB
/
populate_scripts.py
File metadata and controls
180 lines (129 loc) · 6.16 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
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
import os
from flask import Flask, render_template, request, flash, redirect, session, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from sqlalchemy.exc import IntegrityError
from secrets import API_KEY_SECRETS_FILE
import requests
from datetime import datetime
from models import db, connect_db, User, Bio, Prediction_top, Prediction_bottom, Prediction_manager, Team, Season_league, Team_info, Results_all, Results_home, Results_away, League_standing, Fixture
app = Flask(__name__)
API_BASE_URL = "https://api-football-v1.p.rapidapi.com/v2"
app.config['SQLALCHEMY_DATABASE_URI'] = (
os.environ.get('DATABASE_URL', 'postgres:///matchday'))
app.config['API_KEY'] = (os.environ.get('API_KEY', API_KEY_SECRETS_FILE))
API_KEY = app.config['API_KEY']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', "it's a secret")
toolbar = DebugToolbarExtension(app)
LEAGUE2020 = 2790
LEAGUE2021 = 3456
LEAGUE_ID = LEAGUE2021
connect_db(app)
def populate_season_league_table():
"""Populate table for season, league_id, league name for England"""
stored_data = Season_league.query.all()
for data in stored_data:
db.session.delete(data)
db.session.commit()
url = "https://api-football-v1.p.rapidapi.com/v2/leagues/search/england"
headers = {
'x-rapidapi-key': API_KEY,
'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
data = response.json()
for info in data['api']['leagues']:
season_table = Season_league(season = info['season'], league_id = info['league_id'], league_name = info['name'], country = info['country'])
db.session.add(season_table)
db.session.commit()
def populate_team_table():
"""Populate table for all teams of country England"""
url = "https://api-football-v1.p.rapidapi.com/v2/teams/search/england"
headers = {
'x-rapidapi-key': API_KEY,
'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
data = response.json()
for info in data['api']['teams']:
team_table = Team(team_id = info['team_id'], team_name = info['name'])
db.session.add(team_table)
db.session.commit()
def populate_team_info_table():
""" Populate table with data about each team in England
"""
stored_data = Team_info.query.all()
for data in stored_data:
db.session.delete(data)
db.session.commit()
url = "https://api-football-v1.p.rapidapi.com/v2/teams/search/england"
headers = {
'x-rapidapi-key': API_KEY,
'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
data = response.json()
for info in data['api']['teams']:
team_info_table = Team_info(team_name = info['name'], team_id = info['team_id'], logo = info['logo'], venue_name = info['venue_name'], venue_address = info['venue_address'], venue_city = info['venue_city'], venue_capacity = info['venue_capacity'])
db.session.add(team_info_table)
db.session.commit()
def populate_standings_table():
"""populate prem league table. Prem league id for 2020 = 2790"""
stored_data = League_standing.query.all()
for data in stored_data:
db.session.delete(data)
db.session.commit()
url = f"https://api-football-v1.p.rapidapi.com/v2/leagueTable/{LEAGUE_ID}"
headers = {
'x-rapidapi-key': API_KEY,
'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
data = response.json()
for info in data['api']['standings'][0]:
league_table = League_standing(team_id = info['team_id'], rank = info['rank'], logo=info['logo'], form = info['forme'], points = info['points'], goalsDiff = info['goalsDiff'])
db.session.add(league_table)
db.session.commit()
def populate_results_all_table():
"""populate results table for all games. Prem league id for 2020 = 2790"""
stored_data = Results_all.query.all()
for data in stored_data:
db.session.delete(data)
db.session.commit()
url = f"https://api-football-v1.p.rapidapi.com/v2/leagueTable/{LEAGUE_ID}"
headers = {
'x-rapidapi-key': API_KEY,
'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
data = response.json()
for info in data['api']['standings'][0]:
results_all_table = Results_all(team_id = info['team_id'], matches_played = info['all']['matchsPlayed'], win = info['all']['win'], draw = info['all']['draw'], lose = info['all']['lose'], goals_for = info['all']['goalsFor'], goals_against = info['all']['goalsAgainst'])
db.session.add(results_all_table)
db.session.commit()
def populate_fixtures():
"""populate upcoming fixture table for all games. Prem league id for 2020 = 2790"""
stored_data = Fixture.query.all()
for data in stored_data:
db.session.delete(data)
db.session.commit()
url = f"https://api-football-v1.p.rapidapi.com/v2/fixtures/league/{LEAGUE_ID}/next/5"
headers = {
'x-rapidapi-key': API_KEY,
'x-rapidapi-host': "api-football-v1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
data = response.json()
for info in data['api']['fixtures']:
upcoming_games = Fixture(event_date = info['event_date'], venue = info['venue'], referee = info['referee'], homeTeam = info['homeTeam']['team_name'], homeTeam_id = info['homeTeam']['team_id'], homeTeam_logo = info['homeTeam']['logo'], awayTeam = info['awayTeam']['team_name'], awayTeam_id = info['awayTeam']['team_id'], awayTeam_logo = info['awayTeam']['logo'])
db.session.add(upcoming_games)
db.session.commit()
#scripts to run
# populate_season_league_table()
# populate_team_table()
# populate_team_info_table()
# populate_standings_table()
# populate_results_all_table()
# populate_fixtures()