This repository has been archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (88 loc) · 2.99 KB
/
app.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
import express from 'express';
import axios from 'axios';
import { setupCache } from 'axios-cache-adapter'
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const cache = setupCache({
maxAge: 21600
})
const api = axios.create({
adapter: cache.adapter
})
app.get('/', (req, res) => {
res.status(200).send('Welcome to <a href="https://github.com/tobimori/streamdeck-shields">StreamDeck Shields.io Custom Endpoint</a> by tobimori 👋')
});
app.get('/plain/downloads/:id', (req, res) => {
console.log(`[${new Date()}] Received GET /plain/downloads/${req.params.id}`);
api({ 'url': 'https://appstore.elgato.com/streamDeckPlugin/catalog.json', method: 'get' })
.then(async (response) => {
let downloads;
for (let e of response.data.plugins) {
if (e.identifier === req.params.id) {
downloads = `${e.downloads}`;
break;
}
};
downloads ? res.status(200).send(downloads) : res.status(404).send('identifier not found');
})
.catch(function (error) {
res.status(500).send(error);
console.log(`[${new Date()}] 500 Internal Server Error`, error);
})
});
app.get('/json/downloads/:id', (req, res) => {
console.log(`[${new Date()}] Received GET /json/downloads/${req.params.id}`);
api({ 'url': 'https://appstore.elgato.com/streamDeckPlugin/catalog.json', method: 'get' })
.then(async (response) => {
let downloads;
for (let e of response.data.plugins) {
if (e.identifier === req.params.id) {
downloads = `${e.downloads}`;
break;
}
};
downloads ? res.status(200).send({
"identifier": req.params.id,
"downloads": downloads,
}) : res.status(404).send({
"error": "identifier not found",
})
})
.catch(function (error) {
res.status(500).send({
"error": error
})
console.log(`[${new Date()}] 500 Internal Server Error`, error);
})
});
app.get('/shields/downloads/:id', (req, res) => {
console.log(`[${new Date()}] Received GET /shields/downloads/${req.params.id}`);
api({'url': 'https://appstore.elgato.com/streamDeckPlugin/catalog.json', method: 'get'})
.then(async (response) => {
let downloads = "error whilst retrieving data";
for (let e of response.data.plugins) {
if (e.identifier === req.params.id) {
downloads = `${e.downloads}`;
break;
}
};
res.status(200).send({
"schemaVersion": 1,
"label": "downloads",
"message": downloads,
"color": downloads === "error whilst retrieving data" && "critical" || "blue",
"style": "flat",
"isError": downloads === "error whilst retrieving data",
})
})
.catch(function (error) {
res.status(500).send({
"error": error
})
console.log(`[${new Date()}] 500 Internal Server Error`, error);
})
});
app.listen(process.env.PORT, () => {
console.log(`[${new Date()}] Server started on port ${process.env.PORT}`)
});