Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Fix] Load Scores no longer working (fix inside) #45

Open
jstackman opened this issue Sep 11, 2020 · 6 comments
Open

[Bug Fix] Load Scores no longer working (fix inside) #45

jstackman opened this issue Sep 11, 2020 · 6 comments

Comments

@jstackman
Copy link

jstackman commented Sep 11, 2020

The NFL now requires an API key to access their data so the "Load Scores" button no longer works in the admin panel. I found a free feed from ESPN that I am using now. I have only tested this on the Chiefs and Texans game so far, so there could be some issues with it still. Specifically, I am not sure how the feed shows overtime, so I made a guess. I also don't know if all the team abbreviations will match up.

All you need to do is update the getHtmlScores.php file:

<?php
require('includes/application_top.php');

$week = (int)$_GET['week'];

//load source code, depending on the current week, of the website into a variable as a string
//$url = "http://www.nfl.com/ajax/scorestrip?season=".SEASON_YEAR."&seasonType=REG&week=".$week;
$url = "https://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard?week=".$week;
if ($xmlData = file_get_contents($url)) {
	$games = json_decode($xmlData, true);
}

//build scores array, to group teams and scores together in games
$scores = array();
foreach ($games['events'] as $gameArray) {
	$game = $gameArray['competitions'][0];
	if ($game['status']['type']['completed'] == true) {
		$overtime = (($game['status']['period'] == '5') ? 1 : 0);
		foreach ($game['competitors'] as $gameTeams) {
			if ($gameTeams['homeAway'] == "home") {
				$home_team = $gameTeams['team']['abbreviation'];
				$home_score = (int)$gameTeams['score'];
				if ($home_team == "WSH")
					$home_team = "WAS";
			}
			if ($gameTeams['homeAway'] == "away") {
				$away_team = $gameTeams['team']['abbreviation'];
				$away_score = (int)$gameTeams['score'];
				if ($away_team == "WSH")
					$away_team = "WAS";
			}
		}

		$winner = ($away_score > $home_score) ? $away_team : $home_team;
		$gameID = getGameIDByTeamID($week, $home_team);
		if (is_numeric(strip_tags($home_score)) && is_numeric(strip_tags($away_score))) {
			if ($away_score > 0 || $home_score > 0) {
				$scores[] = array(
					'gameID' => $gameID,
					'awayteam' => $away_team,
					'visitorScore' => $away_score,
					'hometeam' => $home_team,
					'homeScore' => $home_score,
					'overtime' => $overtime,
					'winner' => $winner
				);
			}
		}
	}
}

//see how the scores array looks
//echo '<pre>' . print_r($scores, true) . '</pre>';
echo json_encode($scores);

//game results and winning teams can now be accessed from the scores array
//e.g. $scores[0]['awayteam'] contains the name of the away team (['awayteam'] part) from the first game on the page ([0] part)
@peberly
Copy link

peberly commented Sep 18, 2020

I tried this out but didn't work for all the games. Week 1 - just the CLE v BAL game worked. Week 2 - CIN v CLE game only.

@jstackman
Copy link
Author

Yeah, it didn't work for past weeks, only the current one. There was also a problem with the Washington abbreviation. These have both been fixed in this version:

<?php
require('includes/application_top.php');

$week = (int)$_GET['week'];

//load source code, depending on the current week, of the website into a variable as a string
//$url = "http://www.nfl.com/ajax/scorestrip?season=".SEASON_YEAR."&seasonType=REG&week=".$week;
$url = "https://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard?week=".$week;
if ($xmlData = file_get_contents($url)) {
	$games = json_decode($xmlData, true);
}

//build scores array, to group teams and scores together in games
$scores = array();
foreach ($games['events'] as $gameArray) {
	$game = $gameArray['competitions'][0];
	if ($game['status']['type']['completed'] == true) {
		$overtime = (($game['status']['period'] == '5') ? 1 : 0);
		foreach ($game['competitors'] as $gameTeams) {
			if ($gameTeams['homeAway'] == "home") {
				$home_team = $gameTeams['team']['abbreviation'];
				$home_score = (int)$gameTeams['score'];
				if ($home_team == "WSH")
					$home_team = "WAS";
			}
			if ($gameTeams['homeAway'] == "away") {
				$away_team = $gameTeams['team']['abbreviation'];
				$away_score = (int)$gameTeams['score'];
				if ($away_team == "WSH")
					$away_team = "WAS";
			}
		}

		$winner = ($away_score > $home_score) ? $away_team : $home_team;
		$gameID = getGameIDByTeamID($week, $home_team);
		if (is_numeric(strip_tags($home_score)) && is_numeric(strip_tags($away_score))) {
			if ($away_score > 0 || $home_score > 0) {
				$scores[] = array(
					'gameID' => $gameID,
					'awayteam' => $away_team,
					'visitorScore' => $away_score,
					'hometeam' => $home_team,
					'homeScore' => $home_score,
					'overtime' => $overtime,
					'winner' => $winner
				);
			}
		}
	}
}

//see how the scores array looks
//echo '<pre>' . print_r($scores, true) . '</pre>';
echo json_encode($scores);

//game results and winning teams can now be accessed from the scores array
//e.g. $scores[0]['awayteam'] contains the name of the away team (['awayteam'] part) from the first game on the page ([0] part)

@peberly
Copy link

peberly commented Sep 18, 2020 via email

@searnhardt
Copy link

Last version seemed to work great for week 2! Thanks so much!

@jstackman jstackman changed the title Load Scores no longer working (fix inside) [Bug Fix] Load Scores no longer working (fix inside) Sep 24, 2020
@savagegeek22
Copy link

Thanks again for getting this working from last year. Works great this year as well. You are life saver! :-) jstackman

@savagegeek22
Copy link

Question for @jstackman if you may be able to help here... So there was code that was able to get scores auto from nfl, but since that's not available, is the fix somehow able to use in a cron job like this to pull in automatically?
#19
I'm trying to figure it out, but I'm not the best at doing this LOL.
Just thought I would add to this thread to add the capability if available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants