-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
156 lines (133 loc) · 4.67 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
#!/usr/bin/env python3
import sys
import argparse
from pathlib import Path
from fantasy_stats import FantasyStats
import jinja2
from datetime import datetime
import logging
import json
def setup_logger():
formatter = logging.basicConfig(
level=logging.INFO,
style="{",
datefmt="%Y-%m-%d %H:%M:%S",
format="{asctime} {levelname} {filename}:{lineno}: {message}",
)
handler = logging.FileHandler("log.txt", mode="w")
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.addHandler(handler)
return logger
def main(
credentials: Path,
year: int,
qb: int,
rb: int,
wr: int,
te: int,
flex: int,
dst: int,
k: int,
):
# Creating a logger
logger = setup_logger()
outputfile = f"index_{args.year}.html"
title = f"Make Football Great Again Season {args.year} Stats"
# League API
try:
with open(credentials, "r") as f:
login = json.load(f)
except FileNotFoundError:
sys.exit(f"Credentials file {credentials.absolute()} not found")
fantasy_stats = FantasyStats(
league_id=login.get("league_id"),
year=args.year,
espn_s2=login.get("espn_s2"),
swid=login.get("swid"),
logger=logger,
qb=qb,
rb=rb,
wr=wr,
flex=flex,
dst=dst,
k=k,
)
# Get league overview
league_overview = fantasy_stats.get_league_overview()
# Compute points for and against
points_for, points_against = fantasy_stats.print_team_scoring()
# Find closest games
close_games = fantasy_stats.get_close_games()
# Get highest scoring team, yet still lost
high_score_and_lost = fantasy_stats.get_high_score_and_lost()
# Get lowest scoring team, yet won the game
low_score_and_won = fantasy_stats.get_low_score_and_won()
# Top 5 draft pick games
player_injuries = fantasy_stats.print_player_injuries()
team_injuries = fantasy_stats.print_missed_games_per_team()
# Analyze over- and under-performing playsers
over_expectation = fantasy_stats.get_expectation(False)
under_expectation = fantasy_stats.get_expectation(True)
# Get list of top players
top_qb = fantasy_stats.get_top_players("QB")
top_wr = fantasy_stats.get_top_players("WR")
top_rb = fantasy_stats.get_top_players("RB")
top_te = fantasy_stats.get_top_players("TE")
top_kick = fantasy_stats.get_top_players("K")
top_d = fantasy_stats.get_top_players("D/ST")
# Get perfect lineup
perfect_lineup = fantasy_stats.get_perfect_record()
now = datetime.now()
template = jinja2.Environment(loader=jinja2.FileSystemLoader("./")).get_template(
"fantasy_temp.html"
)
output = template.render(
title=title,
league_overview=league_overview,
points_for=points_for,
points_against=points_against,
close_games=close_games,
high_score_and_lost=high_score_and_lost,
low_score_and_won=low_score_and_won,
player_injuries=player_injuries,
team_injuries=team_injuries,
over_expectation=over_expectation,
under_expectation=under_expectation,
top_qb=top_qb,
top_wr=top_wr,
top_rb=top_rb,
top_te=top_te,
top_kick=top_kick,
top_d=top_d,
perfect_lineup=perfect_lineup,
generation_time=now.strftime("%Y-%m-%d %H:%M"),
)
with open(outputfile, "w") as f:
f.write(output)
logger.info("Write to output file")
logger.info("Script completed")
if __name__ == "__main__":
# Parse arguments
parser = argparse.ArgumentParser(
description="Generate HTML for ESPN fantasy football season",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("year", type=int, help="ESPN Fantasy Year")
parser.add_argument(
"-c",
"--credentials",
type=Path,
default="credentials.json",
help="ESPN Credentials file contaning 'league_id', 'espn_s2' and 'swid'",
)
parser.add_argument("--qb", type=int, default=1, help="Number of QBs on team")
parser.add_argument("--rb", type=int, default=2, help="Number of RBs on team")
parser.add_argument("--wr", type=int, default=3, help="Number of WRs on team")
parser.add_argument("--te", type=int, default=1, help="Number of TEs on team")
parser.add_argument("--flex", type=int, default=1, help="Number of Flexs on team")
parser.add_argument("--dst", type=int, default=1, help="Number of D/STs on team")
parser.add_argument("--k", type=int, default=1, help="Number of kickers on team")
args = parser.parse_args()
main(**vars(args))