forked from Sandip124/GreetMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (114 loc) Β· 4.58 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
const express = require("express")
const app = express()
const port = 3000
const uniqueRandomArray = require("unique-random-array");
const morningGreetings = require("./data/greetings.js");
const dayGreetings = require("./data/day.js");
const eveningGreetings = require("./data/evening.js");
const nightGreetings = require("./data/night.js");
const colorPairs = require("./data/colors.js");
const { registerFont, createCanvas } = require('canvas')
const { fillTextWithTwemoji } = require('node-canvas-with-twemoji-and-discord-emoji');
app.get('/',
async (req, res) => {
const {
greet,
heading,
text,
darkmode = false,
w=1200,
h=250
} = req.query;
res.setHeader("Cache-Control", `public, max-age=${5}`);
const currentDate = new Date(getDateFromOffset(getTimeZoneOffset()));
const hours = currentDate.getHours();
let message = "Hello Everyone this is greeter.";
let textMessage = "weeeeeeeeeee weeeeeeeeeee";
if (typeof heading != 'undefined' && heading !== '') {
message = heading;
}
if (typeof text != 'undefined' && text !== '') {
textMessage = text;
}
if (typeof greet != 'undefined' && greet === "true") {
let randomMessage;
if (hours < 12) {
randomMessage = uniqueRandomArray(morningGreetings.greetings)
} else if (hours >= 12 && hours < 18) {
randomMessage = uniqueRandomArray(dayGreetings.greetings)
} else if (hours >= 18 && hours < 21) {
randomMessage = uniqueRandomArray(eveningGreetings.greetings)
} else {
randomMessage = uniqueRandomArray(nightGreetings.greetings)
}
message = randomMessage();
textMessage = randomMessage();
}
let width = 0
let height = 0
if (typeof w != 'undefined' && w !== '') {
width = w;
}
if (typeof h != 'undefined' && h !== '') {
height = h;
}
registerFont("poppins-Regular.ttf", { family: "Poppins", weight: "400", style: "Regular" })
registerFont("poppins-Bold.ttf", { family: "Poppins", weight: "700", style: "Bold" })
const canvas = createCanvas(width, height)
const context = canvas.getContext('2d')
const colors = uniqueRandomArray(colorPairs.colorPairs);
let background = `${colors().background}`;
let foreground = `${colors().foreground}`;
if (typeof darkmode != 'undefined' && darkmode === "true") {
if (darkmode) {
background = `#3b4252`;
foreground = `#FFFFFF`;
}
}
context.fillStyle = background;
roundedRect(context, 0, 0, width, height, 16);
context.font = '700 36px Poppins, Sans-Serif;'
context.textAlign = 'center'
context.textBaseline = 'top'
context.fillStyle = foreground
await fillTextWithTwemoji(context, message, 600, 70);
context.font = '400 18px Poppins, Sans-Serif;'
await fillTextWithTwemoji(context, `${textMessage}`, 600, 150);
const buffer = canvas.toBuffer('image/png')
res.contentType('image/jpeg');
res.send(buffer);
})
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x, y + radius);
ctx.arcTo(x, y + height, x + radius, y + height, radius);
ctx.arcTo(x + width, y + height, x + width, y + height - radius, radius);
ctx.arcTo(x + width, y, x + width - radius, y, radius);
ctx.arcTo(x, y, x, y + radius, radius);
ctx.fill();
ctx.stroke();
}
const getTimeZoneOffset = () => {
const timezoneOffset = new Date().getTimezoneOffset()
const offset = Math.abs(timezoneOffset)
const offsetOperator = timezoneOffset < 0 ? '+' : '-'
const offsetHours = (offset / 60).toString().padStart(2, '0')
const offsetMinutes = (offset % 60).toString().padStart(2, '0')
return `${offsetOperator}${offsetHours}`
}
function getDateFromOffset(offset) {
// create Date object for current location
var d = new Date();
// convert to msec
// subtract local time zone offset
// get UTC time in msec
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
var newDate = new Date(utc + (3600000 * offset));
// return time as a string
return newDate.toLocaleString();
}
app.listen(port, () => {
console.log(`Example app listening on port: ${port}`)
})