-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (76 loc) · 2.65 KB
/
index.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
const axios = require("axios");
const moment = require("moment");
const fs = require("fs");
const year = new Date().getFullYear();
const url = `https://stats.nba.com/stats/internationalbroadcasterschedule?LeagueID=00&Season=2020&RegionID=11`
let args = process.argv.slice(2).map(g => g.toLowerCase());
let output = "terminal";
let mode = "double";
const indexOut = args.indexOf("-o");
const indexMode = args.indexOf("-m");
if (indexOut != -1)
output = args[indexOut + 1] + ".csv";
if (indexMode != -1)
mode = args[indexMode + 1];
const maxIndex = Math.max(indexOut, indexMode);
args = args.slice(maxIndex < 0 ? 0 : maxIndex + 2);
if (mode === "single" && args.length)
args = args.join(" ").split(" ");
console.log("Output: ", output);
console.log("Modo: ", mode);
console.log("Argumentos de busca: ", args);
console.log();
convertDate = (date, time) => moment(date + " " + time, "MM/DD/YYYY hh:mm A").add(3, "hours").format("MM/DD/YYYY");
convertTime = time => moment(time, "hh:mm A").add(3, "hours").format("hh:mm A");
getData = async () => {
console.log("Recuperando dados de http://global.nba.com...");
return axios.get(url)
.then(response => response.data.resultSets[1].CompleteGameList)
.catch(error => {
console.log(error);
});
};
extractNecessaryData = data => {
console.log("Extraindo dados necessarios...");
return data.map(g => ({
homeTeam: g.htNickName,
awayTeam: g.vtNickName,
startDate: g.date,
startTime: g.time,
endDate: convertDate(g.date, g.time),
endTime: convertTime(g.time),
broadcaster: g.broadcasterName,
}))
};
filterGames = (data, args) => {
console.log("Filtrando os jogos...");
if (args.length == 0)
return data;
return data.filter(game => {
let teams = ([game.htShortName, game.htNickName, game.vtShortName, game.vtNickName]).map(g => g.toLowerCase());
if (mode === "single")
teams = teams.join(" ").split(" ");
return args.some(arg => teams.includes(arg));
});
};
dataStringfy = (data) => {
return `${data.homeTeam} @ ${data.awayTeam},${data.startDate},${data.startTime},${data.endDate},${data.endTime},${data.broadcaster}`;
};
header = "Subject,Start Date,Start Time,End Date,End Time,Description";
main = async () => {
const data = await getData();
const filteredData = filterGames(data, args);
const games = extractNecessaryData(filteredData);
let resultString = header;
games.forEach(g => resultString += "\n" + dataStringfy(g));
if (output === "terminal")
console.log("Gerando saída...\n\n" + resultString);
else {
console.log(`Gerando arquivo ${output}...`);
fs.writeFileSync(output, resultString);
console.log("Pronto!");
}
}
main()
.then()
.catch(err => console.log("Ops, Houve algo errado:\n", err));