sleeper-api-wrapper 1.0.7__tar.gz → 1.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sleeper_api_wrapper-1.2.1/LICENSE.md +21 -0
- sleeper_api_wrapper-1.2.1/PKG-INFO +74 -0
- sleeper_api_wrapper-1.2.1/README.md +52 -0
- sleeper_api_wrapper-1.2.1/pyproject.toml +32 -0
- {sleeper-api-wrapper-1.0.7 → sleeper_api_wrapper-1.2.1}/sleeper_wrapper/__init__.py +1 -1
- {sleeper-api-wrapper-1.0.7 → sleeper_api_wrapper-1.2.1}/sleeper_wrapper/base_api.py +2 -2
- sleeper_api_wrapper-1.2.1/sleeper_wrapper/drafts.py +33 -0
- sleeper_api_wrapper-1.2.1/sleeper_wrapper/league.py +322 -0
- sleeper_api_wrapper-1.2.1/sleeper_wrapper/players.py +65 -0
- sleeper_api_wrapper-1.2.1/sleeper_wrapper/stats.py +149 -0
- sleeper_api_wrapper-1.2.1/sleeper_wrapper/user.py +68 -0
- sleeper-api-wrapper-1.0.7/PKG-INFO +0 -336
- sleeper-api-wrapper-1.0.7/README.md +0 -322
- sleeper-api-wrapper-1.0.7/setup.cfg +0 -4
- sleeper-api-wrapper-1.0.7/setup.py +0 -28
- sleeper-api-wrapper-1.0.7/sleeper_api_wrapper.egg-info/PKG-INFO +0 -336
- sleeper-api-wrapper-1.0.7/sleeper_api_wrapper.egg-info/SOURCES.txt +0 -14
- sleeper-api-wrapper-1.0.7/sleeper_api_wrapper.egg-info/dependency_links.txt +0 -1
- sleeper-api-wrapper-1.0.7/sleeper_api_wrapper.egg-info/requires.txt +0 -2
- sleeper-api-wrapper-1.0.7/sleeper_api_wrapper.egg-info/top_level.txt +0 -1
- sleeper-api-wrapper-1.0.7/sleeper_wrapper/drafts.py +0 -18
- sleeper-api-wrapper-1.0.7/sleeper_wrapper/league.py +0 -148
- sleeper-api-wrapper-1.0.7/sleeper_wrapper/players.py +0 -11
- sleeper-api-wrapper-1.0.7/sleeper_wrapper/stats.py +0 -53
- sleeper-api-wrapper-1.0.7/sleeper_wrapper/user.py +0 -26
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Swapnik Katkoori
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sleeper-api-wrapper
|
|
3
|
+
Version: 1.2.1
|
|
4
|
+
Summary: A Python API wrapper for Sleeper Fantasy sports, as well as tools to simplify received data.
|
|
5
|
+
Home-page: https://github.com/dtsong/sleeper-api-wrapper
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: fantasy football,fantasy sports,sports,sleeper fantasy,sleeper
|
|
8
|
+
Author: Dan Song, Ford Higgins
|
|
9
|
+
Author-email: py-sleeper-api-wrapper-maintainers@googlegroups.com
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Requires-Dist: requests (>=2.22.0,<3.0.0)
|
|
18
|
+
Project-URL: Documentation, https://github.com/dtsong/sleeper-api-wrapper/tree/master/docs
|
|
19
|
+
Project-URL: Repository, https://github.com/dtsong/sleeper-api-wrapper
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+

|
|
23
|
+

|
|
24
|
+

