-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcric-cli.js
81 lines (70 loc) · 1.91 KB
/
cric-cli.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
const fetch = require("node-fetch");
const yargs = require("yargs");
require("dotenv").config();
const URL = process.env.URL;
const API_KEY = process.env.API_KEY;
const routes = {
MATCHES: "matches",
SCORE: "cricketScore",
SCORECARD: "fantasySummary"
};
const filterMatches = (match) => {
const today = new Date();
const matchDate = new Date(match["date"]);
if (today.setHours(0, 0, 0, 0) === matchDate.setHours(0, 0, 0, 0)) {
return true;
} else {
return false;
}
};
const mapMatches = (match) => ({
id: match.unique_id,
date: match.date.split("T")[0],
team_one: match["team-1"],
team_two: match["team-2"],
type: match.type,
match_started: match.matchStarted,
});
const getMatches = async () => {
const matchesUrl = `${URL}/${routes.MATCHES}?apikey=${API_KEY}`;
const response = await fetch(matchesUrl);
const data = await response.json();
const matches = data["matches"];
const filteredMatches = matches.filter(filterMatches).map(mapMatches);
console.table(filteredMatches);
};
const getScore = async (matchID) => {
const scoreUrl = `${URL}/${routes.SCORE}?apikey=${API_KEY}&unique_id=${matchID}`;
const response = await fetch(scoreUrl);
const data = await response.json();
const score = data["score"];
console.log(`Summary: ${score}`);
};
yargs
.scriptName("cric-cli")
.usage("$0 <cmd> [args]")
.command(
"matches [format]",
"List all the current matches based on the format",
(yargs) => {
yargs.positional("format", {
type: "string",
default: "ALL",
describe: "filter matches",
});
},
(argv) => getMatches(argv.format)
)
.command(
"score [id]",
"List score for the match corresponding to the given id",
(yargs) => {
yargs.positional("id", {
id: "number",
default: -1,
describe: "unique id for a match",
});
},
(argv) => getScore(argv.id)
)
.help().argv;