-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
148 lines (129 loc) · 5.17 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import express from 'express';
import path from 'path';
import NodeCache from 'node-cache';
import * as libs from './libs.js';
import * as render from './render.js';
import * as api from './api.js';
const cacheControl = new NodeCache({ stdTTL: 600, checkperiod: 600, deleteOnExpire: true });
const app = express();
app.use('/', express.static(path.join(process.cwd(), '/static')));
app.get('/card', async function (req, res) {
res.set({
'Content-Type': 'image/svg+xml',
'Cache-Control': 'public, max-age=3600'
});
let username = req.query.user ?? '';
const playmode = req.query.mode ?? 'std';
const isMini = req.query.mini != undefined && req.query.mini == 'true';
const includeSkills = req.query.skills != undefined && req.query.skills == 'true';
const cycleSkillsStats = req.query.cycleskillsstats != undefined && req.query.cycleskillsstats == 'true' && includeSkills;
const exampleMode = req.query.example != undefined && req.query.example == 'true';
if (exampleMode) {
username = '@example';
}
let userData, avatarBase64, userCoverImage;
let cacheKey = `${username}|${playmode}|${includeSkills}`;
if (req.headers['cache-control'] != 'no-cache' && cacheControl.has(cacheKey)) {
({ userData, avatarBase64, userCoverImage } = cacheControl.get(cacheKey));
} else {
userData = await api.getUser(username, playmode, !isMini, includeSkills);
if (userData.error) return res.send(render.getErrorSVG('Error: ' + userData.error));
avatarBase64 = await api.getImageBase64(userData.user.avatar_url);
userCoverImage = await api.getImage(userData.user.cover_url);
cacheControl.set(cacheKey, { userData, avatarBase64, userCoverImage });
}
let blur = 0;
if (req.query.blur != undefined && req.query.blur == '') {
blur = 6;
} else if (req.query.blur != undefined) {
blur = parseFloat(req.query.blur);
}
const flop = req.query.flop != undefined;
let userCoverImageBase64, width, height;
if (isMini) {
userCoverImageBase64 = await libs.getResizedCoverBase64(userCoverImage, 400, 120, blur, flop);
[width, height] = [400, 120];
} else {
userCoverImageBase64 = await libs.getResizedCoverBase64(userCoverImage, 550, 120, blur, flop);
[width, height] = [550, 320];
}
const margin = (req.query.margin ?? '0,0,0,0').split(',').map((x) => parseInt(x));
const showMemory = req.query.skillmemory != undefined;
const showFiguresForSkills = req.query.skillfigures != undefined;
const showSkillTags = req.query.skilltags != 'false';
userData.options = {
language: req.query.lang ?? 'cn',
animation: req.query.animation != undefined && req.query.animation != 'false',
size: {
width: parseFloat(req.query.w ?? width),
height: parseFloat(req.query.h ?? height)
},
round_avatar: req.query.round_avatar != undefined && req.query.round_avatar != 'false',
color_hue: parseInt(req.query.hue ?? 333),
margin,
includeSkills,
cycleSkillsStats,
skillsPlot: {
showMemory,
showFiguresForSkills,
showSkillTags
}
};
const svg = isMini
? render.getRenderedSVGMini(userData, avatarBase64, userCoverImageBase64)
: render.getRenderedSVGFull(userData, avatarBase64, userCoverImageBase64);
res.send(svg);
});
app.get('/skills', async function (req, res) {
res.set({
'Content-Type': 'image/svg+xml',
'Cache-Control': 'public, max-age=3600'
});
let username = req.query.user ?? '';
const playmode = 'std';
const exampleMode = req.query.example != undefined && req.query.example == 'true';
if (exampleMode) {
username = '@example';
}
let userData, avatarBase64, userCoverImage;
let cacheKey = `${username}|${playmode}|${true}`;
if (req.headers['cache-control'] != 'no-cache' && cacheControl.has(cacheKey)) {
({ userData, avatarBase64, userCoverImage } = cacheControl.get(cacheKey));
} else {
userData = await api.getUser(username, playmode, false, true);
if (userData.error) return res.send(render.getErrorSVG('Error: ' + userData.error));
avatarBase64 = await api.getImageBase64(userData.user.avatar_url);
userCoverImage = await api.getImage(userData.user.cover_url);
cacheControl.set(cacheKey, { userData, avatarBase64, userCoverImage });
}
let blur = 0;
if (req.query.blur != undefined && req.query.blur == '') {
blur = 6;
} else if (req.query.blur != undefined) {
blur = parseFloat(req.query.blur);
}
const flop = req.query.flop != undefined;
let userCoverImageBase64, width, height;
userCoverImageBase64 = await libs.getResizedCoverBase64(userCoverImage, 400, 65, blur, flop);
[width, height] = [400, 250];
const margin = (req.query.margin ?? '0,0,0,0').split(',').map((x) => parseInt(x));
const showMemory = req.query.skillmemory != undefined;
userData.options = {
language: req.query.lang ?? 'cn',
animation: req.query.animation != undefined && req.query.animation != 'false',
size: {
width: parseFloat(req.query.w ?? width),
height: parseFloat(req.query.h ?? height)
},
round_avatar: req.query.round_avatar != undefined && req.query.round_avatar != 'false',
color_hue: parseInt(req.query.hue ?? 333),
margin,
rankingDisplay: req.query.ranking_display ?? 'global',
skillsPlot: {
showMemory
}
};
const svg = render.getRenderedSVGSkillOnly(userData, avatarBase64, userCoverImageBase64);
res.send(svg);
});
app.listen(process.env.PORT || 3000);