Coder Social home page Coder Social logo

joeyagreco / pymfl Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 2.0 101 KB

Python wrapper for the MyFantasyLeague API.

Home Page: https://pypi.org/project/pymfl/

License: MIT License

Python 100.00%
api-wrapper fantasy-football myfantasyleague wrapper wrapper-api

pymfl's Introduction

Hello ๐Ÿ‘‹ I'm Joey Greco

Software Engineer

My Links

Joey's GitHub stats

pymfl's People

Contributors

dependabot[bot] avatar joeyagreco avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

pymfl's Issues

Enable response caching and cookie storage

I just found this library, and it's great. It's saving me a ton of work. As I'm using it, there are two features I want:

  1. Response caching
  2. Cookie storage

Since pymfl uses requests to interact with the MFL API, a relatively small addition to the public interface is enough to make it so that applications can turn on caching (and even set different cache rules per-API) and store cookies between runs.

By adding a requests.Session parameter (defaulted to None) to the APIConfig.add_config_for_year_and_league_id and having pymfl hang onto that with the YearAPIConfig, then having the client base class use that session instead of the raw cookie value, callers who want caching and saving can get it, with no interface changes for callers who don't care about that.

Assuming a caller wants to use requests-cache for caching and a cookiejar from the standard library http.cookies, caching and saving cookies would then look something like this:

import os
import logging

from pymfl.api.config import APIConfig
from pymfl.api import CommonLeagueInfoAPIClient
import requests_cache
from http.cookiejar import LWPCookieJar

logger = logging.getLogger(__name__)

# This particular sample assumes the configuration data is stored in the environment,
# and the thing that runs this script calls `load_dotenv` from python-dotenv to make it so.

def run():
    try:
        mfl_user = os.environ["MFL_USER"]
        mfl_pass = os.environ["MFL_PASS"]
        mfl_league = os.environ["MFL_LEAGUE"]
        mfl_ua = os.environ["MFL_USER_AGENT"]
        mfl_cache = os.environ["MFL_CACHE"]
        mfl_cookies = os.environ["MFL_COOKIES"]
    except KeyError as e:
        logger.error(f"MFL parameters need to be configured in the environment:", e)
        return

    # cache things for 24 hours, for now, to prevent abusive requests while iterating in dev
    expiration_seconds = 60 * 60 * 24
    cookiejar = LWPCookieJar(mfl_cookies)
    if os.path.exists(mfl_cookies):
        cookiejar.load(ignore_discard=True)
    session = requests_cache.CachedSession(cache_name="mfl-cache", expire_after=expiration_seconds)
    session.cookies = cookiejar

    APIConfig.add_config_for_year_and_league_id(year=2022, league_id=mfl_league, username=mfl_user, password=mfl_pass,
                                                user_agent_name=mfl_ua, session=session)
    league_details = CommonLeagueInfoAPIClient.get_league(year=2022, league_id=mfl_league)
    cookiejar.save(ignore_discard=True)
    print(f"retrieved league details:\n{league_details}")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.