This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
save_data.js
243 lines (213 loc) · 6.51 KB
/
save_data.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License'); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/* jshint esversion: 6 */
/**
* This purpose of this script is to save off all MLB data for off-season use.
* Files will written to the data directory with a '-new' suffix.
*
* NOTE: this script should only be run during the season, when MLB
* data is available.
*
* Here are the steps needed to use these new files:
* 1) Run this script
* 2) If files are correct, overwrite existing versions:
* cp data/mlb-teams-new.json data/mlb-teams.json
* cp data/mlb-schedule-new.json data/mlb-schedule.json
* cp data/mlb-standings-new.json data/mlb-standings.json
* 3) Set 'exports.offSeason = true;' in config.json
* 4) Edit run.js function 'getCurrentDate()'. The date returned
* should be set to one day before the date when the new
* MLB data was saved off. Note that months start at 0.
*/
const CONFIG = require('../config.js');
const REQUEST = require('request-promise');
const PROMISE = require('promise');
const MLB_SEASON = CONFIG.MLBSeason;
var mlbTeams;
var mlbTeamsRetrieved = false;
var mlbStandings;
var mlbStandingsRetrieved = false;
var mlbScheduleDates = [];
var scheduleDaysCollected = 0;
var mlbSchedule = [];
var mlbScheduleRetrieved = false;
/**
* Retrieve key to 3rd party MLB data
*/
const MLB_DATA_KEY = CONFIG.MLBFantasySubscriptionKey;
/**
* Get current date
*/
function getCurrentDate() {
var date;
date = new Date(MLB_SEASON, 8, 28);
return date;
}
/**
* Get current MLB team info from MLB Fantasy Data.
*/
function getMlbTeams() {
const options = {
method: 'GET',
uri: 'https://api.fantasydata.net/mlb/v2/JSON/teams',
headers: {
'Host': 'api.fantasydata.net',
'Ocp-Apim-Subscription-Key': MLB_DATA_KEY
}
};
return new PROMISE((resolve, reject) => {
REQUEST(options)
.then(function (response) {
mlbTeams = JSON.parse(response);
var jsonfile = require('jsonfile');
var file = 'data/mlb-teams-new.json';
jsonfile.writeFile(file, mlbTeams, {spaces: 2}, function (err) {
console.error(err);
});
return resolve();
})
.catch(function (err) {
console.log('Unable to retrieve current MLB team info. ', err);
return reject(err);
});
});
}
/**
* Get current MLB standings from MLB Fantasy Data.
*/
function getMlbStandings() {
const options = {
method: 'GET',
uri: 'https://api.fantasydata.net/mlb/v2/JSON/Standings/' + MLB_SEASON,
headers: {
'Host': 'api.fantasydata.net',
'Ocp-Apim-Subscription-Key': MLB_DATA_KEY
}
};
return new PROMISE((resolve, reject) => {
REQUEST(options)
.then(function (response) {
mlbStandings = JSON.parse(response);
var jsonfile = require('jsonfile');
var file = 'data/mlb-standings-new.json';
jsonfile.writeFile(file, mlbStandings, {spaces: 2}, function (err) {
console.error(err);
});
return resolve();
})
.catch(function (err) {
console.log('Unable to retrieve current MLB standings. ', err);
return reject(err);
});
});
}
/**
* Get current MLB schedules from MLB Fantasy Data. Just grab schedules
* from today and for the next week.
*/
function getMlbSchedules() {
var monthNames = [
'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC'
];
var date = getCurrentDate();
return new PROMISE((resolve, reject) => {
for (let i = 0; i < 7; i++) {
/* jshint loopfunc: true */
date.setDate(date.getDate() + 1);
month = date.getMonth();
day = ("0" + date.getDate()).slice(-2);
const options = {
method: 'GET',
uri: 'https://api.fantasydata.net/mlb/v2/JSON/GamesByDate/' + MLB_SEASON + '-' +
monthNames[month] + '-' + day,
headers: {
'Host': 'api.fantasydata.net',
'Ocp-Apim-Subscription-Key': MLB_DATA_KEY
}
};
REQUEST(options)
.then(function (response) {
daySchedule = JSON.parse(response);
if (daySchedule.length > 0) {
console.log('Retrieved schedule for date: ' + daySchedule[0].Day);
// Save each date in array so that they can be sorted
// after all dates are retrieved.
mlbScheduleDates[mlbScheduleDates.length] = daySchedule;
} else {
console.log('Retrieved schedule for date: NO GAMES FOUND');
}
scheduleDaysCollected += 1;
if (scheduleDaysCollected === 7) {
var jsonfile = require('jsonfile');
var file = 'data/mlb-schedule-new.json';
jsonfile.writeFile(file, mlbScheduleDates, {spaces: 2}, function (err) {
console.error(err);
});
return resolve();
}
})
.catch(function (err) {
console.log('Unable to retrieve current MLB schedules. ', err);
return reject(err);
});
}
});
}
/**
* Load all MLB data and save off for off-season use.
*/
function loadData() {
// Generate data to be used during the conversation.
getMlbTeams()
.then(function() {
console.log('Retrieved MLB Teams');
mlbTeamsRetrieved = true;
exitWhenDone();
})
.catch(err => {
throw new Error('Error loading MLB team info');
});
getMlbStandings()
.then(function() {
console.log('Retrieved MLB standings');
mlbStandingsRetrieved = true;
exitWhenDone();
})
.catch(err => {
throw new Error('Error loading MLB standings');
});
getMlbSchedules()
.then(function() {
console.log('Retrieved MLB schedules');
sortSchedule();
mlbScheduleRetrieved = true;
exitWhenDone();
})
.catch(err => {
throw new Error('Error loading MLB schedules');
});
}
/**
* Exit after data is loaded.
*/
function exitWhenDone() {
if (mlbTeamsRetrieved && mlbStandingsRetrieved && mlbScheduleRetrieved) {
process.exit(0);
}
}
// Start by loading MLB data
loadData();