forked from phc/dm-scripts
Add parseunimap script.
parent
58e2abfdca
commit
27b8e7cdf1
@ -0,0 +1,58 @@
|
||||
import {createWriteStream} from 'fs';
|
||||
import {JSDOM} from "jsdom";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const url = process.argv[2];
|
||||
if (!url) {
|
||||
console.error("Usage: node script.js <registro_link>");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const response =
|
||||
await fetch(url, {headers : {"User-Agent" : "Mozilla/5.0"}});
|
||||
const buffer = await response.arrayBuffer();
|
||||
const isoHtml = Buffer.from(buffer, "binary").toString("latin1");
|
||||
|
||||
const dom = new JSDOM(isoHtml);
|
||||
const document = dom.window.document;
|
||||
|
||||
let ol = document.querySelector("ol");
|
||||
if (!ol) {
|
||||
console.error("No <ol> found on the page");
|
||||
return;
|
||||
}
|
||||
|
||||
ol.querySelectorAll("a, i").forEach(el => el.remove());
|
||||
|
||||
const results = [...ol.children ]
|
||||
.map(li => li.textContent.match(/\w+:(.+)/sm))
|
||||
.filter(match => match)
|
||||
.map(match => match[1].trim())
|
||||
.map((x, index) => (index + 1) + ". " + x + " \n");
|
||||
|
||||
const writeStream = createWriteStream("registro.md")
|
||||
const pathName = writeStream.path;
|
||||
writeStream.on('error', function(err) { /* error handling */ });
|
||||
const head = "# Registro delle lezioni \n\n";
|
||||
writeStream.write(head)
|
||||
results.forEach(value => writeStream.write(`${value}`));
|
||||
// the finish event is emitted when all data has been flushed from the
|
||||
// stream
|
||||
writeStream.on(
|
||||
'finish',
|
||||
() => { console.log(`Registro salvato nel file ${pathName}`); });
|
||||
|
||||
// handle the errors on the write process
|
||||
writeStream.on(
|
||||
'error',
|
||||
(err) => {console.error(
|
||||
`There is an error writing the file ${pathName} => ${err}`)});
|
||||
|
||||
// close the stream
|
||||
writeStream.end();
|
||||
} catch (error) {
|
||||
console.error("Error fetching or processing page:", error);
|
||||
}
|
||||
})();
|
Loading…
Reference in New Issue