Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
 - now supports all versions: 1.0, 1.1, and 1.2.
 - updated to version 1.0.0.
  • Loading branch information
bradbarkhouse committed Jan 26, 2018
1 parent 31069c6 commit c9cb19a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ Create main MySportsFeeds object with API version and verbosity as input paramet

var MySportsFeeds = require("mysportsfeeds-node");

var msf = new MySportsFeeds("1.0", true);
var msf = new MySportsFeeds("1.2", true);

If you're using mysportsfeeds-node from a browser environment (like browserify), specify "null" as the 3rd argument to avoid attempts to save the results locally.

var msf = new MySportsFeeds("1.0", true, null);
var msf = new MySportsFeeds("1.2", true, null);

Authenticate (v1.0 uses your MySportsFeeds account credentials)
Authenticate (v1.x uses your MySportsFeeds account credentials)

msf.authenticate("YOUR_USERNAME", "YOUR_PASSWORD");

Expand Down
134 changes: 67 additions & 67 deletions lib/API_v1_0.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,15 @@ var fs = require('fs');
var util = require('util');
var libVersion = require('../package.json').version;

// Private Vars
var platform = util.format('%s-%s%s', os.arch(), os.platform(), os.release());
var options = {
method: 'GET',
uri: '',
json: true,
resolveWithFullResponse: true,
gzip: true,
headers: {
'Authorization': '',
'User-Agent': util.format('MySportsFeeds-Node/%s (%s)', libVersion, platform)
},
qs: {},
transform: function(body) {
// console.log("response = '" + util.inspect(body, {depth: null}) + "'");

return body;
}
};
var auth = null;
var baseUrl = "https://www.mysportsfeeds.com/api/feed/pull";
var verbose = false;
var storeType = null;
var storeLocation = null;
var validFeeds = [];

// Private Functions
function __getBaseUrl(league, feed, format, params) {
return this.baseUrl + '/' + league + '/' + feed + '.' + format;
}