|
|
25
|
+
# sleeper-api-wrapper
|
|
26
|
+
A Python API wrapper for Sleeper Fantasy Sports, as well as tools to simplify data received. It makes all endpoints found in the Sleeper API docs: https://docs.sleeper.app/ available and turns the JSON response received into Python types for easy usage.
|
|
27
|
+
|
|
28
|
+
Ownership was transferred from @SwapnikKatkoori to @dtsong in March 2022 to continue efforts.
|
|
29
|
+
Original Repository: https://github.com/SwapnikKatkoori/sleeper-api-wrapper
|
|
30
|
+
|
|
31
|
+
# Table of Contents
|
|
32
|
+
1. [Project Roadmap](#roadmap)
|
|
33
|
+
2. [Installation](#install)
|
|
34
|
+
3. [Documentation](#documentation)
|
|
35
|
+
4. [Notes](#notes)
|
|
36
|
+
5. [Dependencies](#depends)
|
|
37
|
+
6. [License](#license)
|
|
38
|
+
|
|
39
|
+
<a name="roadmap"></a>
|
|
40
|
+
# Project Roadmap
|
|
41
|
+
* Establish solid CICD practices with automated testing and validation of pull requests via GitHub Actions
|
|
42
|
+
* Ensure libraries are up to date and secure.
|
|
43
|
+
* Update endpoints and logic with the current Sleeper API docs
|
|
44
|
+
* Investigate performance optimization (effort, implementation, etc)
|
|
45
|
+
|
|
46
|
+
Want to help? Send me a message to @dtsong
|
|
47
|
+
|
|
48
|
+
<a name="install"></a>
|
|
49
|
+
# Install
|
|
50
|
+
```
|
|
51
|
+
pip install sleeper-api-wrapper
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
<a name="documentation"></a>
|
|
55
|
+
# Documentation
|
|
56
|
+
There are five objects in the package that get data from the Sleeper API. Most of them are intuitive based on the [Sleeper API docs](https://docs.sleeper.com/), but full documentation for the Python objects and their methods can be found in the [`docs` folder](https://github.com/dtsong/sleeper-api-wrapper/tree/master/docs). There are some bespoke methods for transforming the data into more useful structures in addition to the methods that directly call the API.
|
|
57
|
+
|
|
58
|
+
<a name="notes"></a>
|
|
59
|
+
# Notes
|
|
60
|
+
This package is intended to be used by Python version 3.8 and higher. There might be some wacky results for previous versions.
|
|
61
|
+
|
|
62
|
+
<a name="depends"></a>
|
|
63
|
+
# Dependencies
|
|
64
|
+
|
|
65
|
+
[requests](https://github.com/kennethreitz/requests)
|
|
66
|
+
- Used for all http requests in sleeper_wrapper
|
|
67
|
+
|
|
68
|
+
[pytest](https://github.com/pytest-dev/pytest)
|
|
69
|
+
- Used for all testing in sleeper_wrapper
|
|
70
|
+
|
|
71
|
+
<a name="license"></a>
|
|
72
|
+
# License
|
|
73
|
+
This project is licensed under the terms of the MIT license.
|
|
74
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+

|
|
4
|
+
# sleeper-api-wrapper
|
|
5
|
+
A Python API wrapper for Sleeper Fantasy Sports, as well as tools to simplify data received. It makes all endpoints found in the Sleeper API docs: https://docs.sleeper.app/ available and turns the JSON response received into Python types for easy usage.
|
|
6
|
+
|
|
7
|
+
Ownership was transferred from @SwapnikKatkoori to @dtsong in March 2022 to continue efforts.
|
|
8
|
+
Original Repository: https://github.com/SwapnikKatkoori/sleeper-api-wrapper
|
|
9
|
+
|
|
10
|
+
# Table of Contents
|
|
11
|
+
1. [Project Roadmap](#roadmap)
|
|
12
|
+
2. [Installation](#install)
|
|
13
|
+
3. [Documentation](#documentation)
|
|
14
|
+
4. [Notes](#notes)
|
|
15
|
+
5. [Dependencies](#depends)
|
|
16
|
+
6. [License](#license)
|
|
17
|
+
|
|
18
|
+
<a name="roadmap"></a>
|
|
19
|
+
# Project Roadmap
|
|
20
|
+
* Establish solid CICD practices with automated testing and validation of pull requests via GitHub Actions
|
|
21
|
+
* Ensure libraries are up to date and secure.
|
|
22
|
+
* Update endpoints and logic with the current Sleeper API docs
|
|
23
|
+
* Investigate performance optimization (effort, implementation, etc)
|
|
24
|
+
|
|
25
|
+
Want to help? Send me a message to @dtsong
|
|
26
|
+
|
|
27
|
+
<a name="install"></a>
|
|
28
|
+
# Install
|
|
29
|
+
```
|
|
30
|
+
pip install sleeper-api-wrapper
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
<a name="documentation"></a>
|
|
34
|
+
# Documentation
|
|
35
|
+
There are five objects in the package that get data from the Sleeper API. Most of them are intuitive based on the [Sleeper API docs](https://docs.sleeper.com/), but full documentation for the Python objects and their methods can be found in the [`docs` folder](https://github.com/dtsong/sleeper-api-wrapper/tree/master/docs). There are some bespoke methods for transforming the data into more useful structures in addition to the methods that directly call the API.
|
|
36
|
+
|
|
37
|
+
<a name="notes"></a>
|
|
38
|
+
# Notes
|
|
39
|
+
This package is intended to be used by Python version 3.8 and higher. There might be some wacky results for previous versions.
|
|
40
|
+
|
|
41
|
+
<a name="depends"></a>
|
|
42
|
+
# Dependencies
|
|
43
|
+
|
|
44
|
+
[requests](https://github.com/kennethreitz/requests)
|
|
45
|
+
- Used for all http requests in sleeper_wrapper
|
|
46
|
+
|
|
47
|
+
[pytest](https://github.com/pytest-dev/pytest)
|
|
48
|
+
- Used for all testing in sleeper_wrapper
|
|
49
|
+
|
|
50
|
+
<a name="license"></a>
|
|
51
|
+
# License
|
|
52
|
+
This project is licensed under the terms of the MIT license.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# pyproject.toml
|
|
2
|
+
|
|
3
|
+
[tool.poetry]
|
|
4
|
+
name = "sleeper-api-wrapper"
|
|
5
|
+
packages = [ { include = "sleeper_wrapper" } ]
|
|
6
|
+
version = "1.2.1"
|
|
7
|
+
description = "A Python API wrapper for Sleeper Fantasy sports, as well as tools to simplify received data."
|
|
8
|
+
authors = ["Dan Song, Ford Higgins <py-sleeper-api-wrapper-maintainers@googlegroups.com>"]
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
homepage = "https://github.com/dtsong/sleeper-api-wrapper"
|
|
12
|
+
repository = "https://github.com/dtsong/sleeper-api-wrapper"
|
|
13
|
+
documentation = "https://github.com/dtsong/sleeper-api-wrapper/tree/master/docs"
|
|
14
|
+
keywords = ["fantasy football", "fantasy sports", "sports", "sleeper fantasy", "sleeper"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Programming Language :: Python",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[tool.poetry.dependencies]
|
|
22
|
+
python = "^3.10"
|
|
23
|
+
requests = "^2.22.0"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["poetry-core"]
|
|
27
|
+
build-backend = "poetry.core.masonry.api"
|
|
28
|
+
|
|
29
|
+
[tool.poetry.group.dev.dependencies]
|
|
30
|
+
pytest = "*"
|
|
31
|
+
pytest-mock = "*"
|
|
32
|
+
tox = "^4.31.0"
|
|
@@ -3,7 +3,7 @@ import json
|
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
class BaseApi():
|
|
6
|
-
def _call(self, url):
|
|
6
|
+
def _call(self, url: str) -> dict:
|
|
7
7
|
result_json_string = requests.get(url);
|
|
8
8
|
try:
|
|
9
9
|
result_json_string.raise_for_status()
|
|
@@ -12,4 +12,4 @@ class BaseApi():
|
|
|
12
12
|
#return SleeperWrapperException("Empty value returned")
|
|
13
13
|
|
|
14
14
|
result = result_json_string.json()
|
|
15
|
-
return result
|
|
15
|
+
return result
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from .base_api import BaseApi
|
|
4
|
+
|
|
5
|
+
class Drafts(BaseApi):
|
|
6
|
+
"""The data associated with a given Sleeper draft.
|
|
7
|
+
|
|
8
|
+
Attributes:
|
|
9
|
+
draft_id: Union[str, int]
|
|
10
|
+
The Sleeper ID for the draft. May be provided as a string or int.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
def __init__(self, draft_id: Union[str, int]) -> None:
|
|
14
|
+
"""Initializes the instance based on draft ID.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
draft_id: Union[str, int]
|
|
18
|
+
The Sleeper ID for the draft. May be provided as a string or int.
|
|
19
|
+
"""
|
|
20
|
+
self.draft_id = draft_id
|
|
21
|
+
self._base_url = "https://api.sleeper.app/v1/draft/{}".format(self.draft_id)
|
|
22
|
+
|
|
23
|
+
def get_specific_draft(self) -> dict:
|
|
24
|
+
"""Returns the draft's data."""
|
|
25
|
+
return self._call(self._base_url)
|
|
26
|
+
|
|
27
|
+
def get_all_picks(self) -> list:
|
|
28
|
+
"""Returns all the picks in the specified draft."""
|
|
29
|
+
return self._call("{}/{}".format(self._base_url,"picks"))
|
|
30
|
+
|
|
31
|
+
def get_traded_picks(self) -> list:
|
|
32
|
+
"""Returns all the traded picks in the specified draft."""
|
|
33
|
+
return self._call("{}/{}".format(self._base_url,"traded_picks"))
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
|
|
3
|
+
from .base_api import BaseApi
|
|
4
|
+
from .stats import Stats
|
|
5
|
+
|
|
6
|
+
class League(BaseApi):
|
|
7
|
+
"""The data associated with the given Sleeper league.
|
|
8
|
+
|
|
9
|
+
Can retrieve data for the league such as matchups, transactions, brackets,
|
|
10
|
+
drafts, scoreboards, and more. Some of these are simple calls to the
|
|
11
|
+
Sleeper API while others add on a layer of data transformation or include
|
|
12
|
+
multiple calls.
|
|
13
|
+
|
|
14
|
+
Attributes:
|
|
15
|
+
league_id: Union[str, int]
|
|
16
|
+
The Sleeper ID for the league. May be provided as a string or int.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(self, league_id: Union[str, int]) -> None:
|
|
20
|
+
"""Initializes the instance based on league ID.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
league_id: Union[str, int]
|
|
24
|
+
Defines the league ID for data retrieval.
|
|
25
|
+
"""
|
|
26
|
+
self.league_id = league_id
|
|
27
|
+
self._base_url = "https://api.sleeper.app/v1/league/{}".format(self.league_id)
|
|
28
|
+
self._league = self._call(self._base_url)
|
|
29
|
+
|
|
30
|
+
def get_league(self) -> dict:
|
|
31
|
+
"""Returns the league data."""
|
|
32
|
+
return self._league
|
|
33
|
+
|
|
34
|
+
def get_rosters(self) -> list:
|
|
35
|
+
"""Retrieves the league's rosters."""
|
|
36
|
+
return self._call("{}/{}".format(self._base_url,"rosters"))
|
|
37
|
+
|
|
38
|
+
def get_users(self) -> list:
|
|
39
|
+
"""Retrieves the league's users."""
|
|
40
|
+
return self._call("{}/{}".format(self._base_url,"users"))
|
|
41
|
+
|
|
42
|
+
def get_matchups(self, week: Union[str, int]) -> list:
|
|
43
|
+
"""Retrieves the league's matchups for the given week."""
|
|
44
|
+
return self._call("{}/{}/{}".format(self._base_url,"matchups", week))
|
|
45
|
+
|
|
46
|
+
def get_playoff_winners_bracket(self) -> list:
|
|
47
|
+
"""Retrieves the winner's playoff bracket."""
|
|
48
|
+
return self._call("{}/{}".format(self._base_url,"winners_bracket"))
|
|
49
|
+
|
|
50
|
+
def get_playoff_losers_bracket(self) -> list:
|
|
51
|
+
"""Retrieves the loser's playoff bracket."""
|
|
52
|
+
return self._call("{}/{}".format(self._base_url,"losers_bracket"))
|
|
53
|
+
|
|
54
|
+
def get_transactions(self, week: Union[str, int]) -> list:
|
|
55
|
+
"""Retrieves all of a league's transactions for the given week."""
|
|
56
|
+
return self._call("{}/{}/{}".format(self._base_url,"transactions", week))
|
|
57
|
+
|
|
58
|
+
def get_trades(self, week: Union[str, int]) -> list:
|
|
59
|
+
"""Retrieves the league's trades for the given week."""
|
|
60
|
+
transactions = self.get_transactions(week)
|
|
61
|
+
return [t for t in transactions if t["type"] == "trade"]
|
|
62
|
+
|
|
63
|
+
def get_waivers(self, week: Union[str, int]) -> list:
|
|
64
|
+
"""Retrieves the league's waiver transactions for the given week."""
|
|
65
|
+
transactions = self.get_transactions(week)
|
|
66
|
+
return [t for t in transactions if t["type"] == "waiver"]
|
|
67
|
+
|
|
68
|
+
def get_free_agents(self, week: Union[str, int]) -> list:
|
|
69
|
+
"""Retrieves the league's free agent transactions for the given week."""
|
|
70
|
+
transactions = self.get_transactions(week)
|
|
71
|
+
return [t for t in transactions if t["type"] == "free_agent"]
|
|
72
|
+
|
|
73
|
+
def get_traded_picks(self) -> list:
|
|
74
|
+
"""Retrieves the league's traded draft picks."""
|
|
75
|
+
return self._call("{}/{}".format(self._base_url,"traded_picks"))
|
|
76
|
+
|
|
77
|
+
def get_all_drafts(self) -> list:
|
|
78
|
+
"""Retrieves all of a league's drafts.
|
|
79
|
+
|
|
80
|
+
This will typically return only one draft.
|
|
81
|
+
"""
|
|
82
|
+
return self._call("{}/{}".format(self._base_url, "drafts"))
|
|
83
|
+
|
|
84
|
+
def map_users_to_team_name(self, users: list) -> dict:
|
|
85
|
+
"""Creates a mapping from user ID to team name.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
users: list
|
|
89
|
+
List of user IDs for the league.
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
A dict mapping the user ID to team name for each user / team
|
|
93
|
+
combination in the league.
|
|
94
|
+
"""
|
|
95
|
+
users_dict = {}
|
|
96
|
+
|
|
97
|
+
# Maps the user_id to team name for easy lookup
|
|
98
|
+
for user in users:
|
|
99
|
+
try:
|
|
100
|
+
users_dict[user["user_id"]] = user["metadata"]["team_name"]
|
|
101
|
+
except:
|
|
102
|
+
users_dict[user["user_id"]] = user["display_name"]
|
|
103
|
+
return users_dict
|
|
104
|
+
|
|
105
|
+
def get_standings(self, rosters: list, users: list) -> dict:
|
|
106
|
+
"""Creates standings based on the team's wins, losses, and ties.
|
|
107
|
+
|
|
108
|
+
Args:
|
|
109
|
+
rosters:
|
|
110
|
+
List of rosters for the league.
|
|
111
|
+
users: list
|
|
112
|
+
List of user IDs for the league.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
List of tuples (team_name, wins, losses, points) sorted by wins in
|
|
116
|
+
descending order.
|
|
117
|
+
"""
|
|
118
|
+
users_dict = self.map_users_to_team_name(users)
|
|
119
|
+
|
|
120
|
+
roster_standings_list = []
|
|
121
|
+
for roster in rosters:
|
|
122
|
+
wins = roster["settings"]["wins"]
|
|
123
|
+
points = roster["settings"]["fpts"]
|
|
124
|
+
name = roster["owner_id"]
|
|
125
|
+
losses = roster["settings"]["losses"]
|
|
126
|
+
if name is not None:
|
|
127
|
+
roster_tuple = (wins, losses, points, users_dict[name])
|
|
128
|
+
else:
|
|
129
|
+
roster_tuple = (wins, losses, points, None)
|
|
130
|
+
roster_standings_list.append(roster_tuple)
|
|
131
|
+
|
|
132
|
+
roster_standings_list.sort(reverse = 1)
|
|
133
|
+
|
|
134
|
+
clean_standings_list = []
|
|
135
|
+
for item in roster_standings_list:
|
|
136
|
+
clean_standings_list.append((item[3], str(item[0]), str(item[1]), str(item[2])))
|
|
137
|
+
|
|
138
|
+
return clean_standings_list
|
|
139
|
+
|
|
140
|
+
def map_rosterid_to_ownerid(self, rosters: list) -> dict:
|
|
141
|
+
"""Creates a mapping from roster ID to owner ID.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
rosters: list
|
|
145
|
+
List of rosters for the league.
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
A dict mapping the roster ID to owner ID for each roster / owner
|
|
149
|
+
combination in the league.
|
|
150
|
+
"""
|
|
151
|
+
result_dict = {}
|
|
152
|
+
for roster in rosters:
|
|
153
|
+
roster_id = roster["roster_id"]
|
|
154
|
+
owner_id = roster["owner_id"]
|
|
155
|
+
result_dict[roster_id] = owner_id
|
|
156
|
+
|
|
157
|
+
return result_dict
|
|
158
|
+
|
|
159
|
+
def get_scoreboards(self, rosters: list, matchups: list, users: list, score_type: str, season: Union[str, int], week: Union[str, int]) -> Union[dict, None]:
|
|
160
|
+
"""Returns the team names and scores from each matchup.
|
|
161
|
+
|
|
162
|
+
Uses the provided information about the league to create a scoreboard
|
|
163
|
+
for the given week. It pulls data from the rosters and users to find
|
|
164
|
+
the team name and then pulls the team's score. It does not currently
|
|
165
|
+
support leagues with custom scoring options and relies on the `Stats()`
|
|
166
|
+
class, which is no longer officially documented by Sleeper.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
rosters: list
|
|
170
|
+
List of rosters for the league.
|
|
171
|
+
matchups: list
|
|
172
|
+
List of matchups for that week.
|
|
173
|
+
users: list
|
|
174
|
+
List of users for the league.
|
|
175
|
+
score_type: str
|
|
176
|
+
Scoring type for the league, eg "pts_std", "pts_ppr", or "pts_half_ppr"
|
|
177
|
+
season: Union[str, int]
|
|
178
|
+
The season to retrieve the scoreboards. May be provided as either a
|
|
179
|
+
str or an int.
|
|
180
|
+
week: Union[str, int]
|
|
181
|
+
The week to retrieve the scoreboards. May be provided as either a
|
|
182
|
+
str or an int.
|
|
183
|
+
|
|
184
|
+
Returns:
|
|
185
|
+
A dict with the matchup ID as key and the corresponding teams' names
|
|
186
|
+
and scores for that matchup.
|
|
187
|
+
"""
|
|
188
|
+
roster_id_dict = self.map_rosterid_to_ownerid(rosters)
|
|
189
|
+
|
|
190
|
+
if len(matchups) == 0:
|
|
191
|
+
return None
|
|
192
|
+
|
|
193
|
+
# Get the users to team name stats
|
|
194
|
+
users_dict = self.map_users_to_team_name(users)
|
|
195
|
+
|
|
196
|
+
# Map roster_id to points
|
|
197
|
+
scoreboards_dict = {}
|
|
198
|
+
|
|
199
|
+
for team in matchups:
|
|
200
|
+
matchup_id = team["matchup_id"]
|
|
201
|
+
current_roster_id = team["roster_id"]
|
|
202
|
+
owner_id = roster_id_dict[current_roster_id]
|
|
203
|
+
if owner_id is not None:
|
|
204
|
+
team_name = users_dict[owner_id]
|
|
205
|
+
else:
|
|
206
|
+
team_name = "Team name not available"
|
|
207
|
+
|
|
208
|
+
team_score = self.get_team_score(team["starters"], score_type, season, week)
|
|
209
|
+
if team_score is None:
|
|
210
|
+
team_score = 0
|
|
211
|
+
|
|
212
|
+
team_score_tuple = (team_name, team_score)
|
|
213
|
+
if matchup_id not in scoreboards_dict:
|
|
214
|
+
scoreboards_dict[matchup_id] = [team_score_tuple]
|
|
215
|
+
else:
|
|
216
|
+
scoreboards_dict[matchup_id].append(team_score_tuple)
|
|
217
|
+
return scoreboards_dict
|
|
218
|
+
|
|
219
|
+
def get_close_games(self, scoreboards: list, close_num: float) -> dict:
|
|
220
|
+
"""Returns scoreboard's games where final margin is beneath given number.
|
|
221
|
+
|
|
222
|
+
Args:
|
|
223
|
+
scoreboards: list
|
|
224
|
+
List of scoreboards, which can be retrieved with the
|
|
225
|
+
`get_scoreboards()` method.
|
|
226
|
+
close_num: float
|
|
227
|
+
The final margin to use as a threshold for determining close games.
|
|
228
|
+
|
|
229
|
+
Returns:
|
|
230
|
+
A dict of matchups qualifying as close.
|
|
231
|
+
"""
|
|
232
|
+
close_games_dict = {}
|
|
233
|
+
for key in scoreboards:
|
|
234
|
+
team_one_score = scoreboards[key][0][1]
|
|
235
|
+
team_two_score = scoreboards[key][1][1]
|
|
236
|
+
|
|
237
|
+
if abs(team_one_score-team_two_score) < close_num:
|
|
238
|
+
close_games_dict[key] = scoreboards[key]
|
|
239
|
+
return close_games_dict
|
|
240
|
+
|
|
241
|
+
def get_team_score(self, starters: list, score_type: str, season: Union[str, int], week: Union[str, int]) -> float:
|
|
242
|
+
"""Retrieves a team's scores for a week based on the score type.
|
|
243
|
+
|
|
244
|
+
Uses the provided list of starters to pull their stats for that week
|
|
245
|
+
with the given score type. It does not currently support leagues with
|
|
246
|
+
custom scoring options because it pulls the pre-calculated score based
|
|
247
|
+
on score type and does not calculate the score based on the league's
|
|
248
|
+
scoring setting. It relies on the `Stats()` class to do this, which is
|
|
249
|
+
no longer officially documented by Sleeper.
|
|
250
|
+
|
|
251
|
+
Args:
|
|
252
|
+
starters: list
|
|
253
|
+
A list of starters for the team.
|
|
254
|
+
score_type: str
|
|
255
|
+
Scoring type for the league, eg "pts_std", "pts_ppr", or "pts_half_ppr"
|
|
256
|
+
season: Union[str, int]
|
|
257
|
+
The season to retrieve the scores. May be provided as either a str
|
|
258
|
+
or an int.
|
|
259
|
+
week: Union[str, int]
|
|
260
|
+
The week to retrieve the scores. May be provided as either a str or
|
|
261
|
+
an int.
|
|
262
|
+
|
|
263
|
+
Returns:
|
|
264
|
+
The total score as a float for the team in that week.
|
|
265
|
+
"""
|
|
266
|
+
total_score = 0
|
|
267
|
+
stats = Stats()
|
|
268
|
+
week_stats = stats.get_week_stats("regular", season, week)
|
|
269
|
+
for starter in starters:
|
|
270
|
+
if stats.get_player_week_stats(week_stats, starter) is not None:
|
|
271
|
+
try:
|
|
272
|
+
total_score += stats.get_player_week_stats(week_stats, starter)[score_type]
|
|
273
|
+
except KeyError:
|
|
274
|
+
total_score += 0
|
|
275
|
+
|
|
276
|
+
return total_score
|
|
277
|
+
|
|
278
|
+
def empty_roster_spots(self, user_id: Union[str, int]) -> Union[int, None]:
|
|
279
|
+
"""Returns the number of empty roster spots on a user's team.
|
|
280
|
+
|
|
281
|
+
Args:
|
|
282
|
+
user_id: Union[str, int]
|
|
283
|
+
The user's ID to check for empty roster spots.
|
|
284
|
+
|
|
285
|
+
Returns:
|
|
286
|
+
The number of empty roster spots assuming the user was found. Otherwise
|
|
287
|
+
returns `None`.
|
|
288
|
+
"""
|
|
289
|
+
# get size of maximum roster
|
|
290
|
+
max_roster_size = len(self._league["roster_positions"])
|
|
291
|
+
|
|
292
|
+
# finds roster of user, returns max size - size of user roster
|
|
293
|
+
rosters = self.get_rosters()
|
|
294
|
+
for roster in rosters:
|
|
295
|
+
if user_id == roster["owner_id"]:
|
|
296
|
+
return max_roster_size - len(roster["players"])
|
|
297
|
+
|
|
298
|
+
# returns None if user was not found
|
|
299
|
+
return None
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def get_league_name(self) -> str:
|
|
303
|
+
"""Returns name of league."""
|
|
304
|
+
return self._league["name"]
|
|
305
|
+
|
|
306
|
+
def get_negative_scores(self, week: Union[str, int]) -> None:
|
|
307
|
+
pass
|
|
308
|
+
|
|
309
|
+
def get_rosters_players(self) -> None:
|
|
310
|
+
pass
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def get_sport_state(sport: str = "nfl") -> dict:
|
|
314
|
+
"""
|
|
315
|
+
Returns info about the current state of the specified sport.
|
|
316
|
+
|
|
317
|
+
https://docs.sleeper.com/#get-nfl-state
|
|
318
|
+
|
|
319
|
+
Parameters:
|
|
320
|
+
- sport: str: Options are "nfl", "nba", "lcs". Default "nfl".
|
|
321
|
+
"""
|
|
322
|
+
return BaseApi()._call("https://api.sleeper.app/v1/state/{}".format(sport))
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from .base_api import BaseApi
|
|
4
|
+
|
|
5
|
+
logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
7
|
+
class Players(BaseApi):
|
|
8
|
+
"""Retrieves player data from Sleeper."""
|
|
9
|
+
|
|
10
|
+
def __init__(self):
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
def get_all_players(self, sport: str = "nfl") -> dict:
|
|
14
|
+
"""Gets all players from Sleeper.
|
|
15
|
+
|
|
16
|
+
Retrieves data pertaining to each player in the Sleeper, including
|
|
17
|
+
positions, biographical data, height / weight, team, and more.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
sport: str
|
|
21
|
+
The sport to retrieve the players. Options include "nfl",
|
|
22
|
+
"nba", and "lcs".
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
A dict of dicts where the keys are the player IDs and the values
|
|
26
|
+
contain all of the player information.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
message = """Please use this call sparingly, as it is intended only to be used once per day at most to keep your player IDs updated.
|
|
30
|
+
|
|
31
|
+
Save the information to your own servers, if possible.
|
|
32
|
+
"""
|
|
33
|
+
logger.info(message)
|
|
34
|
+
return self._call("https://api.sleeper.app/v1/players/nfl")
|
|
35
|
+
|
|
36
|
+
def get_trending_players(self, sport: str, add_drop: str = "add", hours: int = 24, limit: int = 25) -> list:
|
|
37
|
+
"""Gets trending players from Sleeper.
|
|
38
|
+
|
|
39
|
+
Retrieves the player ID and number of adds / drops for that player
|
|
40
|
+
during the specified lookback hours.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
sport: str
|
|
44
|
+
The sport to retrieve the players. Options include "nfl",
|
|
45
|
+
"nba", and "lcs".
|
|
46
|
+
add_drop: str
|
|
47
|
+
Type of action to retreive. Either "add" or "drop".
|
|
48
|
+
hours: int
|
|
49
|
+
The number of hours to look back.
|
|
50
|
+
limit: int
|
|
51
|
+
The number of players to retrieve.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
A list of dicts containing the player ID and a count of the adds /
|
|
55
|
+
drops.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
message = """If you use this trending data, please attribute Sleeper.
|
|
60
|
+
|
|
61
|
+
Copy the code below to embed it in your app:
|
|
62
|
+
<iframe src="https://sleeper.app/embed/players/nfl/trending/add?lookback_hours=24&limit=25" width="350" height="500" allowtransparency="true" frameborder="0"></iframe>
|
|
63
|
+
"""
|
|
64
|
+
logger.info(message)
|
|
65
|
+
return self._call("https://api.sleeper.app/v1/players/{}/trending/{}?lookback_hours={}&limit={}".format(sport, add_drop, hours, limit))
|