-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.js
83 lines (77 loc) · 3.51 KB
/
fetch.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
var async = require('async');
var request = require('request');
var jsonfile = require('jsonfile');
var colors = require('colors/safe');
var param2 = process.argv[2];
var param3 = process.argv[3];
const season = param2;
const game = param3;
// Additional parameter indicates individual game to fetch
if (process.argv[3] != null) {
const URL = `http://live.nhl.com/GameData/${season}/${game}/PlayByPlay.json`;
request(URL, function (error, response, body) {
if (error || response.statusCode !== 200) {
console.log(colors.red(`Failed to fetch GameID: ${game}`));
return;
}
var importedJSON = JSON.parse(body);
jsonfile.writeFileSync(`data/${season}/${game}.json`, importedJSON);
console.log(colors.green(`Fetching URL: ${URL}`));
console.log(colors.green('Game fetched successfully'));
})
} else {
// Entire season fetch - First: fetch and save JSON season schedule
request(`http://live.nhl.com/GameData/SeasonSchedule-${season}.json`, function (error, response, body) {
if (error || response.statusCode !== 200) {
console.log(colors.red(`Failed to get schedule for season ${season}`));
return;
}
var importedJSON = JSON.parse(body);
jsonfile.writeFileSync(`data/${season}/schedule-${season}.json`, importedJSON);
console.log(colors.green(`Schedule for season ${season} processed successfully`));
// Today's date
var utcDate = new Date().toJSON().slice(0,10);
utcDate = utcDate.replace(/-/g,'');
// Second: fetch and save each JSON play by play games from season schedule
let lastProcessedId;
async.eachSeries(importedJSON, function (row, callback) {
const gameId = row.id;
const gameDate = row.est;
const URL = `http://live.nhl.com/GameData/${season}/${gameId}/PlayByPlay.json`;
lastProcessedId = gameId
request(URL, function (error, response, body) {
if (error) {
callback(error);
} else {
// catch exceptions from either writeFileSync or JSON.parse
try {
// If today's date 'utdDate' is smaller than 'gameDate', game not played yet! Then exit
if ((utcDate - 1) < gameDate.substring(0, 8)) {
console.log(colors.green('All available games fetched successfully'));
return;
} else {
console.log(colors.green(`Fetching URL: ${URL}`));
jsonfile.writeFileSync(`data/${season}/${gameId}.json`, JSON.parse(body));
callback();
}
} catch (err) {
console.log(colors.red(`Failed to fetch GameID: ${gameId}`));
// this will stop processing
callback(err);
}
}
})
}, function (err) {
// if any of the file processing produced an error, err would equal that error
if (err) {
// One of the iterations produced an error.
// All processing will now stop.
console.log(colors.red(`Last attempted GameID: ${lastProcessedId}`));
console.log('detailed error:', err);
return;
} else {
console.log(colors.green('All available games fetched successfully'));
}
})
})
}