function __verifyFeedName(feed) {
var isValid = false;

validFeeds.forEach(function(value) {
this.validFeeds.forEach(function(value) {
if ( value == feed ) {
isValid = true;
}
Expand All @@ -56,11 +34,11 @@ function __verifyFormat(format) {
}

function __leagueOnlyUrl(league, feed, format, params) {
return baseUrl + '/' + league + '/' + feed + '.' + format;
return this.baseUrl + '/' + league + '/' + feed + '.' + format;
}

function __leagueAndSeasonUrl(league, season, feed, format, params) {
return baseUrl + '/' + league + '/' + season + '/' + feed + '.' + format;
return this.baseUrl + '/' + league + '/' + season + '/' + feed + '.' + format;
}

function __makeOutputFilename(league, season, feed, format, params) {
Expand Down Expand Up @@ -91,52 +69,74 @@ function __saveFeed(response, league, season, feed, format, params) {
throw new Error("CSV feed format not supported in this version.");
}

if ( storeType == "file" ) {
if ( !fs.existsSync(storeLocation) ) {
fs.mkdir(storeLocation);
if ( this.storeType == "file" ) {
if ( !fs.existsSync(this.storeLocation) ) {
fs.mkdir(this.storeLocation);
}

var filename = __makeOutputFilename(league, season, feed, format, params);
var filename = __makeOutputFilename.call(this, league, season, feed, format, params);

if ( format == "json" ) { // This is JSON
fs.writeFileSync(storeLocation + filename, JSON.stringify(storeOutput));
fs.writeFileSync(this.storeLocation + filename, JSON.stringify(storeOutput));
} else {
throw new Error("Could not interpret feed output format.");
}

if ( verbose ) {
console.log("File saved as '" + storeLocation + filename + "'.");
if ( this.verbose ) {
console.log("File saved as '" + this.storeLocation + filename + "'.");
}
}
}

// Public Functions
var API_v1_0 = function(v, storeT, storeL) {
verbose = v;
storeType = storeT;
storeLocation = storeL;
this.platform = util.format('%s-%s%s', os.arch(), os.platform(), os.release());
this.options = {
method: 'GET',
uri: '',
json: true,
resolveWithFullResponse: true,
gzip: true,
headers: {
'Authorization': '',
'User-Agent': util.format('MySportsFeeds-Node/%s (%s)', libVersion, this.platform)
},
qs: {},
transform: function(body) {
// console.log("response = '" + util.inspect(body, {depth: null}) + "'");

return body;
}
};
this.auth = null;
this.baseUrl = null;

validFeeds = [
'current_season',
this.verbose = v;
this.storeType = storeT;
this.storeLocation = storeL;
this.baseUrl = "https://api.mysportsfeeds.com/v1.0/pull";

this.validFeeds = [
'cumulative_player_stats',
'full_game_schedule',
'daily_game_schedule',
'daily_player_stats',
'game_playbyplay',
'game_boxscore',
'scoreboard',
'game_playbyplay',
'player_gamelogs',
'team_gamelogs',
'roster_players',
'game_startinglineup',
'active_players',
'player_injuries',
'latest_updates',
'daily_dfs',
'overall_team_standings',
'division_team_standings',
'conference_team_standings',
'playoff_team_standings'
'division_team_standings',
'playoff_team_standings',
'player_injuries',
'daily_dfs',
'current_season',
'latest_updates'
];
};

Expand All @@ -147,17 +147,17 @@ API_v1_0.prototype.supportsBasicAuth = function() {

// Establish BASIC auth credentials
API_v1_0.prototype.setAuthCredentials = function(username, password) {
auth = {
this.auth = {
username: username,
password: password
};

options.headers.Authorization = util.format('Basic %s',
this.options.headers.Authorization = util.format('Basic %s',
new Buffer(username + ":" + password).toString('base64'));
};

API_v1_0.prototype.getData = function(league, season, feed, format, params) {
if ( !auth ) {
if ( !this.auth ) {
throw new Error("You must authenticate() before making requests.");
}

Expand All @@ -166,40 +166,40 @@ API_v1_0.prototype.getData = function(league, season, feed, format, params) {
params['force'] = 'false';
}

if ( !__verifyFeedName(feed) ) {
if ( !__verifyFeedName.call(this, feed) ) {
throw new Error("Unknown feed '" + feed + "'.");
}

if ( !__verifyFormat(format) ) {
if ( !__verifyFormat.call(this, format) ) {
throw new Error("Unsupported format '" + format + "'.");
}

var url = "";

if ( feed == 'current_season' ) {
url = __leagueOnlyUrl(league, feed, format, params);
url = __leagueOnlyUrl.call(this, league, feed, format, params);
} else {
url = __leagueAndSeasonUrl(league, season, feed, format, params);
url = __leagueAndSeasonUrl.call(this, league, season, feed, format, params);
}

if ( verbose ) {
if ( this.verbose ) {
console.log("Making API request to '" + url + "'.");
console.log(" with headers:");
console.log(options.headers);
console.log(this.options.headers);
console.log(" and params:");
console.log(params);
}

var deferred = Q.defer();
options.uri = util.format(url);
options.qs = params;
this.options.uri = util.format(url);
this.options.qs = params;

// Make the request
rp(options).then(function(body) {
rp(this.options).then(function(body) {
var data = body;

if ( storeType ) {
__saveFeed(data, league, season, feed, format, params);
if ( this.storeType ) {
__saveFeed.call(this, data, league, season, feed, format, params);
}

if ( format == "json" ) {
Expand All @@ -211,20 +211,20 @@ API_v1_0.prototype.getData = function(league, season, feed, format, params) {
}

deferred.resolve(data);
})
}.bind(this))
.catch(function(err) {
if ( verbose ) {
if ( this.verbose ) {
console.log("err = '" + util.inspect(err, {depth: null}) + "'");
}

if ( err.statusCode == 304 ) { // Content hasn't changed, read from local file
if ( verbose ) {
if ( this.verbose ) {
console.log("Data hasn't changed since last call.");
}

var filename = __makeOutputFilename(league, season, feed, format, params);
var filename = __makeOutputFilename.call(this, league, season, feed, format, params);

var data = fs.readFileSync(storeLocation + filename);
var data = fs.readFileSync(this.storeLocation + filename);

if ( format == "json" ) {
data = JSON.parse(data);
Expand All @@ -238,7 +238,7 @@ API_v1_0.prototype.getData = function(league, season, feed, format, params) {
} else {
throw new Error("API call failed with error: " + err.statusCode);
}
});
}.bind(this));

return deferred.promise;
};
Expand Down
13 changes: 13 additions & 0 deletions lib/API_v1_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var API_v1_0 = require('./API_v1_0');
var util = require('util');

// Public Functions
var API_v1_1 = function(v, storeT, storeL) {
API_v1_0.apply(this, arguments);

this.baseUrl = "https://api.mysportsfeeds.com/v1.1/pull";
}

util.inherits(API_v1_1, API_v1_0);

exports = module.exports = API_v1_1;
13 changes: 13 additions & 0 deletions lib/API_v1_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var API_v1_1 = require('./API_v1_1');
var util = require('util');

// Public Functions
var API_v1_2 = function(v, storeT, storeL) {
API_v1_1.apply(this, arguments);

this.baseUrl = "https://api.mysportsfeeds.com/v1.2/pull";
}

util.inherits(API_v1_2, API_v1_1);

exports = module.exports = API_v1_2;
50 changes: 28 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
var API_v1_0 = require('./API_v1_0.js');

// Private Vars
var apiVersion = "1.0";
var storeType = "file";
var storeLocation = "results/";
var apiInstance = null;
var API_v1_1 = require('./API_v1_1.js');
var API_v1_2 = require('./API_v1_2.js');

// Private Functions
function __verifyVersion(version) {
if ( version != '1.0' ) {
throw new Error("Unrecognized version specified. Supported versions are: '1.0'");
if ( version != '1.0' && version != '1.1' && version != '1.2' ) {
throw new Error("Unrecognized version specified. Supported versions are: '1.0', '1.1', '1.2'");
}
}

Expand All @@ -26,32 +22,42 @@ function __verifyStore(storeType, storeLocation) {
}

// Public Functions
var MySportsFeeds = function(apiVers = "1.0", verb = true, storeT = "file", storeL = "results/") {
__verifyVersion(apiVers);
__verifyStore(storeT, storeL);
var MySportsFeeds = function(apiVers = "1.2", verb = true, storeT = "file", storeL = "results/") {
__verifyVersion.call(this, apiVers);
__verifyStore.call(this, storeT, storeL);

apiVersion = apiVers;
verbose = verb;
storeType = storeT;
storeLocation = storeL;
this.apiVersion = apiVers;
this.verbose = verb;
this.storeType = storeT;
this.storeLocation = storeL;

// Instantiate an instance of the appropriate API depending on version
if ( apiVersion == '1.0' ) {
apiInstance = new API_v1_0(verbose, storeType,
storeLocation);
if ( this.apiVersion == '1.0' ) {
this.apiInstance = new API_v1_0(this.verbose, this.storeType,
this.storeLocation);
}

if ( this.apiVersion == '1.1' ) {
this.apiInstance = new API_v1_1(this.verbose, this.storeType,
this.storeLocation);
}

if ( this.apiVersion == '1.2' ) {
this.apiInstance = new API_v1_2(this.verbose, this.storeType,
this.storeLocation);
}
};

MySportsFeeds.prototype.authenticate = function(username, password) {
if ( !apiInstance.supportsBasicAuth() ) {
throw new Error("BASIC authentication not supported for version " + apiVersion);
if ( !this.apiInstance.supportsBasicAuth() ) {
throw new Error("BASIC authentication not supported for version " + this.apiVersion);
}

apiInstance.setAuthCredentials(username, password);
this.apiInstance.setAuthCredentials(username, password);
};

MySportsFeeds.prototype.getData = function(league, season, feed, format, params) {
return apiInstance.getData(league, season, feed, format, params);
return this.apiInstance.getData(league, season, feed, format, params);
};

exports = module.exports = MySportsFeeds;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mysportsfeeds-node",
"version": "0.2.2",
"version": "1.0.0",
"description": "A NodeJS wrapper for the MySportsFeeds Sports Data API (http://www.mysportsfeeds.com/)",
"directories": {
"lib": "./lib"
Expand Down

0 comments on commit c9cb19a

Please sign in to comment.