From 9f958c9ab1dcb39548b5b4b7701a7d498438f17c Mon Sep 17 00:00:00 2001 From: Brad Barkhouse Date: Wed, 22 Aug 2018 17:14:13 -0300 Subject: [PATCH] Latest changes to support v2.0 feed requests. --- README.md | 44 +++++++-- example.php | 19 +++- src/API_v1_0.php | 39 +++++++- src/API_v1_1.php | 6 +- src/API_v1_2.php | 6 +- src/API_v2_0.php | 224 ++++++++++++++++++++++++++++++++++++++++++ src/ApiFactory.php | 3 + src/BaseApi.php | 49 +++------ src/MySportsFeeds.php | 11 ++- 9 files changed, 341 insertions(+), 60 deletions(-) create mode 100644 src/API_v2_0.php diff --git a/README.md b/README.md index 2032f88..37bc95b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Free for Non-Commercial Use. Using composer, simply add it to the "require" section of your composer.json: "require": { - "mysportsfeeds\mysportsfeeds-php": ">=1.0.0" + "mysportsfeeds\mysportsfeeds-php": ">=2.0.0" } If you haven't signed up for API access, do so here [https://www.mysportsfeeds.com/index.php/register/](https://www.mysportsfeeds.com/index.php/register/) @@ -20,32 +20,64 @@ If you haven't signed up for API access, do so here [https://www.mysportsfeeds.c Create main MySportsFeeds object with API version as input parameter +For v1.x feed requests (free non-commercial access available): + use MySportsFeeds\MySportsFeeds; $msf = new MySportsFeeds("1.2"); -Authenticate (v1.x uses your MySportsFeeds account credentials) +For v2.0 feed requests (donation required for non-commercial access): + + use MySportsFeeds\MySportsFeeds; + + $msf = new MySportsFeeds("2.0"); + + +Authenticate for v1.x (uses your MySportsFeeds account password) + + $msf->authenticate("", ""); + +Authenticate for v2.0 (simply uses "MYSPORTSFEEDS" as password) + + $msf->authenticate("", "MYSPORTSFEEDS"); - $msf->authenticate("YOUR_USERNAME", "YOUR_PASSWORD"); Start making requests, specifying in this order: $league, $season, $feed, $format, and any other applicable params for the feed. See example.php for sample usage. -Get all NBA 2016-2017 regular season gamelogs for Stephen Curry, in JSON format +Example (v1.x): Get all NBA 2016-2017 regular season gamelogs for Stephen Curry, in JSON format ``` $data = $msf->getData('nba', '2016-2017-regular', 'player_gamelogs', 'json', 'player=stephen-curry'); ``` -Get all NFL 2015-2016 regular season seasonal stats totals for all Dallas Cowboys players, in XML format +Example (v1.x): Get all NFL 2015-2016 regular season seasonal stats totals for all Dallas Cowboys players, in XML format ``` $data = $msf->getData('nfl', '2015-2016-regular', 'cumulative_player_stats', 'xml', 'team=dallas-cowboys'); ``` -Get full game schedule for the MLB 2016 playoff season, in CSV format +Example (v1.x): Get full game schedule for the MLB 2016 playoff season, in CSV format ``` $data = $msf->getData('mlb', '2016-playoff', 'full_game_schedule', 'csv'); ``` +Example (v2.0): Get all NBA 2016-2017 regular season gamelogs for Stephen Curry, in JSON format + +``` + $data = $msf->getData('nba', '2016-2017-regular', 'seasonal_player_gamelogs', 'json', 'player=stephen-curry'); +``` + +Example (v2.0): Get all NFL 2015 regular season stats totals for all Dallas Cowboys players, in XML format + +``` + $data = $msf->getData('nfl', '2015-regular', 'seasonal_player_stats', 'xml', 'team=dallas-cowboys'); +``` + +Example (v2.0): Get full game schedule and scores for the MLB 2016 playoff season, in CSV format + +``` + $data = $msf->getData('mlb', '2016-playoff', 'seasonal_games', 'csv'); +``` + That's it! Returned data is also stored locally under "results/" by default, in appropriately named files. diff --git a/example.php b/example.php index 0260595..3b73e83 100644 --- a/example.php +++ b/example.php @@ -5,7 +5,7 @@ use MySportsFeeds\MySportsFeeds; $data_query = new MySportsFeeds('1.2', true); -$data_query->authenticate('YOUR_USERNAME', 'YOUR_PASSWORD'); +$data_query->authenticate('', ''); $data = $data_query->getData( 'nba', // league @@ -20,3 +20,20 @@ ); var_dump($data); + +$data_query = new MySportsFeeds('2.0', true); +$data_query->authenticate('', 'MYSPORTSFEEDS'); +$data = $data_query->getData( + 'nba', // league + + '2016-2017-regular', // season identifier + + 'seasonal_player_gamelogs', // feed name + + 'json', // format (must be one of: "csv", "json", or "xml") + + // add any number of additional valid parameters, with "name=value" format + 'player=stephen-curry' +); + +var_dump($data); diff --git a/src/API_v1_0.php b/src/API_v1_0.php index 33e195e..05ecc6b 100644 --- a/src/API_v1_0.php +++ b/src/API_v1_0.php @@ -3,8 +3,43 @@ namespace MySportsFeeds; class API_v1_0 extends BaseApi { - protected function getBaseUrlForVersion($version) + + # Constructor + public function __construct($version, $verbose, $storeType = null, $storeLocation = null) { + $this->validFeeds = [ + 'seasonal_games', + 'daily_games', + 'weekly_games', + 'seasonal_dfs', + 'daily_dfs', + 'weekly_dfs', + 'seasonal_player_gamelogs', + 'daily_player_gamelogs', + 'weekly_player_gamelogs', + 'seasonal_team_gamelogs', + 'daily_team_gamelogs', + 'weekly_team_gamelogs', + 'game_boxscore', + 'game_playbyplay', + 'game_lineup', + 'current_season', + 'player_injuries', + 'latest_updates', + 'seasonal_team_stats', + 'seasonal_player_stats', + 'seasonal_venues', + 'players', + 'seasonal_standings' + ]; + } + + protected function __determineUrl($league, $season, $feed, $output, $params) { - return "https://api.mysportsfeeds.com/v1.0/pull"; + if ( $feed == 'current_season' ) { + return $this->baseUrl . "/" . $league . "/" . $feed . "." . $outputFormat; + } else { + return $this->baseUrl . "/" . $league . "/" . $season . "/" . $feed . "." . $outputFormat; + } } + } diff --git a/src/API_v1_1.php b/src/API_v1_1.php index ed4ddc3..1fcdf11 100644 --- a/src/API_v1_1.php +++ b/src/API_v1_1.php @@ -2,9 +2,5 @@ namespace MySportsFeeds; -class API_v1_1 extends BaseApi { - protected function getBaseUrlForVersion($version) - { - return "https://api.mysportsfeeds.com/v1.1/pull"; - } +class API_v1_1 extends API_v1_0 { } diff --git a/src/API_v1_2.php b/src/API_v1_2.php index 1e3f1a4..a59c115 100644 --- a/src/API_v1_2.php +++ b/src/API_v1_2.php @@ -2,9 +2,5 @@ namespace MySportsFeeds; -class API_v1_2 extends BaseApi { - protected function getBaseUrlForVersion($version) - { - return "https://api.mysportsfeeds.com/v1.2/pull"; - } +class API_v1_2 extends API_v1_1 { } diff --git a/src/API_v2_0.php b/src/API_v2_0.php new file mode 100644 index 0000000..4944575 --- /dev/null +++ b/src/API_v2_0.php @@ -0,0 +1,224 @@ +validFeeds = [ + 'seasonal_games', + 'daily_games', + 'weekly_games', + 'seasonal_dfs', + 'daily_dfs', + 'weekly_dfs', + 'seasonal_player_gamelogs', + 'daily_player_gamelogs', + 'weekly_player_gamelogs', + 'seasonal_team_gamelogs', + 'daily_team_gamelogs', + 'weekly_team_gamelogs', + 'game_boxscore', + 'game_playbyplay', + 'game_lineup', + 'current_season', + 'player_injuries', + 'latest_updates', + 'seasonal_team_stats', + 'seasonal_player_stats', + 'seasonal_venues', + 'players', + 'seasonal_standings' + ]; + } + + protected function __determineUrl($league, $season, $feed, $outputFormat, $params) + { + if ( $feed == 'seasonal_games' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/games." . $outputFormat; + + } else if ( $feed == 'daily_games' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "date") ) { + throw new \ErrorException("You must specify a 'date' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/date/" . $params["date"] . "/games." . $outputFormat; + + } else if ( $feed == 'weekly_games' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "week") ) { + throw new \ErrorException("You must specify a 'week' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/week/" . $params["week"] . "/games." . $outputFormat; + + } else if ( $feed == 'seasonal_dfs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/dfs." . $outputFormat; + + } else if ( $feed == 'daily_dfs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "date") ) { + throw new \ErrorException("You must specify a 'date' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/date/" . $params["date"] . "/dfs." . $outputFormat; + + } else if ( $feed == 'weekly_dfs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "week") ) { + throw new \ErrorException("You must specify a 'week' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/week/" . $params["week"] . "/dfs." . $outputFormat; + + } else if ( $feed == 'seasonal_player_gamelogs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/player_gamelogs." . $outputFormat; + + } else if ( $feed == 'daily_player_gamelogs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "date") ) { + throw new \ErrorException("You must specify a 'date' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/date/" . $params["date"] . "/player_gamelogs." . $outputFormat; + + } else if ( $feed == 'weekly_player_gamelogs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "week") ) { + throw new \ErrorException("You must specify a 'week' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/week/" . $params["week"] . "/player_gamelogs." . $outputFormat; + + } else if ( $feed == 'seasonal_team_gamelogs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/team_gamelogs." . $outputFormat; + + } else if ( $feed == 'daily_team_gamelogs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "date") ) { + throw new \ErrorException("You must specify a 'date' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/date/" . $params["date"] . "/team_gamelogs." . $outputFormat; + + } else if ( $feed == 'weekly_team_gamelogs' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "week") ) { + throw new \ErrorException("You must specify a 'week' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/week/" . $params["week"] . "/team_gamelogs." . $outputFormat; + + } else if ( $feed == 'game_boxscore' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "game") ) { + throw new \ErrorException("You must specify a 'game' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/games/" . $params["game"] . "/boxscore." . $outputFormat; + + } else if ( $feed == 'game_playbyplay' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "game") ) { + throw new \ErrorException("You must specify a 'game' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/games/" . $params["game"] . "/playbyplay." . $outputFormat; + + } else if ( $feed == 'game_lineup' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + if ( !array_key_exists($params, "game") ) { + throw new \ErrorException("You must specify a 'game' param for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/games/" . $params["game"] . "/lineup." . $outputFormat; + + } else if ( $feed == 'current_season' ) { + + return $this->baseUrl . "/" . $league . "/current_season." . $outputFormat; + + } else if ( $feed == 'latest_updates' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/latest_updates." . $outputFormat; + + } else if ( $feed == 'seasonal_team_stats' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/team_stats_totals." . $outputFormat; + + } else if ( $feed == 'seasonal_player_stats' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/player_stats_totals." . $outputFormat; + + } else if ( $feed == 'seasonal_venues' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/venues." . $outputFormat; + + } else if ( $feed == 'seasonal_standings' ) { + if ( !$season ) { + throw new \ErrorException("You must specify a season for this request."); + } + + return $this->baseUrl . "/" . $league . "/" . $season . "/standings." . $outputFormat; + + } else { + throw new \ErrorException("Unrecognized feed '" . $feed . "'."); + } + + } + +} diff --git a/src/ApiFactory.php b/src/ApiFactory.php index 4400d51..7cff94c 100644 --- a/src/ApiFactory.php +++ b/src/ApiFactory.php @@ -17,6 +17,9 @@ public static function create($version, $verbose, $storeType, $storeLocation) case '1.2': $apiVersion = new API_v1_2($version, $verbose, $storeType, $storeLocation); break; + case '2.0': + $apiVersion = new API_v2_0($version, $verbose, $storeType, $storeLocation); + break; default: $apiVersion = new BaseApi($version, $verbose, $storeType, $storeLocation); break; diff --git a/src/BaseApi.php b/src/BaseApi.php index 357045e..68f7145 100644 --- a/src/BaseApi.php +++ b/src/BaseApi.php @@ -12,28 +12,7 @@ class BaseApi protected $storeLocation; protected $storeOutput; protected $version; - protected $validFeeds = [ - 'cumulative_player_stats', - 'full_game_schedule', - 'daily_game_schedule', - 'daily_player_stats', - 'game_boxscore', - 'scoreboard', - 'game_playbyplay', - 'player_gamelogs', - 'team_gamelogs', - 'roster_players', - 'game_startinglineup', - 'active_players', - 'overall_team_standings', - 'conference_team_standings', - 'division_team_standings', - 'playoff_team_standings', - 'player_injuries', - 'daily_dfs', - 'current_season', - 'latest_updates' - ]; + protected $validFeeds = []; # Constructor public function __construct($version, $verbose, $storeType = null, $storeLocation = null) { @@ -51,6 +30,11 @@ protected function getBaseUrlForVersion($version) return "https://api.mysportsfeeds.com/v{$version}/pull"; } + protected function __determineUrl($league, $season, $feed, $output, $params) + { + return ""; + } + # Verify a feed protected function __verifyFeedName($feed) { $isValid = false; @@ -76,13 +60,8 @@ protected function __verifyFormat($format) { return $isValid; } - # Feed URL (with only a league specified) - protected function __leagueOnlyUrl($league, $feed, $outputFormat, ...$params) { - return $this->baseUrl . "/" . $league . "/" . $feed . "." . $outputFormat; - } - - # Feed URL (with league + season specified) - protected function __leagueAndSeasonUrl($league, $season, $feed, $outputFormat, ...$params) { + # Feed URL + protected function __determineUrl($league, $season, $feed, $outputFormat, ...$params) { return $this->baseUrl . "/" . $league . "/" . $season . "/" . $feed . "." . $outputFormat; } @@ -131,8 +110,8 @@ public function supportsBasicAuth() { } # Establish BASIC auth credentials - public function setAuthCredentials($username, $password) { - $this->auth = ['username' => $username, 'password' => $password]; + public function setAuthCredentials($apikey, $password) { + $this->auth = ['username' => $apikey, 'password' => $password]; } # Request data (and store it if applicable) @@ -174,18 +153,14 @@ public function getData($league = "", $season = "", $feed = "", $format = "", .. } if ( !$this->__verifyFeedName($feed) ) { - throw new \ErrorException("Unknown feed '" . $feed . "'."); + throw new \ErrorException("Unknown feed '" . $feed . "'. Supported values are: [" . print_r($this->validFeeds, true) . "]"); } if ( !$this->__verifyFormat($format) ) { throw new \ErrorException("Unsupported format '" . $format . "'."); } - if ( $feed == 'current_season' ) { - $url = $this->__leagueOnlyUrl($league, $feed, $format, $params); - } else { - $url = $this->__leagueAndSeasonUrl($league, $season, $feed, $format, $params); - } + $url = $this->__determineUrl($league, $season, $feed, $format, $params); $delim = "?"; if ( strpos($url, '?') !== false ) { diff --git a/src/MySportsFeeds.php b/src/MySportsFeeds.php index a3839a0..facc06e 100644 --- a/src/MySportsFeeds.php +++ b/src/MySportsFeeds.php @@ -3,10 +3,13 @@ namespace MySportsFeeds; use MySportsFeeds\API_v1_0; +use MySportsFeeds\API_v1_1; +use MySportsFeeds\API_v1_2; +use MySportsFeeds\API_v2_0; class MySportsFeeds { - public $buildVersion = "1.0.0"; + public $buildVersion = "2.0.0"; private $version; private $verbose; @@ -40,13 +43,13 @@ private function __verifyStore($storeType, $storeLocation) { } } - # Authenticate against the API (for v1.0) - public function authenticate($username, $password) { + # Authenticate against the API (for v1.x, v2.x) + public function authenticate($apikey, $password) { if ( !$this->apiInstance->supportsBasicAuth() ) { throw new \ErrorException("BASIC authentication not supported for version " + $this->version); } - $this->apiInstance->setAuthCredentials($username, $password); + $this->apiInstance->setAuthCredentials($apikey, $password); } # Request data (and store it if applicable)