-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (42 loc) · 1.37 KB
/
index.js
File metadata and controls
55 lines (42 loc) · 1.37 KB
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
const app = require('express')();
const jimp = require('jimp');
const axios = require('axios');
const cheerio = require('cheerio');
const https = require('https');
const fs = require('fs');
app.get('/', (req, res) => {
res.send('it works');
});
app.get('/github-pro/:id', async (req, res) => {
const { params: { id }} = req;
const profileUrl = `https://github.com/${id}`;
console.log(id);
const originalFilePath = `${id}.jpeg`;
const resultFilePath = `${id}_result.jpeg`;
if (fs.existsSync(resultFilePath)) {
res.download(resultFilePath, 'result.jpeg');
} else {
try {
const { status: profileStatus, data: profileHtml } = await axios.get(profileUrl);
const $ = cheerio.load(profileHtml);
const imgUrl = $('a.u-photo.d-block').attr('href');
const { status: imageStatus, data: imageRaw } = await axios.get(imgUrl, { responseType: 'arraybuffer' });
fs.writeFileSync(originalFilePath, imageRaw);
} catch (err) {
console.log(err);
}
Promise.all([
jimp.read(`${id}.jpeg`),
jimp.read('pro_banner.png')
])
.then(([profileImage, banner]) => {
profileImage.resize(400, 400).composite(banner, 0, 300);
return profileImage.writeAsync(resultFilePath);
})
.then(() => {
res.download(resultFilePath, 'result.jpeg');
})
.catch(console.log);
}
});
app.listen(4000);