Skip to content

Instantly share code, notes, and snippets.

@mrphilroth
Last active September 29, 2024 15:28
Show Gist options
  • Save mrphilroth/a0edf08175c1a55811b8416e7170d386 to your computer and use it in GitHub Desktop.
Save mrphilroth/a0edf08175c1a55811b8416e7170d386 to your computer and use it in GitHub Desktop.
Yahoo Fantasy Basketball Positions
#!/usr/bin/env python
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests",
# "pandas",
# ]
# ///
import pandas as pd
import requests
headers = {
"authority": "pub-api-ro.fantasysports.yahoo.com",
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,ca;q=0.8,da;q=0.7",
"cache-control": "no-cache",
"origin": "https://basketball.fantasysports.yahoo.com",
"pragma": "no-cache",
"referer": "https://basketball.fantasysports.yahoo.com/",
"sec-ch-ua": '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": (
"""Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36"""
"""(KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36")"""
),
}
params = {
"format": "json_f",
}
def main():
names = []
positions = []
for start in range(0, 600, 30):
print(start)
response = requests.get(
(
"""https://pub-api-ro.fantasysports.yahoo.com/fantasy/v2/league/"""
f"""428.l.public;out=settings/players;position=ALL;start={start};"""
"""count=30;sort=rank_season;search=;out=auction_values,ranks;ranks=season;"""
"""ranks_by_position=season;out=expert_ranks;expert_ranks.rank_type="""
"""projected_season_remaining/draft_analysis;cut_types=diamond;slices=last7days"""
),
params=params,
headers=headers,
)
obj = response.json()
for player in obj["fantasy_content"]["league"]["players"]:
names.append(player["player"]["name"]["full"])
positions.append(
", ".join(
[
p
for dp in player["player"]["eligible_positions"]
for p in dp.values()
if p != "Util"
]
)
)
df = pd.DataFrame({"name": names, "position": positions})
df.to_csv("positions.csv", index=False)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment