-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
109 lines (100 loc) · 2.4 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
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
import axios from "axios";
import config from "./config.js";
import storage from "node-persist";
import { createContestObject } from "./utils.js";
await storage.init({ dir: ".node-persist/storage" });
let dataPool = await storage.getItem("dataPool");
async function seed() {
try {
if (!dataPool) {
let { data } = await axios.get(config.url);
await storage.setItem("dataPool", data);
dataPool = data;
console.info("\x1b[36m%s\x1b[0m", "==> Fetch Successful");
} else {
console.log("\x1b[35m==> Using cached DataPool\x1b[0m");
}
} catch (err) {
console.log("\x1b[31m==> Fetch Unsuccessful\x1b[0m");
}
}
async function everyHour() {
try {
await axios.get("https://illicit-bots.herokuapp.com/");
let { data } = await axios.get(config.url);
let ret = [];
if (!dataPool) {
dataPool = data;
return data;
}
data.forEach((ele) => {
let found = dataPool.find((each) => {
return each.name === ele.name;
});
if (!found) {
ret.push(ele);
}
});
await storage.setItem("dataPool", data);
return ret;
} catch (error) {
console.log("\x1b[31m==> fetch unsuccessful", error, "\x1b[0m");
}
}
function getRunning() {
let contests = [];
dataPool.forEach((ele) => {
if (ele.status === "CODING") {
let contest = createContestObject(ele);
contests.push(contest);
}
});
return contests;
}
function getByWebsite(name) {
let contests = [];
dataPool.forEach((ele) => {
if (
ele.site.toLowerCase().split(" ").join("") === name ||
ele.url.includes(name)
) {
let contest = createContestObject(ele);
contests.push(contest);
}
});
return contests;
}
function getInHours(hrs) {
let contests = [];
dataPool.forEach((ele) => {
let contest = createContestObject(ele);
if (contest.hrs_until === hrs) contests.push(contest);
});
return contests;
}
function getAll() {
let contests = [];
dataPool.forEach((ele) => {
let contest = createContestObject(ele);
if (contest.hrs_until > 0) contests.push(contest);
});
return contests;
}
function getByName(name) {
let contests = [];
dataPool.forEach((data) => {
if (data.name.toLowerCase().includes(name.toLowerCase())) {
contests.push(data);
}
});
return contests;
}
export {
seed,
everyHour,
getRunning,
getAll,
getByWebsite,
getInHours,
getByName,
};