fantraxapi


Namefantraxapi JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/meisnate12/FantraxAPI
SummaryA lightweight Python library for The Fantrax API.
upload_time2025-10-17 13:54:10
maintainerNone
docs_urlNone
authorNathan Taggart
requires_python>=3.11
licenseMIT License
keywords fantraxapi fantrax fantasy wrapper api
VCS
bugtrack_url
requirements requests setuptools
Travis-CI No Travis.
coveralls test coverage
            Welcome to Fantrax Documentation!
===========================================================================

.. image:: https://img.shields.io/github/v/release/meisnate12/FantraxAPI?style=plastic
    :target: https://github.com/meisnate12/FantraxAPI/releases
    :alt: GitHub release (latest by date)

.. image:: https://img.shields.io/github/actions/workflow/status/meisnate12/FantraxAPI/tests.yml?branch=master&style=plastic
    :target: https://github.com/meisnate12/FantraxAPI/actions/workflows/tests.yml
    :alt: Build Testing

.. image:: https://img.shields.io/codecov/c/github/meisnate12/FantraxAPI?color=greenred&style=plastic
    :target: https://codecov.io/gh/meisnate12/FantraxAPI
    :alt: Build Coverage

.. image:: https://img.shields.io/github/commits-since/meisnate12/FantraxAPI/latest?style=plastic
    :target: https://github.com/meisnate12/FantraxAPI/commits/master
    :alt: GitHub commits since latest release (by date) for a branch

.. image:: https://img.shields.io/pypi/v/FantraxAPI?style=plastic
    :target: https://pypi.org/project/FantraxAPI/
    :alt: PyPI

.. image:: https://img.shields.io/pypi/dm/FantraxAPI.svg?style=plastic
    :target: https://pypi.org/project/FantraxAPI/
    :alt: Downloads

.. image:: https://img.shields.io/readthedocs/fantraxapi?color=%2300bc8c&style=plastic
    :target: https://fantraxapi.kometa.wiki/en/latest/
    :alt: Wiki

.. image:: https://img.shields.io/discord/822460010649878528?color=%2300bc8c&label=Discord&style=plastic
    :target: https://discord.gg/NfH6mGFuAB
    :alt: Discord

.. image:: https://img.shields.io/reddit/subreddit-subscribers/Kometa?color=%2300bc8c&label=r%2FKometa&style=plastic
    :target: https://www.reddit.com/r/Kometa/
    :alt: Reddit

.. image:: https://img.shields.io/github/sponsors/meisnate12?color=%238a2be2&style=plastic
    :target: https://github.com/sponsors/meisnate12
    :alt: GitHub Sponsors

.. image:: https://img.shields.io/badge/-Sponsor_or_Donate-blueviolet?style=plastic
    :target: https://github.com/sponsors/meisnate12
    :alt: 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.
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
---------------------------------------------------------------------------

.. code-block:: python

    pip install fantraxapi

Documentation_ can be found at Read the Docs.

.. _Documentation: https://fantraxapi.kometa.wiki


Using the API
===========================================================================

Getting a FantraxAPI Instance
---------------------------------------------------------------------------

To connect to the Fantrax API you use the FantraxAPI object.

.. code-block:: python

    from fantraxapi import League

    league_id = "96igs4677sgjk7ol"

    league = League(league_id)


.. code-block:: python

    import fantraxapi

    league_id = "96igs4677sgjk7ol"

    league = fantraxapi.League(league_id)


Usage Examples
===========================================================================

Example: Get the Scores for the Season.

.. code-block:: python

    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 :code:`api.request` function with
a new function that will automatically log you in using Google Chrome and the :code:`selenium` and :code:`webdriver-manager`
packages or load a saved cookie when :code:`NotLoggedIn` is raised then load the cookie into your current session and save
the logged in cookie to :code:`fantraxloggedin.cookie`.

First install the two packages:

.. code-block:: python

    pip install selenium
    pip install webdriver-manager

Second use the code below to setup the auto login on requests.

.. code-block:: python

    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 <https://github.com/meisnate12/FantraxAPI>`_.
