npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fantasysports

v0.3.0

Published

A wrapper for the Yahoo Fantasy API for Express.js.

Downloads

4

Readme

FantasySports Build Status

This is a node.js library for interacting with the Yahoo Fantasy API. It currently works with Express 3.0, and maybe 4.0, but haven't tried it in 4 yet as of 8/4/14.

The Yahoo API is an OAuth v1 API (gross), so I did my best to make it easy to use, but feel free to contribute any way to making it better!

Getting Started

Install the module with: npm install fantasysports

Configure

For my setup, I configure the API in the router with this...

var FantasySports = require('FantasySports');
FantasySports.options({
    "accessTokenUrl": "https://api.login.yahoo.com/oauth/v2/get_request_token",
    "requestTokenUrl": "https://api.login.yahoo.com/oauth/v2/get_token",
    "oauthKey": process.env.OAUTHKEY,
    "oauthSecret": process.env.OAUTHSECRET,
    "version": "1.0",
    "callback": "http://yourwebsite.com/auth/oauth/callback",
    "encryption": "HMAC-SHA1"
};);

To get an access token you'll have to set up 2 routes in your express app...

// routes/index.js

// app.get("/auth/oauth")
exports.oauth = function(req, res) {
    FantasySports.startAuth(req, res);
};

// app.get("/auth/oauth/callback")
exports.authorize = function(req, res) {
    FantasySports.endAuth(req, res);
};

You also need to make sure that you have session support setup in express.

I'm currently using cookieSession in express 3.0.

app.use(express.cookieSession({ 
    key: 'some key', 
    secret: 'some secret', 
    proxy: true 
}));

Then calling the API in a route is as easy as...

exports.myTeams = function(req, res) {
    FantasySports
        .request(req, res)
        .api('http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=nfl/leagues?format=json')
        .done(function(data) {
            var leagueData = data.fantasy_content.users[0].user[1].games[0].game[1].leagues,
                leagues = [];

            _.each(leagueData, function(value) {
                if (value.league) leagues.push(value.league[0]);
            });

            res.json(leagues);
        });
};

The data model for the Yahoo API is as weird as I've ever seen... hence stuff like var leagueData = data.fantasy_content.users[0].user[1].games[0].game[1].leagues,.

POST/PUT

You can pass an object full of data as a second parameter to the .api function for a POST

** NOTE: You have to pass XML data in your posts, which is... weird**

FantasySports
    .request(req, res)
    .api('http://fantasysports.yahooapis.com/fantasy/v2/league/LEAGUEID/transactions?format=json', '<?xml version="1.0" encoding="UTF-8" ?>' +
'<fantasy_content>' +
    '<transaction>' +
        '<type>drop</type>' +
        '<player>' +
            '<player_key>331.p.24869</player_key>' +
            '<transaction_data>' +
                '<type>drop</type>' +
                '<source_team_key>331.l.198983.t.2</source_team_key>' +
            '</transaction_data>' +
        '</player>' +
    '</transaction>' +
'</fantasy_content>')
    .done(function(data) {
        res.json(data);
    }, function(err) {
        res.json(err);
    });

You can also specify the type as well

FantasySports
    .request(req, res)
    .api('http://fantasysports.yahooapis.com/fantasy/v2/league/LEAGUEID/transactions?format=json', 'PUT', XMLDATA
    .done(function(data) {
        res.json(data);
    }, function(err) {
        res.json(err);
    });

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

v0.2.0 8/3/2014

License

Copyright (c) 2014 Jonathan Creamer
Licensed under the MIT license.