-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
67 lines (57 loc) · 1.47 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
const express = require("express");
const cors = require("cors");
const fs = require("fs");
const yaml = require("js-yaml");
const https = require("https");
const cron = require("node-cron");
const app = express();
const port = 4444;
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.listen(process.env.PORT || port, () => {
console.log("We are live on port 4444");
});
const url =
"https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml";
cron.schedule("0 0 1 * *", () => {
let myURL = new URL(url);
let body = [];
https
.request(myURL, (res) => {
res.on("data", (chunk) => body.push(chunk));
res.on("end", () =>
fs.writeFile(
"github.yml",
Buffer.concat(body).toString(),
(err, data) => {
if (err) return err;
}
)
);
})
.end();
});
app.get("/", (req, res) => {
try {
let fileContents = fs.readFileSync("./github.yml", "utf8");
let data = yaml.safeLoad(fileContents);
console.log(data);
res.send(data);
} catch (e) {
console.log(e);
}
});
app.get("/lang", (req, res) => {
try {
let fileContents = fs.readFileSync("./github.yml", "utf8");
let data = yaml.safeLoad(fileContents);
const response = {};
for (const entry in data) {
if (data[entry].color) response[entry] = data[entry].color;
}
res.json(response);
} catch (e) {
console.log(e);
}
});