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 path from 'path';
const __dirname = path.resolve();
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) {
return listItems[Math.floor(Math.random() * listItems.length)]
}
@ -66,16 +71,62 @@ export default function getElementCartographique() {
const selectedTocLink = getRandomElementOfArray(listOfLinks).attribs.href;
console.log("selectedTocLink", selectedTocLink)
const tableRow = getRandomElementOfArray($(`${selectedTocLink}`, html)).parents('.wikitable')
console.log("tableRow.length", tableRow.length)
// const tableCells = $('td', tableRow)
// console.log("tableRow.length", tableRow.length)
// console.log("tag key value", tableCells[1])
// console.log("Description", tableCells[3])
// console.log("icone", tableCells[4])
// console.log("photo", tableCells[5])
const listOfTableRows = $('.wikitable', html);
let keys = Object.keys(listOfTableRows);
let randNumber = randomIntFromInterval(1, keys.length)
// let randNumber = 3
console.log("rand", randNumber)
const foundLine = $(listOfTableRows[randNumber]).find('tr');
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;
});