-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildSchedule.php
76 lines (64 loc) · 2.1 KB
/
buildSchedule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
require('includes/application_top.php');
$weeks = 18;
$schedule = array();
for ($week = 1; $week <= $weeks; $week++) {
$url = "https://www.espn.com/nfl/schedule/_/year/".SEASON_YEAR."/week/".$week."?xhr=1";
if ($json = @file_get_contents($url)) {
$jsonDecoded = json_decode($json, true);
$scheduleData = $jsonDecoded['content']['schedule'];
} else {
die('Error getting schedule from espn.com.');
}
//build scores array, to group teams and scores together in games
foreach ($scheduleData as $gameDay) {
foreach ($gameDay['games'] as $game) {
$gameID = substr($game['uid'], strrpos($game['uid'], ':') + 1);
//get game time (UTC) and set to eastern
$date = new DateTime($game['date']);
$date->setTimezone(new DateTimeZone('America/New_York'));
$gameTimeEastern = $date->format('Y-m-d H:i:00');
//get tv
foreach ($game['competitions'][0]['geoBroadcasts'] as $broadcast) {
if ($broadcast['type']['shortName'] == 'TV') {
$tv = $broadcast['media']['shortName'];
}
}
//get team codes
foreach ($game['competitions'][0]['competitors'] as $team) {
if ($team['homeAway'] == 'home') {
$home_team = $team['team']['abbreviation'];
$home_score = $team['score'];
} else {
$away_team = $team['team']['abbreviation'];
$away_score = $team['score'];
}
}
$schedule[] = array(
'game_id' => $gameID,
'season' => SEASON_YEAR,
'season_type' => 'REG',
'week_num' => $week,
'game_time_eastern' => $gameTimeEastern,
'home_id' => $home_team,
'visitor_id' => $away_team,
'home_score' => $home_score,
'visitor_score' => $away_score,
'tv' => $tv
);
}
}
}
//output to csv
// create a file pointer connected to the output stream
$fp = fopen('php://output', 'w');
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=nfl_schedule_'.SEASON_YEAR.'.csv');
// output the column headings
fputcsv($fp, array_keys($schedule[0]));
//output the data
foreach ($schedule as $row) {
fputcsv($fp, $row);
}
fclose($fp);