89 lines
No EOL
2.9 KiB
JavaScript
89 lines
No EOL
2.9 KiB
JavaScript
const express = require('express');
|
|
const app = express();
|
|
const http = require('http');
|
|
const server = http.createServer(app);
|
|
const path = require('path');
|
|
const { Server } = require("socket.io");
|
|
const io = new Server(server);
|
|
const { Readable } = require('stream');
|
|
const { parse } = require('node-html-parser');
|
|
|
|
const names = {};
|
|
const getName = async (category = "Informatica") => {
|
|
const altre = ['Anarchici+italiani', 'Informatica', 'Distretti+dell%27Irlanda+del+Nord+soppressi']
|
|
|
|
const page = await fetch("https://it.wikipedia.org/wiki/Speciale:CasualeInCategoria", {
|
|
"credentials": "include",
|
|
"headers": {
|
|
"User-Agent": "hacklabbo.indivia.net/hackordiye22 crawler",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
|
"Accept-Language": "en-US,en;q=0.5",
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
"Upgrade-Insecure-Requests": "1",
|
|
"Sec-Fetch-Dest": "document",
|
|
"Sec-Fetch-Mode": "navigate",
|
|
"Sec-Fetch-Site": "same-origin",
|
|
"Sec-Fetch-User": "?1"
|
|
},
|
|
"referrer": "https://it.wikipedia.org/wiki/Speciale:CasualeInCategoria",
|
|
"body": `title=Speciale%3ACasualeInCategoria&wpcategory=${category}&wpEditToken=%2B%5C&redirectparams=`,
|
|
"method": "POST",
|
|
"mode": "cors"
|
|
});
|
|
const pageText = await page.text();
|
|
const name = parse(pageText)
|
|
.querySelector('title')
|
|
.textContent
|
|
.split(' - ')[0]
|
|
.toLowerCase()
|
|
.split(' ')
|
|
.join('-')
|
|
// remove parentheses
|
|
.replaceAll('(', '')
|
|
.replaceAll(')', '');
|
|
const link = parse(pageText)
|
|
.querySelector('link[rel="canonical"]')
|
|
.getAttribute('href');
|
|
if (name.includes("categoria")) {
|
|
return getName(altre[2]);
|
|
}
|
|
return { name, link };
|
|
}
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
|
});
|
|
|
|
app.get('/waiting', (req, res) => {
|
|
res.sendStatus(418);
|
|
});
|
|
|
|
app.get('/users', (req, res) => {
|
|
res.send(JSON.stringify(Object.values(names)));
|
|
});
|
|
|
|
io.on('connection', async (socket) => {
|
|
const { name: nameForUser, link } = await getName();
|
|
names[socket.id] = nameForUser;
|
|
// Emit username and chat message
|
|
socket.emit('username', `${nameForUser}@@@${link}`);
|
|
socket.emit('chat message', `Ciao ${nameForUser}!`);
|
|
|
|
console.info(`${nameForUser} connected`);
|
|
|
|
socket.on('chat message', (msg) => {
|
|
const chatMessage = `${nameForUser}: ${msg}`;
|
|
console.log(`${chatMessage}`);
|
|
io.emit('chat message', `${nameForUser}: ${msg}`);
|
|
|
|
});
|
|
|
|
socket.on('disconnect', () => {
|
|
console.info(`${nameForUser} disconnected`);
|
|
delete names[socket.id];
|
|
});
|
|
});
|
|
|
|
server.listen(3000, () => {
|
|
console.log('listening on *:3000');
|
|
}); |