-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
102 lines (91 loc) · 3.13 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
100
101
102
require("./src/config");
const { asyncForEach } = require("./src/utils");
const mediumCard = require("./src/card");
const { getUserData } = require("./api/api");
var express = require("express");
var app = express();
app.use(express.json());
app.get("/latest", async (request, response) => {
try {
if (!request.query.username) {
response.write(
JSON.stringify({
error: "username is required",
})
);
response.end();
return;
}
const username = request.query.username;
console.log(request.query);
const offset = request.query.offset || 0;
const width = request.query.width || config.card.width;
const height = request.query.height || config.card.height;
const limit = request.query.limit == null ? 1 :
request.query.limit <= 10
? request.query.limit
: false || config.default.limit;
request.query.width = width;
request.query.height = height;
var resultData = await getUserData(username);
let result = `<svg>`;
if (resultData.length < limit) limit = resultData.length;
result = `<svg xmlns="http:https://www.w3.org/2000/svg" xmlns:xlink="http:https://www.w3.org/1999/xlink"
width="${
(limit == 1 ? width : 2 * width) +
config.default.margin_left +
config.card.spacing
}"
version="1.2"
height="${
Math.round(limit / 2) * height +
config.default.margin_top * 2 +
config.card.spacing * Math.floor(limit / 2)
}"
viewBox="0 0 ${
(limit == 1 ? width : 2 * width) +
config.default.margin_left +
config.card.spacing
} ${
Math.round(limit / 2) * height +
config.default.margin_top * 2 +
config.card.spacing * Math.floor(limit / 2)
}">`;
resultData = resultData.slice(offset, offset + limit);
await asyncForEach(
resultData,
request.query,
async (blog, index, settings) => {
if (index >= limit) {
return;
}
const mediumCardObj = await mediumCard(blog, settings, index);
result += `<g requiredFeatures="http:https://www.w3.org/Graphics/SVG/feature/1.2/#TextFlow" transform="translate(${
(index % 2 ? width + config.card.spacing : 0) +
config.default.margin_left
}, ${
Math.floor(index / 2) * height +
config.default.margin_top +
(index > 1 ? config.card.spacing * Math.floor(index / 2) : 0)
})">${mediumCardObj}</g>`;
}
);
result += `</svg>`;
response.setHeader(
"Cache-Control",
"public, no-cache, no-store, must-revalidate"
);
response.setHeader("Expires", "-1");
response.setHeader("Pragma", "no-cache");
response.writeHead(200, { "Content-Type": "image/svg+xml" });
response.write(result);
response.end();
} catch (error) {
console.log(error);
response.send("Error while fetching the data" + error);
}
});
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log("Server listening " + port);
});