-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleeper.py
314 lines (262 loc) · 9.09 KB
/
sleeper.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import asyncio
import os
import requests
from gql import AIOHTTPTransport, Client, gql
from gql.transport.exceptions import TransportQueryError
from database import getDB, getPlayerData
from sportsdata import getTimeframe
# from flask import session
class SleeperGraphQLClient:
def __init__(self):
# TODO remove once PR 135 makes it into gql
asyncio.set_event_loop(asyncio.new_event_loop())
transport = AIOHTTPTransport(url="https://sleeper.app/graphql")
self.client = Client(transport=transport, fetch_schema_from_transport=False)
def request(self, body, variables=None):
return self.client.execute(body, variable_values=variables)
def _getLeague():
leagueID = os.getenv("LEAGUE_ID")
return requests.get(f"https://api.sleeper.app/v1/league/{leagueID}").json()
def _getOwners():
leagueID = os.getenv("LEAGUE_ID")
owners = requests.get(f"https://api.sleeper.app/v1/league/{leagueID}/users").json()
ownerDict = {}
for owner in owners:
ownerDict[owner["user_id"]] = owner
return ownerDict
def _getRosters():
leagueID = os.getenv("LEAGUE_ID")
return requests.get(f"https://api.sleeper.app/v1/league/{leagueID}/rosters").json()
def _getRosterPlayers(rosters):
# flatten player list
simpleRosters = [roster["players"] for roster in rosters]
allIds = [player for roster in simpleRosters for player in roster]
# get players from DB
db = getDB()
return getPlayerData(db, allIds)
def getOwner(user, pw):
"""Verifies user is an owner in the league."""
client = SleeperGraphQLClient()
query = gql(
"""
query getUserID ($user: String! $pw: String!) {
login(email_or_phone_or_username: $user password: $pw) {
avatar
display_name
real_name
email
phone
user_id
}
}
"""
)
try:
data = client.request(query, {"user": user, "pw": pw})
except TransportQueryError:
return None
userID = data["login"]["user_id"]
season = getTimeframe()["season"]
leagues = requests.get(
f"https://api.sleeper.app/v1/user/{userID}/leagues/nfl/{season}"
).json()
leagueIDs = [league["league_id"] for league in leagues]
if os.getenv("LEAGUE_ID") in leagueIDs:
db = getDB()
db.users.update(
{"user_id": data["login"]["user_id"]}, data["login"], upsert=True
)
return userID
return None
def updatePlayers():
"""Gets players from Sleepers database and uploads to Mongo."""
players = requests.get("https://api.sleeper.app/v1/players/nfl").json()
# update players in database
db = getDB()
db.drop_collection("players")
db.create_collection("players")
db.players.insert_many(players.values())
db.players.create_index("player_id")
def getTeams(skipPlayers=False):
"""Gets the current teams in the league."""
owners = _getOwners()
rosters = _getRosters()
players = _getRosterPlayers(rosters) if not skipPlayers else {}
def getPlayerGroup(ids, positions):
# pad positions with empty slots
ids.extend(["0"] * (len(positions) - len(ids)))
return list(
map(
lambda pId: {
"name": f"{players[pId]['first_name']} {players[pId]['last_name']}"
if pId != "0"
else "(Empty)",
"pos": positions.pop(0) if len(positions) else "",
},
ids,
)
)
teams = []
for i, roster in enumerate(rosters):
# bench players are the ones remaining
owner = owners[roster["owner_id"]]
teamName = (
owner["metadata"]["team_name"]
if "team_name" in owner["metadata"].keys()
else f"Team {i+1}"
)
team = {
# convert sleeper ID to KPFFL ID
"id": "team|" + str(roster["roster_id"]),
"name": teamName,
"owner": owner["display_name"],
"players": {
"starters": getPlayerGroup(
roster["starters"],
["QB", "RB", "RB", "WR", "WR", "TE", "Flex", "DEF"],
),
"bench": getPlayerGroup(
list(
set(roster["players"])
- set(
roster["starters"]
+ (roster["reserve"] or [])
+ (roster["taxi"] or [])
)
),
["Bench"] * 12,
),
"reserve": getPlayerGroup(roster["reserve"] or [], ["IR"]),
"taxi": getPlayerGroup(roster["taxi"] or [], ["Taxi"] * 3),
}
if len(players.keys())
else {},
"stats": {
"w": roster["settings"]["wins"],
"l": roster["settings"]["losses"],
"pf": roster["settings"]["fpts"],
},
}
teams.append(team)
return teams
def getMatchups(week=None, teams=None):
"""Gets matchups for current week."""
leagueID = os.getenv("LEAGUE_ID")
week = week or getTimeframe()["week"]
teams = teams or getTeams(True)
data = requests.get(
f"https://api.sleeper.app/v1/league/{leagueID}/matchups/{week}"
).json()
# get all matchup IDs
matchups = {
str(matchupid): []
for matchupid in set([matchup["matchup_id"] for matchup in data])
}
# add in team names and scores
teamHash = {team["id"].split("|")[1]: team for team in teams}
for matchup in data:
matchups[str(matchup["matchup_id"])].append(
{
"team": teamHash[str(matchup["roster_id"])],
"score": round(matchup["points"], 2),
}
)
return matchups.values()
def getTrades(currentWeek=None, teams=None, n=5):
"""Gets recent transactions"""
leagueID = os.getenv("LEAGUE_ID")
currentWeek = currentWeek or getTimeframe()["week"]
teams = teams or getTeams(True)
db = getDB()
# recursively search until I find 5 trades
tradesData = []
def findTrades(week):
txns = requests.get(
f"https://api.sleeper.app/v1/league/{leagueID}/transactions/{week}"
).json()
# filter for only trades of two teams
tradesData.extend(
[
trade
for trade in txns
if trade["type"] == "trade" and len(trade["roster_ids"]) == 2
]
)
if len(tradesData) < n:
findTrades(week - 1)
findTrades(currentWeek)
tradesData = tradesData[:n]
# get player info
id_list_list = [trade["adds"].keys() for trade in tradesData]
id_list = set([item for sublist in id_list_list for item in sublist])
allPlayers = getPlayerData(db, id_list)
trades = []
def getTradeData(roster_id):
players = [
key
for key in tradeData["adds"].keys()
if tradeData["adds"][key] == roster_id
]
picks = [
pick for pick in tradeData["draft_picks"] if pick["owner_id"] == roster_id
]
draftConvert = {1: "1st", 2: "2nd", 3: "3rd"}
voteData = db.trade_votes.find(
{
"transaction_id": tradeData["transaction_id"],
"roster_id": roster_id,
}
)
voteUsers = [vote["user_id"] for vote in voteData]
return {
"team": [
team for team in teams if team["id"].split("|")[1] == str(roster_id)
][0],
"players": [allPlayers[player_id] for player_id in players],
"picks": [
{
"season": pick["season"],
"round": draftConvert[pick["round"]],
"owner": [
team
for team in teams
if team["id"].split("|")[1] == str(pick["roster_id"])
][0]["owner"],
}
for pick in picks
],
"votes": voteUsers,
}
for tradeData in tradesData:
trade = {
"transaction_id": tradeData["transaction_id"],
"team1": getTradeData(tradeData["roster_ids"][0]),
"team2": getTradeData(tradeData["roster_ids"][1]),
}
trades.append(trade)
return trades
def addTradeVote(transaction_id, user_id, roster_id):
"""Adds Trade Vote to DB."""
db = getDB()
db.trade_votes.update(
{
"transaction_id": transaction_id,
"user_id": user_id,
"roster_id": roster_id,
},
{
"transaction_id": transaction_id,
"user_id": user_id,
"roster_id": roster_id,
},
upsert=True,
)
def deleteTradeVotes(transaction_id, user_id):
"""Delete Trade Votes per user."""
db = getDB()
db.trade_votes.delete_many(
{
"transaction_id": transaction_id,
"user_id": user_id,
},
)