-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
62 lines (48 loc) · 1.77 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
const http = require('http');
const url = require('url');
const ArticleCard = require('./src/ArticleCard');
const { userArticles } = require('./src/mediumAPI');
const { asyncForEach } = require('./src/utils');
http.createServer(async (req, res) => {
const reqURL = url.parse(req.url, true);
const { username, limit, bg, text } = reqURL.query;
const colors = { bg: bg, text: text }
const timestamp = Math.floor(Date.now() / 1000);
let articles = [];
if (!username) {
res.write(JSON.stringify({ error: 'Add your medium username as query string' }));
res.end();
return;
}
const responseArticles = await userArticles(`${username}?t=${timestamp}`);
if (!responseArticles || responseArticles.length === 0) {
res.write(JSON.stringify({ error: 'You dont have any medium article' }));
res.end();
return;
}
if (!colors.bg) {
colors.bg = '22272e'
}
if (!colors.text) {
colors.text = 'white'
}
if (limit) {
articles = [...responseArticles.slice(0, limit)];
} else {
articles = [responseArticles[0]];
}
let result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="390px" version="1.2" height="${articles.length * 120}">`;
await asyncForEach(articles, async (article, index) => {
const articleCard = await ArticleCard(article, colors);
result += `<g transform="translate(0, ${index * 120})">${articleCard}</g>`;
});
result += `</svg>`;
res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
res.setHeader('Pragma', 'no-cache');
res.writeHead(200, { 'Content-Type': 'image/svg+xml' });
res.write(result);
res.end();
}).listen(process.env.PORT || 3000, function () {
console.log("server start at port 3000");
});