-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacts.py
245 lines (212 loc) · 8.18 KB
/
facts.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
import json
import glob
import itertools
import datetime
import time
import dateutil.parser
import os
import requests
import logging
from pathlib import Path
from modernblaseball.modern_blaseball import blaseball_api
import pprint
KNOWN_NUMBER_OF_SEASONS = 2
GET_PLAYER_DATA = False
REPORT_DATA_RETREVIAL = True
blaseball = blaseball_api()
def mungeFuncName(name):
name = name.replace(" ", "_")
name = name.replace("(", "")
name = name.replace(")", "")
name = name.replace(",", "")
return name
def storeData(data, id):
now_timestamp = str(datetime.datetime.now().timestamp())
if data == []:
return data
id = mungeFuncName(id)
Path(f'data/{KNOWN_NUMBER_OF_SEASONS:05d}').mkdir(parents=True, exist_ok=True)
with open(f'data/{KNOWN_NUMBER_OF_SEASONS:05d}/data_{id}_{now_timestamp}.json', 'w') as f:
json.dump(data, f)
return data
def retrieveData(id, args=None):
if args != None:
id = id + "_" + str(args)
id = mungeFuncName(id)
files_found = glob.glob(f"data/data_{id}_*")
try:
most_recent_file = max(files_found, key=os.path.getctime)
except ValueError as err:
# no files found
logging.warning(err)
return None
data = None
with open(most_recent_file) as f:
data = json.load(f)
return data
def getMainData():
funcs = [blaseball.get_global_events,
blaseball.get_all_divisions,
blaseball.get_offseason_setup,
blaseball.get_all_teams,
blaseball.get_simulation_data]
for func in funcs:
func_name = str(func.__name__)[4:]
if REPORT_DATA_RETREVIAL:
print(func_name)
request_result = func()
if REPORT_DATA_RETREVIAL:
print(request_result.status_code)
if request_result.status_code == 200:
print(request_result)
print(request_result.text)
data = json.loads(request_result.text)
storeData(data, func_name)
def getSpecificData(func, args=None):
func_name = str(func.__name__)[4:]
request_result = None
if args != None:
func_name = func_name + "_" + str(args)
func_name = mungeFuncName(func_name)
if REPORT_DATA_RETREVIAL:
print(func_name, args)
request_result = blaseball.failover_500(func, args)
#request_result = func(args)
#print(request_result.status_code)
data = None
if request_result.status_code == 200:
#print(request_result)
if request_result.text == "":
if REPORT_DATA_RETREVIAL:
print("\t* * * EMPTY RESULT * * *")
return data
#print(request_result.text)
data = json.loads(request_result.text)
storeData(data, func_name)
return data
def retrieveSpecificData(func, args=None):
func_name = str(func.__name__)[4:]
request_result = None
if args != None:
func_name = func_name + "_" + str(args)
func_name = mungeFuncName(func_name)
request_result = retrieveData(func_name)
if request_result == None:
return getSpecificData(func, args=args)
return request_result
def get_GamesOnDay(which_game):
game_season, game_day = which_game
URL_BASE = 'https://blaseball.com'
#print("get_GamesOnDay", game_day, game_season)
res = requests.get(URL_BASE + '/database/games', params={'day': game_day, 'season': game_season})
if res.text[:9] == "<!doctype":
print ("NOT FOUND")
return res
def getSeasonInfo():
all_divisions = retrieveData("all_divisions")
offseason_setup = retrieveData("offseason_setup")
offseason_recap = retrieveData("offseason_recap")
all_teams = retrieveData("all_teams")
simulation_data = retrieveData("simulation_data")
for n in range(KNOWN_NUMBER_OF_SEASONS):
getSpecificData(blaseball.get_season, [n])
getSpecificData(blaseball.get_offseason_recap, [n])
getSpecificData(blaseball.get_playoff_details, [n])
def getAllPlayerData():
all_teams = retrieveData("all_teams")
print(all_teams)
for t in all_teams:
pprint.pprint(t)
#team_data = getSpecificData(blaseball.get_team, t["_id"])
pprint.pprint(t["fullName"])
player_groups = ["bench", "bullpen", "rotation", "lineup"]
for g in player_groups:
print(g)
print(t[g])
for p in t[g]:
print(".", end="", flush=True)
getSpecificData(blaseball.get_player_stats, p)
time.sleep(0.4)
def getTodaysGames(game_season, game_day, retrieve=False):
if retrieve:
return retrieveSpecificData(get_GamesOnDay, (game_season, game_day))
return getSpecificData(get_GamesOnDay, (game_season, game_day))
book_of_facts = {}
def makeFact(key, val):
book_of_facts.update({key: val})
def getFact(key):
return book_of_facts[key]
def showAllFacts():
for k,v in book_of_facts.items():
print(f"{k}\t{v}")
def storeAllFacts():
storeData(data, "book_of_facts")
def retrieveAllFacts():
return retrieveData(data, "book_of_facts")
def reloadAllFactsFromDisk():
book_of_facts = retrieveAllFacts()
def makeFacts():
global_ticker = retrieveData("global_ticker")
all_divisions = retrieveData("all_divisions")
offseason_setup = retrieveData("offseason_setup")
offseason_recap = retrieveData("offseason_recap")
all_teams = retrieveData("all_teams")
simulation_data = retrieveData("simulation_data")
# Time
now_time = datetime.datetime.now().timestamp()
next_phase_time = dateutil.parser.parse(simulation_data["nextPhaseTime"]).timestamp()
makeFact(("time", "now"), now_time)
makeFact(("time", "nextPhaseTime"), next_phase_time)
makeFact(("time", "TimeUntilNextPhase"), next_phase_time - now_time)
if True:
pprint.pprint(global_ticker)
for msg in global_ticker:
makeFact(("global_ticker", msg["_id"]), msg["msg"])
pprint.pprint(all_divisions)
for d in all_divisions:
makeFact(("division", d["_id"], "name"), d["name"])
makeFact(("division", d["_id"], "teams"), d["teams"])
for t in d["teams"]:
makeFact(("team", t, "division", "id"), d["_id"])
makeFact(("team", t, "division", "name"), d["name"])
# TODO: index facts by team name too
# makeFact(["team", team_name, "division", "id"], d["_id"],)
# makeFact(["team", team_name, "division", "name"], d["name"],)
pprint.pprint(all_teams)
for t in all_teams:
pprint.pprint(t)
for k,v in t.items():
makeFact(("team", t["_id"], k), v)
makeFact(("team", t["fullName"], k), v)
player_roster_categories = ["bench", "bullpen", "rotation", "lineup"]
player_list = list(itertools.chain.from_iterable([t[prc] for prc in player_roster_categories]))
makeFact(("team", t["fullName"], "players"), player_list)
makeFact(("team", t["_id"], "players"), player_list)
for k,v in simulation_data.items():
makeFact(("simulation_data", k), v)
# get past seasons' games
#for n_season in range(KNOWN_NUMBER_OF_SEASONS + 1):
# get this season's games
n_season = KNOWN_NUMBER_OF_SEASONS
if True:
for n_game_day in range(120):
t_games = getTodaysGames(n_season, n_game_day, retrieve=True)
pprint.pprint(t_games)
today_game_ids = []
if t_games != None:
for game in t_games:
today_game_ids.append(game["_id"])
for k,v in game.items():
makeFact(("game", n_season, n_game_day, game["_id"], k), v)
makeFact(("game", n_season, n_game_day, "games"), today_game_ids)
storeAllFacts()
showAllFacts()
input()
#getSpecificData(blaseball.get_global_events)
#getMainData()
#getSeasonInfo()
if GET_PLAYER_DATA:
getAllPlayerData()
#tgames = getTodaysGames(2,0)
#pprint.pprint(tgames)
makeFacts()