multi-account-post-schedule.../helpers/wiki_osm.mjs

122 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-08-04 14:16:43 +02:00
// https://www.mediawiki.org/wiki/Manual:Random_page
import fetch from "node-fetch"
import rp from "request-promise";
import $ from "cheerio";
2022-08-04 16:30:18 +02:00
import fs from "fs";
import path from 'path';
const __dirname = path.resolve();
function getRandomWikiOSMPage() {
2022-08-04 14:16:43 +02:00
// get all FR tags
// const url = "https://wiki.openstreetmap.org/w/index.php?title=Special:Categories&offset=FR&limit=500"
const url = "https://wiki.openstreetmap.org/wiki/Special:AllPages?from=&to=&namespace=202&limit=500"
// get a random page wich is not a discussion
// const url = "https://wiki.openstreetmap.org/wiki/Special:Random?namespace=all-discussions%3B1%3B2%3B3%3B5%3B7%3B9%3B11%3B120%3B121%3B122%3B123%3B201%3B203&invert=1&limit=10&days=1&enhanced=1&namespace__202_color=c1&urlversion=2"
rp(url)
2022-08-04 16:30:18 +02:00
.then(function (html) {
2022-08-04 14:16:43 +02:00
//success!
const wikiUrls = [];
const filteredHtml = $('.mw-spcontent ul li a', html)
console.log("filteredHtml.length", filteredHtml.length)
// .filter(elem => {
// return (elem.attribs.title.indexOf('description for') === -1)
// })
for (let i = 0; i < filteredHtml.length; i++) {
// const contentCurated = filteredHtml[i]
const contentCurated = filteredHtml[i].attribs.href
wikiUrls.push(contentCurated);
}
console.log(wikiUrls);
return wikiUrls;
})
2022-08-04 16:30:18 +02:00
.catch(function (err) {
2022-08-04 14:16:43 +02:00
//handle error
console.error(err)
});
}
2022-08-04 16:30:18 +02:00
function getRandomElementOfArray(listItems) {
return listItems[Math.floor(Math.random() * listItems.length)]
2022-08-04 14:16:43 +02:00
}
2022-08-04 16:30:18 +02:00
export default function getElementCartographique() {
2022-08-04 14:16:43 +02:00
const url = "https://wiki.openstreetmap.org/wiki/FR:%C3%89l%C3%A9ments_cartographiques"
const titleLink = "#toc a"
const listElementSelector = "#toc a"
// prendre un lien dans la table des matières de la page, extraire l'id du lien.
// prendre une ligne de tableau au hasard correspondant à l'ID de la section
const titleSelector = ""
2022-08-04 16:30:18 +02:00
fs.readFile( __dirname + '/assets/documents/elements_carto_osm.html', 'utf8', function (err, html) {
if (err) {
return console.log(err);
}
// console.log(data);
//success!
const wikiUrls = [];
const listOfLinks = $(titleLink, html)
console.log("filteredHtml.length", listOfLinks.length)
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])
return tableRow;
});
2022-08-04 14:16:43 +02:00
}
function getQuery() {
// var url = "https://wiki.openstreetmap.org/w/api.php";
var url = "https://fr.wikipedia.org/w/api.php";
var params = {
action: "query",
format: "json",
list: "random",
enhanced: "1",
hiderobot: "1",
urlversion: "2",
invert: "1",
rnnamespace: "all-discussions%3B1%3B2%3B3%3B5%3B7%3B9%3B11%3B120%3B121%3B122%3B123%3B201%3B203", // WIKI et FR:
lang: "FR",
rnlimit: "5"
};
url = url + "?&origin=*";
Object.keys(params).forEach(function (key) {
url += "&" + key + "=" + params[key];
});
return fetch(url)
.then(function (response) {
return response.json();
})
.then(function (response) {
const randoms = response.query.random;
for (let ii = 0; ii < randoms.length; ii++) {
// console.log(randoms[ii].title);
console.log("randoms[ii]", randoms[ii])
}
})
.catch(function (error) {
console.log(error);
});
}