A lightweight Python library for The Fantrax API.
Project description
Welcome to Fantrax Documentation!
Overview
Unofficial Python bindings for the Fantrax API. The goal is to make interaction with the API as easy as possible while emulating the endpoints as much as possible. I built this testing with my NHL H2HPoints League so results may vary for other sports/league types. I’d be happy to add more but im not in any of those leagues. If you make your league public under Commissioner -> League Setup -> Misc -> Misc and create an issue with your league id or url and i will work to get it added.
Installation & Documentation
pip install fantraxapi
Documentation can be found at Read the Docs.
Using the API
Getting a FantraxAPI Instance
To connect to the Fantrax API you use the FantraxAPI object.
from fantraxapi import League
league_id = "96igs4677sgjk7ol"
league = League(league_id)
import fantraxapi
league_id = "96igs4677sgjk7ol"
league = fantraxapi.League(league_id)
Usage Examples
Example: Get the Scores for the Season.
from fantraxapi import League
league_id = "96igs4677sgjk7ol"
league = League(league_id)
for _, scoring_period in league.scoring_periods().items():
print("")
print(scoring_period)
Connecting with a private league or accessing specific endpoints
I was unable to decipher the api login method so in order to connect to a private league or specific endpoints in a public
league that are not public you will need to use a cookie. The code below overrides the api.request function with
a new function that will automatically log you in using Google Chrome and the selenium and webdriver-manager
packages or load a saved cookie when NotLoggedIn is raised then load the cookie into your current session and save
the logged in cookie to fantraxloggedin.cookie.
First install the two packages:
pip install selenium
pip install webdriver-manager
Second use the code below to setup the auto login on requests.
import os
import pickle
import time
from requests import Session
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from fantraxapi import League, NotLoggedIn, api
from fantraxapi.api import Method
username = "YOUR_USERNAME_HERE" # Provide your Fantrax Username here
password = "YOUR_PASSWORD_HERE" # Provide your Fantrax Password here
cookie_filepath = "fantraxloggedin.cookie" # Name of the saved Cookie file
old_request = api.request # Saves the old function
def new_request(league: "League", methods: list[Method] | Method) -> dict:
try:
if not league.logged_in:
add_cookie_to_session(league.session) # Tries the login function when not logged in
return old_request(league, methods) # Run old function
except NotLoggedIn:
add_cookie_to_session(league.session, ignore_cookie=True) # Adds/refreshes the cookie when NotLoggedIn is raised
return new_request(league, methods) # Rerun the request
api.request = new_request # replace the old function with the new function
def add_cookie_to_session(session: Session, ignore_cookie: bool = False) -> None:
if not ignore_cookie and os.path.exists(cookie_filepath):
with open(cookie_filepath, "rb") as f:
for cookie in pickle.load(f):
session.cookies.set(cookie["name"], cookie["value"])
else:
service = Service(ChromeDriverManager().install())
options = Options()
options.add_argument("--headless")
options.add_argument("--window-size=1920,1600")
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36")
with webdriver.Chrome(service=service, options=options) as driver:
driver.get("https://www.fantrax.com/login")
username_box = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//input[@formcontrolname='email']")))
username_box.send_keys(username)
password_box = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//input[@formcontrolname='password']")))
password_box.send_keys(password)
password_box.send_keys(Keys.ENTER)
time.sleep(5)
cookies = driver.get_cookies()
with open(cookie_filepath, "wb") as cookie_file:
pickle.dump(driver.get_cookies(), cookie_file)
for cookie in cookies:
session.cookies.set(cookie["name"], cookie["value"])
league_id = "usglqmvqmelpe6um"
my_league = League(league_id)
print(my_league.trade_block()) # The Trade Block Page is always private
Usage & Contributions
Source is available on the Github Project Page.
Contributors to FantraxAPI own their own contributions and may distribute that code under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fantraxapi-1.0.1.tar.gz.
File metadata
- Download URL: fantraxapi-1.0.1.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e37ea879a2be75f050c04d80b032d4cd80f766a11dc46083ea4595a81b9eac50
|
|
| MD5 |
421650cdc4b78360edc922daa5def483
|
|
| BLAKE2b-256 |
59871944481a61d039835c4a7545c02d7a8816ec3f14149c8001ce0ac46edde3
|
File details
Details for the file fantraxapi-1.0.1-py3-none-any.whl.
File metadata
- Download URL: fantraxapi-1.0.1-py3-none-any.whl
- Upload date:
- Size: 24.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ef9c22593cc3085338a2b7d120245dcf3e60233dfc2a944cf5bb001c13d44ce
|
|
| MD5 |
7e609ee6c5a06951a8ca07c2518b4435
|
|
| BLAKE2b-256 |
21c8ef76b9acbba78b2cd8dd3fcdd8628700dc8cd664c900ee74671688c57ad5
|