Skip to content

ejde/FantraxAPI

 
 

Repository files navigation

Welcome to Fantrax Documentation!

GitHub release (latest by date) Build Testing Build Coverage GitHub commits since latest release (by date) for a branch PyPI Downloads Wiki Discord Reddit GitHub Sponsors Sponsor or Donate

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

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 FantraxAPI

league_id = "96igs4677sgjk7ol"

api = FantraxAPI(league_id)
import fantraxapi

api = fantraxapi.FantraxAPI()

Usage Examples

Example: Get the Scores for the Season.

from fantraxapi import FantraxAPI

league_id = "96igs4677sgjk7ol"

api = FantraxAPI(league_id)

for _, scoring_period in api.scoring_periods().items():
    print("")
    print(scoring_period)

Connecting with a private League

I was unable to decipher the api login method so in order to connect to a private league or specific pages in a public league that are not public you need to use a cookie. The code below uses Google Chrome and the selenium and webdriver-manager packages to open a chrome instance where you can login and after 30 seconds a cookie file with that login will be save to fantraxloggedin.cookie.

First install the two packages:

pip install selenium
pip install webdriver-manager

Second run the code below and login when the chrome window loads the Fantrax login page:

import pickle
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

service = Service(ChromeDriverManager().install())

options = Options()
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")
    time.sleep(30)
    pickle.dump(driver.get_cookies(), open("fantraxloggedin.cookie", "wb"))

Third use the saved cookie file with the wrapper:

import pickle
from fantraxapi import FantraxAPI
from requests import Session

session = Session()

with open("fantraxloggedin.cookie", "rb") as f:
    for cookie in pickle.load(f):
        session.cookies.set(cookie["name"], cookie["value"])

league_id = "96igs4677sgjk7ol"

api = FantraxAPI(league_id, session=session)

print(api.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.

About

Python Wrapper for the Frantrax API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%