get random osm element infos from scraped html

This commit is contained in:
Tykayn 2022-08-04 17:35:20 +02:00 committed by tykayn
parent 2ed836b043
commit 9f98a60ffe

View File

@ -7,6 +7,7 @@ import $ from "cheerio";
import fs from "fs"; import fs from "fs";
import path from 'path'; import path from 'path';
const __dirname = path.resolve(); const __dirname = path.resolve();
function getRandomWikiOSMPage() { function getRandomWikiOSMPage() {
@ -39,6 +40,10 @@ function getRandomWikiOSMPage() {
}); });
} }
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
function getRandomElementOfArray(listItems) { function getRandomElementOfArray(listItems) {
return listItems[Math.floor(Math.random() * listItems.length)] return listItems[Math.floor(Math.random() * listItems.length)]
} }
@ -52,7 +57,7 @@ export default function getElementCartographique() {
const titleSelector = "" const titleSelector = ""
fs.readFile( __dirname + '/assets/documents/elements_carto_osm.html', 'utf8', function (err, html) { fs.readFile(__dirname + '/assets/documents/elements_carto_osm.html', 'utf8', function (err, html) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
@ -66,16 +71,62 @@ export default function getElementCartographique() {
const selectedTocLink = getRandomElementOfArray(listOfLinks).attribs.href; const selectedTocLink = getRandomElementOfArray(listOfLinks).attribs.href;
console.log("selectedTocLink", selectedTocLink) console.log("selectedTocLink", selectedTocLink)
const tableRow = getRandomElementOfArray($(`${selectedTocLink}`, html)).parents('.wikitable') const listOfTableRows = $('.wikitable', html);
console.log("tableRow.length", tableRow.length) let keys = Object.keys(listOfTableRows);
// const tableCells = $('td', tableRow) let randNumber = randomIntFromInterval(1, keys.length)
// console.log("tableRow.length", tableRow.length) // let randNumber = 3
// console.log("tag key value", tableCells[1]) console.log("rand", randNumber)
// console.log("Description", tableCells[3]) const foundLine = $(listOfTableRows[randNumber]).find('tr');
// console.log("icone", tableCells[4])
// console.log("photo", tableCells[5])
return tableRow; // console.log("randomTable",foundTable)
// on regarde les lignes du tableau
keys = Object.keys(foundLine);
randNumber = randomIntFromInterval(0, keys.length)
console.log("lignes de tableau", keys.length)
console.log("ligne sélectionnée", randNumber)
let cells = $(foundLine[randNumber]).find('td')
keys = Object.keys(cells);
console.log("cellules trouvées", keys.length)
let result = {
key: '',
value: '',
description: ''
}
cells.each((i,element)=>{
console.log("cell element", i, $(element).text().trim())
if(i===0){
result.key = $(element).text().trim();
}
if(i===1){
result.value = $(element).text().trim();
}
if(i===3){
result.description = $(element).text().trim();
}
// let cell = $(element).find("td");
// console.log($(cell[0]));
})
// const tableRow = foundTable[randNumber] // get one of all tables but the TOC
// console.log("keys", keys)
// console.log("foundTable[0]", foundTable[0].text())
// console.log("foundTable.attribs", foundTable.attribs)
// console.log("tableRow", $( 'td' ,foundTable).text())
// console.log("tableRow",tableRow.data)
// const tableCells = $('td', tableRow)
// console.log("tableRow.length", tableCells.length)
// console.log("tag key value", cells[1].text())
// console.log("Description", cells[3])
// // console.log("icone", tableCells[4])
// console.log("photo", cells[5].find('img')?.attribs.href)
result.link = `https://wiki.openstreetmap.org/wiki/FR:Tag:${result.key}=${result.value}`
console.log("result", result)
return result;
}); });