-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (151 loc) · 5.46 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const express = require('express')
const fs = require('fs/promises')
const app = express()
const port = 1234
const puppeteer = require('puppeteer');
// const nodeHtmlToImage = require('node-html-to-image')
// const font2base64 = require('node-font2base64')
app.get('/', async (req, res) => {
// console.log(image)
// console.log(req.query);
const url = req.query.url
const username = req.query.username
const texte = req.query.texte
const dataURIBackground = 'data:image/jpeg;base64,' + new Buffer.from(await fs.readFile('./assets/marinotweetBackground.png')).toString('base64');
const dataURICalque = 'data:image/jpeg;base64,' + new Buffer.from(await fs.readFile('./assets/marinotweetCleanNoPaper.png')).toString('base64');
const hour = new Date().toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })
const date = new Date().toLocaleDateString('fr-FR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' })
//random entre 0 et 100
const nbRt = Math.floor(Math.random() * 100)
const nbTwt = Math.floor(Math.random() * 100)
const likes = Math.floor(Math.random() * 100)
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
headless: true
});
const page = await browser.newPage();
await page.setViewport({
width: 3584,
height: 2018,
deviceScaleFactor: 1,
});
await page.setContent(`<html>
<head>
<style>
:root {
font-family: 'Source Sans Pro', 'Lucida Grande', sans-serif;
color: #3B3F43;
}
body {
width: 3584px;
height: 2018px;
background: url({{dataURIBackground}}) no-repeat center center fixed;
}
img {
transform: rotate(-4deg);
position: fixed;
top: 1195px;
left: 1465px;
border-radius: 30000em;
width: 120px;
filter: blur(1.5px);
}
h1{
position: fixed;
top: 1160px;
left: 1600px;
font-size: 45px;
font-weight: bold;
transform: rotate(-4deg);
font-weight: 500;
filter: blur(1.5px);
}
h2{
position: fixed;
top: 1220px;
left: 1605px;
font-size: 30px;
font-weight: bold;
transform: rotate(-4deg);
font-weight: 300;
filter: blur(1.5px);
}
.texte {
position: fixed;
top: 1310px;
left: 1490px;
font-size: 35px;
font-weight: bold;
transform: rotate(-4deg);
width: 740px;
line-height: 1.3;
word-wrap: break-word;
font-weight: 400;
filter: blur(1.5px);
}
.info {
position: fixed;
top: 1570px;
left: 1500px;
font-size: 29px;
transform: rotate(-4deg);
width: 740px;
line-height: 1.3;
word-wrap: break-word;
font-weight: 300;
filter: blur(1.5px);
}
footer {
position: fixed;
top: 1675px;
left: 1500px;
transform: rotate(-4deg);
font-size: 29px;
filter: blur(1.5px);
}
div {
position: fixed;
width: 3584px;
height: 2018px;
background: url({{dataURICalque}}) no-repeat center center fixed;
}
span {
color: red;
filter: blur(1.5px);
}
</style>
</head>
<body>
<img src="{{imageAvatar}}" />
<h1>{{username}}</h1>
<h2>@{{username}}</h2>
<p class="texte">{{texte}}</p>
<p class="info">{{hour}}ㆍ{{date}}</p>
<footer><b>{{nbRt}}</b> Retweets <b>{{nbTwt}}</b> Tweets cités <b>{{likes}}</b> j'aime</footer>
<div></div>
</body>
</html>
`
.replace('{{dataURIBackground}}', dataURIBackground)
.replace('{{dataURICalque}}', dataURICalque)
.replace('{{imageAvatar}}', url)
.replace(/{{username}}/g, username)
.replace('{{texte}}', texte)
.replace('{{hour}}', hour)
.replace('{{date}}', date)
.replace('{{nbRt}}', nbRt)
.replace('{{nbTwt}}', nbTwt)
.replace('{{likes}}', likes)
);
const buffer = await page.screenshot({
encoding: 'binary',
type: 'jpeg',
quality: 100
});
await browser.close();
res.writeHead(200, { 'Content-Type': 'image/png' });
res.end(buffer, "binary");
});
app.listen(port, () => {
console.log(`Serveur ecoute sur le port ${port}`)
})