-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_downloader.js
466 lines (419 loc) · 16.3 KB
/
data_downloader.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var moment = require('moment');
var Promise = require('bluebird');
var Gameday = require('./gameday.js');
var GamedayRemoteFetcher = require('./gameday_remote_fetcher.js');
var GamedayUtil = require('./gameday_util.js');
var Batter = require("./batter.js");
var Pitcher = require("./pitcher.js");
var Game = require('./game.js');
function testPromise(year, month, date) {
return new Promise((resolve, reject) => {
console.log('test for: ' + year + "/" + month + "/" + date);
resolve();
});
}
/* This class is used to download data files from the MLB Gameday site to
local storage. The data that is downloaded will be stored into a path that
replicates the MLB Gameday paths, for example here is a sample path for
a specified date:
/components/game/mlb/year_2008/month_04/day_07
*/
module.exports = {
download_xml_for_date : function(year, month, day) {
var that = this;
var day_path = this.get_day_path(year, month, day);
GamedayRemoteFetcher.fetch_epg(year, month, day).then(function(data) {
that.write_file(day_path + "/epg.xml", data);
});
GamedayRemoteFetcher.fetch_scoreboard(year, month, day).then(function(data) {
that.write_file(day_path + "/master_scoreboard.xml", data);
});
GamedayRemoteFetcher.fetch_day_highlights(year, month, day).then(function(data) {
that.write_file(day_path + "/highlights.xml", data);
});
},
// Downloads all data files associated with the game specified by the game id passed in.
download_all_for_game : function(gid) {
return new Promise((resolve, reject) => {
console.log("Downloading game - " + gid);
var that = this;
var downloads = [];
downloads.push(this.download_xml_for_game(gid));
downloads.push(this.download_batters_for_game(gid));
downloads.push(this.download_inning_for_game(gid));
this.download_media_for_game(gid);
this.download_onbase_for_game(gid);
downloads.push(this.download_pitchers_for_game(gid));
Promise.all(downloads).then(function() {
//console.log("All promises resolved for game - " + gid);
resolve();
});
});
},
download_all_for_date : function(year, month, day) {
return new Promise((resolve, reject) => {
var that = this;
var day_path = this.get_day_path(year, month, day);
GamedayRemoteFetcher.fetch_games_page(year, month, day).then(function(data) {
that.write_file(day_path + "/games.html", data);
that.download_xml_for_date(year, month, day);
var games = Game.find_by_date(year, month, day, data);
GamedayUtil.promiseFor(function(count) {
return count < games.length; // condition
}, function(count) {
return that.download_all_for_game(games[count].gid)
.then(function(res) {
return ++count; // step function
});
}, 0).then(function() {console.log("######### ALL DONE for date: " + year + "/" + month + "/" + day); resolve();});
});
});
},
download_all_for_month : function(year, month) {
return new Promise((resolve, reject) => {
var that = this;
var date = new Date(parseInt(year), parseInt(month-1));
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var firstDayMnt = moment(firstDay); // first day of month
var lastDayMnt = moment(lastDay); // last day of month
var currentDate = firstDayMnt;
var endDate = lastDayMnt.add(1, 'days');
GamedayUtil.promiseFor(function(count) {
return currentDate.format('M/D/YYYY') !== endDate.format('M/D/YYYY'); // condition
}, function(count) {
console.log('Downloading ' + year.toString() + '/' + month.toString() + '/' + currentDate.date());
return that.download_all_for_date(year, month, currentDate.date())
.then(function(res) {
return currentDate.add(1, 'days'); // step function
});
}, 0).then(function() {console.log("ALL DONE for month: " + year + "/" + month); resolve();});
});
},
download_all_for_range : function(year, start_month, start_day, end_month, end_day) {
return new Promise((resolve, reject) => {
var that = this;
var startDay = new Date(parseInt(year), parseInt(start_month), parseInt(start_day));
var lastDay = new Date(parseInt(year), parseInt(end_month), parseInt(end_day));
var startDayMnt = moment(startDay); // first day of month
var lastDayMnt = moment(lastDay); // last day of month
var currentDate = startDayMnt;
var endDate = lastDayMnt.add(1, 'days');
GamedayUtil.promiseFor(function(count) {
return currentDate.format('M/D/YYYY') !== endDate.format('M/D/YYYY'); // condition
}, function(count) {
console.log('Downloading ' + currentDate.year().toString() + '/' + currentDate.month().toString() + '/' + currentDate.date());
return that.download_all_for_date(currentDate.year(), currentDate.month(), currentDate.date())
.then(function(res) {
return currentDate.add(1, 'days'); // step function
});
}, 0).then(function() {
console.log("ALL DONE for range: " + year + "/" + start_month + "/" + start_day + " TO " + year + "/" + end_month + "/" + end_day);
resolve();
});
});
},
// Downloads the top-level xml directories for the game specified by the passed in game id.
// The files include:
// bench.xml
// benchO.xml
// boxscore.xml
// emailSource.xml
// eventLog.xml
// game.xml
// game_events.xml
// gamecenter.xml
// gameday_Syn.xml
// linescore.xml
// miniscoreboard.xml
// players.xml
// plays.xml
download_xml_for_game : function(gid) {
return new Promise((resolve, reject) => {
var that = this;
var files = [];
var gid_path = this.get_gid_path(gid);
GamedayRemoteFetcher.fetch_bench(gid).then(function(data) {
files.push(that.write_file(gid_path + "/bench.xml", data));
});
GamedayRemoteFetcher.fetch_bencho(gid).then(function(data) {
files.push(that.write_file(gid_path + "/benchO.xml", data));
});
GamedayRemoteFetcher.fetch_boxscore(gid).then(function(data) {
files.push(that.write_file(gid_path + "/boxscore.xml", data));
});
GamedayRemoteFetcher.fetch_emailsource(gid).then(function(data) {
files.push(that.write_file(gid_path + "/emailSource.xml", data));
});
GamedayRemoteFetcher.fetch_eventlog(gid).then(function(data) {
files.push(that.write_file(gid_path + "/eventLog.xml", data));
});
GamedayRemoteFetcher.fetch_game_xml(gid).then(function(data) {
files.push(that.write_file(gid_path + "/game.xml", data));
});
GamedayRemoteFetcher.fetch_game_events(gid).then(function(data) {
files.push(that.write_file(gid_path + "/game_events.xml", data));
});
GamedayRemoteFetcher.fetch_gamecenter_xml(gid).then(function(data) {
files.push(that.write_file(gid_path + "/gamecenter.xml", data));
});
GamedayRemoteFetcher.fetch_gamedaysyn(gid).then(function(data) {
files.push(that.write_file(gid_path + "/gameday_Syn.xml", data));
});
GamedayRemoteFetcher.fetch_linescore(gid).then(function(data) {
files.push(that.write_file(gid_path + "/linescore.xml", data));
});
GamedayRemoteFetcher.fetch_miniscoreboard(gid).then(function(data) {
files.push(that.write_file(gid_path + "/miniscoreboard.xml", data));
});
GamedayRemoteFetcher.fetch_players(gid).then(function(data) {
files.push(that.write_file(gid_path + "/players.xml", data));
});
GamedayRemoteFetcher.fetch_plays(gid).then(function(data) {
files.push(that.write_file(gid_path + "/plays.xml", data));
});
Promise.all(files).then(function() {
//console.log("all the files were created for " + gid);
resolve();
});
});
},
download_batters_for_game : function(gid) {
return new Promise((resolve, reject) => {
var that = this;
var gid_path = this.get_gid_path(gid);
GamedayRemoteFetcher.fetch_batters_page(gid).then(function(data) {
that.write_file(gid_path + "/batters.html", data);
var batter_path = gid_path + "/batters";
var ids = Batter.get_all_ids_for_game(data);
GamedayUtil.promiseFor(function(count) {
return count < ids.length; // condition
}, function(count) {
return that.fetch_and_write_batter_data(batter_path, gid, ids[count])
.then(function(res) {
return ++count; // step function
});
}, 0).then(function() {/*console.log("All Batters Downloaded for " + gid);*/ resolve();});
});
});
},
download_inning_for_game : function(gid) {
return new Promise((resolve, reject) => {
var that = this;
var game = new Game.Game(gid);
game.get_num_innings().then(function(inningsCount) {
var inn_path = that.get_gid_path(gid) + "/inning"
GamedayUtil.promiseFor(function(count) {
return count <= inningsCount; // condition
}, function(count) {
var path = inn_path + "/inning_" + count.toString() + ".xml";
return that.fetch_and_write_inning_data(path, count, gid)
.then(function(res) {
return ++count; // step function
});
}, 1).then(function() {
that.download_inning_common_for_game(gid, inn_path);
that.download_notification_for_game(gid, inningsCount);
//console.log("All Innings Downloaded for " + gid);
resolve();
});
/*
var files = [];
for (var i=1; i<=inningsCount; i++) {
var path = inn_path + "/inning_" + i.toString() + ".xml";
if (!fs.existsSync(path)) {
files.push(that.fetch_and_write_inning_data(path, i, gid));
}
}
Promise.all(files).then(function() {
that.download_inning_common_for_game(gid, inn_path);
that.download_notification_for_game(gid, inningsCount);
resolve();
});*/
});
});
},
download_inning_common_for_game : function(gid, innPath) {
var that = this;
var innScoresPath = innPath + "/inning_Scores.xml";
//console.log("about to download inning scores");
if (!fs.existsSync(innScoresPath)) {
GamedayRemoteFetcher.fetch_inning_scores(gid).then(function(data) {
that.write_file(innScoresPath, data);
});
}
var innHitPath = innPath + "/inning_hit.xml";
//console.log("about to download inning hit");
if (!fs.existsSync(innHitPath)) {
GamedayRemoteFetcher.fetch_inning_hit(gid).then(function(data) {
that.write_file(innHitPath, data);
});
}
},
download_media_for_game : function(gid) {
var that = this;
var media_path = this.get_gid_path(gid) + "/media";
if (!fs.existsSync(media_path + "/highlights.xml")) {
GamedayRemoteFetcher.fetch_media_highlights(gid).then(function(data) {
that.write_file(media_path + "/highlights.xml", data);
});
}
if (!fs.existsSync(media_path + "/mobile.xml")) {
GamedayRemoteFetcher.fetch_media_mobile(gid).then(function(data) {
that.write_file(media_path + "/mobile.xml", data);
});
}
},
// pass the innings count into this function so that we dont have to rebuild the boxscore in get_num_innings
download_notification_for_game : function(gid, inningsCount) {
return new Promise((resolve, reject) => {
var that = this;
var notif_path = this.get_gid_path(gid) + "/notifications";
var files = [];
for (var i=1; i<=inningsCount; i++) {
if (!fs.existsSync(notif_path + "/notifications_" + i.toString() + ".xml")) {
files.push(that.fetch_and_write_inning_notifcation(gid, i));
}
}
Promise.all(files).then(function() {
if (!fs.existsSync(notif_path + "/notifications_full.xml")) {
GamedayRemoteFetcher.fetch_notifications_full(gid).then(function(data) {
that.write_file(notif_path + "/notifications_full.xml", data);
});
}
resolve();
});
});
},
download_onbase_for_game : function(gid) {
var that = this;
var onbase_path = this.get_gid_path(gid) + "/onbase";
GamedayRemoteFetcher.fetch_onbase_linescore(gid).then(function(data) {
that.write_file(onbase_path + "/linescore.xml", data);
});
GamedayRemoteFetcher.fetch_onbase_plays(gid).then(function(data) {
that.write_file(onbase_path + "/plays.xml", data);
});
},
download_pitchers_for_game : function(gid) {
return new Promise((resolve, reject) => {
var that = this;
var gid_path = this.get_gid_path(gid);
GamedayRemoteFetcher.fetch_pitchers_page(gid).then(function(data) {
that.write_file(gid_path + "/pitchers.html", data);
var pitcher_path = gid_path + "/pitchers";
var ids = Pitcher.get_all_ids_for_game(data);
GamedayUtil.promiseFor(function(count) {
return count < ids.length; // condition
}, function(count) {
return that.fetch_and_write_pitcher_data(pitcher_path, gid, ids[count])
.then(function(res) {
return ++count; // step function
});
}, 0).then(function() {/*console.log("All Pitchers Downloaded for " + gid);*/ resolve();});
});
});
},
fetch_and_write_inning_notifcation : function(gid, inn) {
var that = this;
var notif_path = this.get_gid_path(gid) + "/notifications";
GamedayRemoteFetcher.fetch_notifications_inning(gid, inn).then(function(data) {
that.write_file(notif_path + "/notifications_" + inn.toString() + ".xml", data);
});
},
fetch_and_write_inning_data : function(path, inningNumber, gid) {
return new Promise((resolve, reject) => {
var that = this;
GamedayRemoteFetcher.fetch_inningx(gid, inningNumber).then(function(data) {
that.write_file(path, data).then(function() {
resolve();
})
});
});
},
fetch_and_write_batter_data : function(batter_path, gid, id) {
return new Promise((resolve, reject) => {
var that = this;
GamedayRemoteFetcher.fetch_batter(gid, id).then(function(data) {
that.write_file(batter_path + "/" + id + ".xml", data).then(function() {
resolve();
});
});
});
},
fetch_and_write_pitcher_data : function(pitcher_path, gid, id) {
return new Promise((resolve, reject) => {
var that = this;
GamedayRemoteFetcher.fetch_pitcher(gid, id).then(function(data) {
that.write_file(pitcher_path + "/" + id + ".xml", data).then(function() {
resolve();
})
});
});
},
// Writes the gameday data to the file specified.
// Does not overwrite existing files.
write_file : function(file_path, gd_data) {
return new Promise((resolve, reject) => {
if (gd_data && !fs.existsSync(file_path)) {
var dirPath = path.dirname(file_path);
mkdirp(dirPath, function (err) {
if (err) return err;
fs.writeFile(file_path, gd_data, 'binary', function() {
resolve();
});
});
}
else {
resolve();
}
});
},
get_gid_path : function(gid) {
var gameday_info = GamedayUtil.parse_gameday_id('gid_' + gid);
return Gameday.FILE_BASE_PATH + "/year_" + gameday_info['year'] + "/month_" + gameday_info['month'] + "/day_" + gameday_info['day'] + "/gid_"+gid;
},
get_day_path : function(year, month, day) {
var year = GamedayUtil.convert_digit_to_string(parseInt(year));
var month = GamedayUtil.convert_digit_to_string(parseInt(month));
var day = GamedayUtil.convert_digit_to_string(parseInt(day));
return Gameday.FILE_BASE_PATH + "/year_" + year + "/month_" + month + "/day_" + day;
}
}
/*
require 'gameday_api/game'
require 'gameday_api/batter'
require 'gameday_api/pitcher'
require 'fileutils'
require 'gameday_api/gameday_fetcher'
module GamedayApi
class DataDownloader
FILE_BASE_PATH = 'components/game/mlb'
def tmp_fetch_pages_for_month(year, month, end_day)
start_date = Date.new(year.to_i, month.to_i) # first day of month
if end_day
end_date = Date.new(year.to_i, month.to_i, end_day.to_i)
else
end_date = (start_date >> 1)-1 # last day of month
end
((start_date)..(end_date)).each do |dt|
day = dt.day.to_s
puts day
day_path = get_day_path(year, month, day)
write_file("#{day_path}/games.html", GamedayFetcher.fetch_games_page(year, month, day))
games = Game.find_by_date(year, month, day)
games.each do |game|
tmp_fetch_pages_for_game(game.gid)
end
end
end
def tmp_fetch_pages_for_game(gid)
write_file(get_gid_path(gid) + "/batters.html", GamedayFetcher.fetch_batters_page(gid))
write_file(get_gid_path(gid) + "/pitchers.html", GamedayFetcher.fetch_pitchers_page(gid))
end
end
end
*/