-
Notifications
You must be signed in to change notification settings - Fork 1
/
getData.py
201 lines (150 loc) · 6.21 KB
/
getData.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
import asyncio
import json
import pandas as pd
import os
import urllib.request
import aiohttp
from datetime import datetime
from getLeagueResults import getSeasons
import requests
from understat import Understat
async def main():
async with aiohttp.ClientSession() as session:
url = 'https://understat.com/main/getPlayersStats/'
if(getNextGameWeek() < 6):
myobj = {'season': '2023','league':'epl'}
else:
myobj = {'season': '2023','league':'epl','date_start':getRangeStart(5)}
players = requests.post(url, data = myobj)
players = players.json()
players = players['response']['players']
players = json.dumps(players)
with open('tempfiles/understat.json', 'w') as file:
file.write(players)
def getNextGameWeek():
fpl = urllib.request.urlopen("https://fantasy.premierleague.com/api/bootstrap-static/").read()
fpl = json.loads(fpl)
fpl_players = fpl['elements']
_events = fpl['events']
for e in _events:
if(e['is_next']):
return e['id']
def getGameWeek(week):
with open('tempfiles/events.json', 'r') as all_games:
games = json.load(all_games)
return games[week]
def getFix():
with open('tempfiles/fixtures.json', 'r') as all_games:
games = json.load(all_games)
return games
def getRangeStart(form_range):
week = getNextGameWeek()
return getGameWeek(week-form_range)['deadline_time']
def fplFixtures(isAllSeason):
fpl = urllib.request.urlopen("https://fantasy.premierleague.com/api/fixtures/").read()
fpl = json.loads(fpl)
if not isAllSeason:
fixtures = [x for x in fpl if x['finished'] == False and x['kickoff_time'] != None]
results = json.dumps(fixtures)
with open('tempfiles/fixtures.json', 'w') as file:
file.write(results)
else:
results = json.dumps(fpl)
with open('tempfiles/all_fpl_fixtures.json', 'w') as file:
file.write(results)
def getFPL():
print("Get FPL")
fpl = urllib.request.urlopen("https://fantasy.premierleague.com/api/bootstrap-static/").read()
fpl = json.loads(fpl)
fpl_players = fpl['elements']
_events = fpl['events']
_events = json.dumps(_events)
with open('tempfiles/events.json', 'w') as file:
file.write(_events)
with open('tempfiles/fpl_players.json', 'w') as file:
file.write(json.dumps(fpl_players))
jsonTocsv = pd.read_json('tempfiles/fpl_players.json')
jsonTocsv.to_csv('tempfiles/fpl_players.csv',index=False)
def getLeague(league):
fpl = urllib.request.urlopen("https://fantasy.premierleague.com/api/leagues-classic/"+ str(league) + "/standings/").read()
fpl = json.loads(fpl)
with open('tempfiles/botsvsbots.json','r') as main_pl:
entries = json.load(main_pl)
if(entries != []):
if(entries[-1]["last_updated_data"] != fpl['last_updated_data']):
entries.append(fpl)
with open('tempfiles/botsvsbots.json', 'w') as file:
file.write(json.dumps(entries))
else:
with open('tempfiles/botsvsbots.json', 'w') as file:
file.write(json.dumps([fpl]))
def getUnderStat():
print("Get Understat")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
def mergeSets():
print("Merge Datasets")
fpl = pd.read_json('tempfiles/fpl_players.json')
unders = pd.read_json('tempfiles/understat.json')
fpl.rename(columns={'id': 'fpl_id'}, inplace=True)
unders.rename(columns={'id':'understat_id'},inplace=True)
fpl.to_csv('tempfiles/pl_all_players.csv',index=False)
unders.to_csv('tempfiles/under_all_players.csv',index=False)
keys = pd.read_csv('keys_pl_understat.csv', low_memory=False)
main_table = pd.merge(fpl,keys, on='fpl_id',how='inner')
main_table = pd.merge(main_table,unders, on='understat_id',how='inner')
#main_table = pd.merge(main_table,pos, on='fpl_id',how='inner')
#player_teams = pd.merge(keys,unders,on='understat_id', how='inner')
#player_teams.to_csv('teamshelper.csv',index=False)
main_table.rename(columns={'team_x':'team'},inplace=True)
main_table.to_csv('tempfiles/maindata.csv',index=False)
main_table.to_json('tempfiles/maindata.json',orient='records')
keys.to_json('keys_pl_understat.json',orient='records')
def findMissingPlayers():
with open('tempfiles/fpl_players.json','r') as fpl:
all_players = json.load(fpl)
with open('keys_pl_understat.json','r') as main_pl:
keys = json.load(main_pl)
for k in keys:
if(k['understat_id'] != None):
k['understat_id'] = round(k['understat_id'])
if(k['fpl_id'] != None):
k['fpl_id'] = round(k['fpl_id'])
missingPL = []
for p in all_players:
if(next((x for x in keys if x['fpl_id'] == p['id']), None) == None):
missingPL.append(p)
with open('tempfiles/understat.json','r') as fpl:
all_players = json.load(fpl)
missingUnder = []
for p in all_players:
if(next((x for x in keys if str(x['understat_id']) == str(p['id'])), None) == None):
missingUnder.append(p)
with open('tempfiles/missing_pl.json', 'w') as file:
file.write(json.dumps(missingPL))
with open('tempfiles/missing_under.json', 'w') as file:
file.write(json.dumps(missingUnder))
fpl = pd.read_json('tempfiles/missing_pl.json')
unders = pd.read_json('tempfiles/missing_under.json')
fpl.to_csv('tempfiles/missing_fpl.csv',index=False)
unders.to_csv('tempfiles/missing_under.csv',index=False)
def plAllToJson():
print("pl_all_players to json")
cs = pd.read_csv('tempfiles/pl_all_players.csv')
cs.to_json('tempfiles/pl_all_players.json',orient='records')
def getFixtures(week):
print("Get FPL Fixtures")
games = urllib.request.urlopen("https://fantasy.premierleague.com/api/fixtures/").read()
games = json.loads(games)
games = list(filter(lambda g: g['event'] == week, games))
with open('tempfiles/fixtures.json', 'w') as file:
file.write(json.dumps(games))
def getData(week):
getSeasons('EPL','2024')
getFPL()
getUnderStat()
mergeSets()
getFixtures(week)
plAllToJson()
#week = 26
#getData(week)