1
0
Fork 0

Add parseunimap script.

main
alberto 1 day ago
parent 58e2abfdca
commit 27b8e7cdf1

@ -105,3 +105,25 @@ Questa repository contiene i seguenti script:
```
Qui un esempio di utilizzo: [Argomenti di G2](https://git.phc.dm.unipi.it/lukefleed/domande-orali/src/branch/master/geoemtria-2)
- `parseunimap` — [@aziis98](https://git.phc.dm.unipi.it/aziis98) + [@alberto](https://git.phc.dm.unipi.it/alberto)
Script che converte il registro delle lezioni dell'Unipi in markdown
Installare:
```bash
bun init
bun install jsdom
bun install node-fetch
```
Per eseguire:
```bash
bun run script.js <registro_link>
```
Una volta ottenuto in output il file markdown, è possibile convertirlo in pdf utilizzando ad
esempio Pandoc tramite il comando
```bash
pandoc fileregistro.md -o fileregistro.pdf
```

@ -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…
Cancel
Save