* Contributors to FantraxAPI own their own contributions and may distribute that code under
  the `MIT license <https://github.com/meisnate12/FantraxAPI/blob/master/LICENSE.txt>`_.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/meisnate12/FantraxAPI",
    "name": "fantraxapi",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "fantraxapi, fantrax, fantasy, wrapper, api",
    "author": "Nathan Taggart",
    "author_email": "meisnate12@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/59/87/1944481a61d039835c4a7545c02d7a8816ec3f14149c8001ce0ac46edde3/fantraxapi-1.0.1.tar.gz",
    "platform": null,
    "description": "Welcome to Fantrax Documentation!\n===========================================================================\n\n.. image:: https://img.shields.io/github/v/release/meisnate12/FantraxAPI?style=plastic\n    :target: https://github.com/meisnate12/FantraxAPI/releases\n    :alt: GitHub release (latest by date)\n\n.. image:: https://img.shields.io/github/actions/workflow/status/meisnate12/FantraxAPI/tests.yml?branch=master&style=plastic\n    :target: https://github.com/meisnate12/FantraxAPI/actions/workflows/tests.yml\n    :alt: Build Testing\n\n.. image:: https://img.shields.io/codecov/c/github/meisnate12/FantraxAPI?color=greenred&style=plastic\n    :target: https://codecov.io/gh/meisnate12/FantraxAPI\n    :alt: Build Coverage\n\n.. image:: https://img.shields.io/github/commits-since/meisnate12/FantraxAPI/latest?style=plastic\n    :target: https://github.com/meisnate12/FantraxAPI/commits/master\n    :alt: GitHub commits since latest release (by date) for a branch\n\n.. image:: https://img.shields.io/pypi/v/FantraxAPI?style=plastic\n    :target: https://pypi.org/project/FantraxAPI/\n    :alt: PyPI\n\n.. image:: https://img.shields.io/pypi/dm/FantraxAPI.svg?style=plastic\n    :target: https://pypi.org/project/FantraxAPI/\n    :alt: Downloads\n\n.. image:: https://img.shields.io/readthedocs/fantraxapi?color=%2300bc8c&style=plastic\n    :target: https://fantraxapi.kometa.wiki/en/latest/\n    :alt: Wiki\n\n.. image:: https://img.shields.io/discord/822460010649878528?color=%2300bc8c&label=Discord&style=plastic\n    :target: https://discord.gg/NfH6mGFuAB\n    :alt: Discord\n\n.. image:: https://img.shields.io/reddit/subreddit-subscribers/Kometa?color=%2300bc8c&label=r%2FKometa&style=plastic\n    :target: https://www.reddit.com/r/Kometa/\n    :alt: Reddit\n\n.. image:: https://img.shields.io/github/sponsors/meisnate12?color=%238a2be2&style=plastic\n    :target: https://github.com/sponsors/meisnate12\n    :alt: GitHub Sponsors\n\n.. image:: https://img.shields.io/badge/-Sponsor_or_Donate-blueviolet?style=plastic\n    :target: https://github.com/sponsors/meisnate12\n    :alt: Sponsor or Donate\n\n\nOverview\n---------------------------------------------------------------------------\nUnofficial 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.\nI 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.\nIf 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.\n\n\nInstallation & Documentation\n---------------------------------------------------------------------------\n\n.. code-block:: python\n\n    pip install fantraxapi\n\nDocumentation_ can be found at Read the Docs.\n\n.. _Documentation: https://fantraxapi.kometa.wiki\n\n\nUsing the API\n===========================================================================\n\nGetting a FantraxAPI Instance\n---------------------------------------------------------------------------\n\nTo connect to the Fantrax API you use the FantraxAPI object.\n\n.. code-block:: python\n\n    from fantraxapi import League\n\n    league_id = \"96igs4677sgjk7ol\"\n\n    league = League(league_id)\n\n\n.. code-block:: python\n\n    import fantraxapi\n\n    league_id = \"96igs4677sgjk7ol\"\n\n    league = fantraxapi.League(league_id)\n\n\nUsage Examples\n===========================================================================\n\nExample: Get the Scores for the Season.\n\n.. code-block:: python\n\n    from fantraxapi import League\n\n    league_id = \"96igs4677sgjk7ol\"\n\n    league = League(league_id)\n\n    for _, scoring_period in league.scoring_periods().items():\n        print(\"\")\n        print(scoring_period)\n\n\nConnecting with a private league or accessing specific endpoints\n===========================================================================\n\nI was unable to decipher the api login method so in order to connect to a private league or specific endpoints in a public\nleague that are not public you will need to use a cookie. The code below overrides the :code:`api.request` function with\na new function that will automatically log you in using Google Chrome and the :code:`selenium` and :code:`webdriver-manager`\npackages or load a saved cookie when :code:`NotLoggedIn` is raised then load the cookie into your current session and save\nthe logged in cookie to :code:`fantraxloggedin.cookie`.\n\nFirst install the two packages:\n\n.. code-block:: python\n\n    pip install selenium\n    pip install webdriver-manager\n\nSecond use the code below to setup the auto login on requests.\n\n.. code-block:: python\n\n    import os\n    import pickle\n    import time\n\n    from requests import Session\n    from selenium import webdriver\n    from selenium.webdriver import Keys\n    from selenium.webdriver.chrome.options import Options\n    from selenium.webdriver.chrome.service import Service\n    from selenium.webdriver.common.by import By\n    from selenium.webdriver.support import expected_conditions\n    from selenium.webdriver.support.ui import WebDriverWait\n    from webdriver_manager.chrome import ChromeDriverManager\n\n    from fantraxapi import League, NotLoggedIn, api\n    from fantraxapi.api import Method\n\n    username = \"YOUR_USERNAME_HERE\" # Provide your Fantrax Username here\n    password = \"YOUR_PASSWORD_HERE\" # Provide your Fantrax Password here\n    cookie_filepath = \"fantraxloggedin.cookie\" # Name of the saved Cookie file\n\n    old_request = api.request # Saves the old function\n\n\n    def new_request(league: \"League\", methods: list[Method] | Method) -> dict:\n        try:\n            if not league.logged_in:\n                add_cookie_to_session(league.session) # Tries the login function when not logged in\n            return old_request(league, methods) # Run old function\n        except NotLoggedIn:\n            add_cookie_to_session(league.session, ignore_cookie=True) # Adds/refreshes the cookie when NotLoggedIn is raised\n            return new_request(league, methods) # Rerun the request\n\n\n    api.request = new_request # replace the old function with the new function\n\n\n    def add_cookie_to_session(session: Session, ignore_cookie: bool = False) -> None:\n        if not ignore_cookie and os.path.exists(cookie_filepath):\n            with open(cookie_filepath, \"rb\") as f:\n                for cookie in pickle.load(f):\n                    session.cookies.set(cookie[\"name\"], cookie[\"value\"])\n        else:\n            service = Service(ChromeDriverManager().install())\n\n            options = Options()\n            options.add_argument(\"--headless\")\n            options.add_argument(\"--window-size=1920,1600\")\n            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\")\n\n            with webdriver.Chrome(service=service, options=options) as driver:\n                driver.get(\"https://www.fantrax.com/login\")\n                username_box = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, \"//input[@formcontrolname='email']\")))\n                username_box.send_keys(username)\n                password_box = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, \"//input[@formcontrolname='password']\")))\n                password_box.send_keys(password)\n                password_box.send_keys(Keys.ENTER)\n                time.sleep(5)\n\n                cookies = driver.get_cookies()\n                with open(cookie_filepath, \"wb\") as cookie_file:\n                    pickle.dump(driver.get_cookies(), cookie_file)\n\n                for cookie in cookies:\n                    session.cookies.set(cookie[\"name\"], cookie[\"value\"])\n\n    league_id = \"usglqmvqmelpe6um\"\n\n    my_league = League(league_id)\n\n    print(my_league.trade_block())  # The Trade Block Page is always private\n\n\nUsage & Contributions\n---------------------------------------------------------------------------\n\n* Source is available on the `Github Project Page <https://github.com/meisnate12/FantraxAPI>`_.\n* Contributors to FantraxAPI own their own contributions and may distribute that code under\n  the `MIT license <https://github.com/meisnate12/FantraxAPI/blob/master/LICENSE.txt>`_.\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "A lightweight Python library for The Fantrax API.",
    "version": "1.0.1",
    "project_urls": {
        "Documentation": "https://fantraxapi.kometa.wiki",
        "Funding": "https://github.com/sponsors/meisnate12",
        "Homepage": "https://github.com/meisnate12/FantraxAPI",
        "Issues": "https://github.com/meisnate12/FantraxAPI/issues",
        "Source": "https://github.com/meisnate12/FantraxAPI"
    },
    "split_keywords": [
        "fantraxapi",
        " fantrax",
        " fantasy",
        " wrapper",
        " api"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "21c8ef76b9acbba78b2cd8dd3fcdd8628700dc8cd664c900ee74671688c57ad5",
                "md5": "7e609ee6c5a06951a8ca07c2518b4435",
                "sha256": "9ef9c22593cc3085338a2b7d120245dcf3e60233dfc2a944cf5bb001c13d44ce"
            },
            "downloads": -1,
            "filename": "fantraxapi-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7e609ee6c5a06951a8ca07c2518b4435",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 24858,
            "upload_time": "2025-10-17T13:54:09",
            "upload_time_iso_8601": "2025-10-17T13:54:09.541218Z",
            "url": "https://files.pythonhosted.org/packages/21/c8/ef76b9acbba78b2cd8dd3fcdd8628700dc8cd664c900ee74671688c57ad5/fantraxapi-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "59871944481a61d039835c4a7545c02d7a8816ec3f14149c8001ce0ac46edde3",
                "md5": "421650cdc4b78360edc922daa5def483",
                "sha256": "e37ea879a2be75f050c04d80b032d4cd80f766a11dc46083ea4595a81b9eac50"
            },
            "downloads": -1,
            "filename": "fantraxapi-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "421650cdc4b78360edc922daa5def483",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 24558,
            "upload_time": "2025-10-17T13:54:10",
            "upload_time_iso_8601": "2025-10-17T13:54:10.444955Z",
            "url": "https://files.pythonhosted.org/packages/59/87/1944481a61d039835c4a7545c02d7a8816ec3f14149c8001ce0ac46edde3/fantraxapi-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-17 13:54:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "meisnate12",
    "github_project": "FantraxAPI",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [
        {
            "name": "requests",
            "specs": []
        },
        {
            "name": "setuptools",
            "specs": []
        }
    ],
    "lcname": "fantraxapi"
}
        
Elapsed time: 2.77086s