Inital commit

This commit is contained in:
Leon Grünewald 2024-10-01 10:46:53 +02:00
commit dfa58e883b

158
src/main.js Normal file
View file

@ -0,0 +1,158 @@
// User Config starts here
const username = 'Leon'; // Leave string empty to disable filter.
const mediaAttributes = 'loop autoplay controls';
const locale = 'de-DE';
// User Config ends here
const fs = require('node:fs/promises');
const path = require('node:path');
const http = require('node:http');
const url = require('node:url');
const chatData = require(path.join(process.cwd(), 'result.json'));
console.log('Starting data generation...');
const stickerScores = chatData.messages
.filter(message => message.media_type === 'sticker' && message.from.includes(username))
.reduce((scores, message) => {
if (!scores.hasOwnProperty(message.file)) {
scores[message.file] = {score: 1, lastUsed: message.date_unixtime};
return scores;
}
if (scores[message.file].lastUsed < message.date_unixtime) {
scores[message.file].lastUsed = message.date_unixtime;
}
scores[message.file].score++;
return scores;
}, {});
(async () => {
let file = await fs.open('./index.html', 'w');
file.write(`
<!DOCTYPE html>
<head>
<title>Sticker Summarry</title>
<style>
* { box-sizing: border-box; }
h1 {text-align: center;}
body {
width: 100vw;
margin: 0;
padding: 0;
}
.container {
max-width: 1024px;
margin: 0 auto;
}
.container,
.sticker {
display: flex;
position: relative;
flex-flow: row wrap;
}
.sticker {
flex: 0 0 25%;
align-items: flex-end;
padding: 1rem;
border: 1px solid #00000055;
}
.sticker p {
flex: 1;
text-align: center;
}
img, video, tgs-player {
width: 100%;
height: auto;
margin: 0 auto;
aspect-ratio: 1 / 1;
object-fit: contain;
}
</style>
<script src="https://unpkg.com/@lottiefiles/lottie-player@0.4.0/dist/tgs-player.js"></script>
</head>
<body>
<h1>Sticker Gallery</h1>
<div class="container">
`);
let stickerScoreEntries = Object.entries(stickerScores);
stickerScoreEntries
.sort((a, b) => b[1].score - a[1].score);
stickerScoreEntries
.forEach(entry => {
const formattedDate = new Date(entry[1].lastUsed * 1000).toLocaleString(locale);
if (entry[0].endsWith('tgs')) {
file.write(`<div class="sticker animated">
<tgs-player loading="lazy" ${mediaAttributes} src="${entry[0]}"></tgs-player>
<p>${entry[1].score} (Lottie)<br />
${formattedDate}
</p>
</div>`);
return;
}
if (entry[0].endsWith('webm')) {
file.write(`<div class="sticker video">
<video loading="lazy" ${mediaAttributes} src="${entry[0]}"></video>
<p>${entry[1].score} (Video)<br />
${formattedDate}
</p>
</div>`);
return;
}
file.write(`<div class="sticker">
<img loading="lazy" src="${entry[0]}"/>
<p>${entry[1].score} (Image)<br />
${formattedDate}
</p>
</div>`);
});
file.write('</div></body>');
file.close();
console.log('Starting webserver!');
let server = http.createServer({}, async (req, res) => {
let uri = url.parse(req.url);
try {
let data;
if (req.url === '/') {
data = await fs.readFile(path.join(process.cwd(), './index.html'));
res.writeHead(200);
res.write(data);
} else {
data = await fs.readFile(path.join(process.cwd(), decodeURI(uri.path)));
res.writeHead(200);
res.write(data);
}
} catch (e) {
if (e.code === 'ENOENT') {
console.warn('404: ' + uri.path);
res.writeHead(404);
} else {
console.error(e);
res.writeHead(500);
}
res.write(e.toString());
}
res.end();
});
server.listen(3000, 'localhost');
console.log('Done...');
})();