-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (108 loc) · 3.31 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
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from tabulate import tabulate
options = Options()
options.headless = True
browser = webdriver.Firefox(options=options)
week = 15
print("<h2>Week "+str(week)+"</h2>")
browser.get("https://fantasy.espn.com/basketball/league/scoreboard?leagueId=73608366&matchupPeriodId="+str(week))
# 2D array for all team stats
teams = []
# Wait for stats to load
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "tr.Table__TR--sm")))
# Loop over teams and store stats
for team in browser.find_elements(By.CSS_SELECTOR, "tr.Table__TR--sm"):
stats = []
for stat in team.find_elements(By.CSS_SELECTOR, "td div"):
try:
stats.append(float(stat.text))
except:
stats.append(stat.text)
teams.append(stats)
browser.quit()
results = []
# Concat losses into string
def lostToString(arr):
string = ""
for i, defeat in enumerate(arr):
if i:
string += ", "+defeat
else:
string += defeat
return string
# Simulate matchups
for teamIndex in range(10):
team = teams[teamIndex]
matchups = teams.copy()
del matchups[teamIndex] # matchups = all teams without current team
wonMatchups = 0
lostTo = []
for matchup in matchups:
teamScore = 0
matchupScore = 0
ties = 0
for i in range(1, 10):
# Stat win
if team[i] > matchup[i]:
teamScore += 1
# Stat loss
elif team[i] < matchup[i]:
matchupScore += 1
else:
ties = ties + 1
# Team won
if teamScore > matchupScore:
wonMatchups += 1
# Tie, break with points stat
elif teamScore == matchupScore and team[9] > matchup[9]:
wonMatchups += 1
# Team lost
else:
lostTo.append(matchup[0])
results.append([team[0], wonMatchups, lostToString(lostTo)])
# Sort value handler
def sortByWins(arr):
return arr[1]
# Sort results array DESC by wins
results.sort(key=sortByWins, reverse=True)
# Print results in HTML friendly format
print("<h4>Record vs. all other teams</h4>")
print(tabulate(results, tablefmt='html', headers="firstrow"))
# Get stat leaders
stats = [["FG%", "FT%", "3PM", "3P%", "REB", "AST", "STL", "BLK", "PTS"], []]
# Loop over stats
for statIndex in range(1, 10):
statLeader = ""
topStat = float(0)
# Loop over teams
for team in teams:
# New stat leader
if team[statIndex] > topStat:
statLeader = team[0]
topStat = team[statIndex]
# Tie for stat leader
elif team[statIndex] == topStat:
statLeader += ", "+team[0]
stats[1].append(statLeader)
sortedStats = [[], [], [], [], [], [], [], [], [], []]
# Sort stats
for statIndex in range(1, 10):
currentStat = []
for team in teams:
currentStat.append([team[0], team[statIndex]])
currentStatSorted = sorted(currentStat, key=lambda x: -x[1])
sortedTeamNames = []
for team in currentStatSorted:
sortedTeamNames.append(team[0])
i=0
for team in sortedTeamNames:
sortedStats[i].append(team)
i=i+1
sortedStats.insert(0,["FG%", "FT%", "3PM", "3P%", "REB", "AST", "STL", "BLK", "PTS"])
print("<h4>Top stat performers</h4>")
#print(tabulate(stats, tablefmt='html', headers="firstrow"))
print(tabulate(sortedStats, tablefmt='html', headers="firstrow"))