check existence of wiki osm page

This commit is contained in:
Tykayn 2022-08-08 15:47:09 +02:00 committed by tykayn
parent 4933e2c904
commit 15bb11bf1e
2 changed files with 59 additions and 35 deletions

View File

@ -1,12 +1,21 @@
/**
* lieu l'on définit les descriptions à ajouter aux dossiers contenant des images àposter de façon random.
* Ce descriptif sera ajouté avec chaque post d'image
*/
export default { export default {
folders : [ folders : [
{ {
name: 'default', name: 'default',
descriptions: '' descriptions: '',
author: 'curator'
}, },
{ {
name: 'default', name: 'tykayn-sketches',
descriptions: '' descriptions: 'Sketch'
},
{
name: 'memes',
descriptions: 'meme'
}, },
] ]
} }

View File

@ -4,14 +4,17 @@ import rp from "request-promise";
import $ from "cheerio"; import $ from "cheerio";
import fs from "fs"; import fs from "fs";
import path from 'path'; import path from 'path';
import {getRandomElementOfArray, randomIntFromInterval, sendPostMastodon} from "./utils.js"; import {randomIntFromInterval, sendPostMastodon} from "./utils.js";
const __dirname = path.resolve(); const __dirname = path.resolve();
const selectionOverrideOfSectionTable = 2; const selectionOverrideOfSectionTable = 2;
const selectionOverrideOfLineTable = 7; const selectionOverrideOfLineTable = 7;
const reallySendToot= false; // const selectionOverrideOfSectionTable = '';
// const selectionOverrideOfLineTable = '';
const reallySendToot = false;
function getRandomWikiOSMPage() { function getRandomWikiOSMPage() {
@ -70,9 +73,9 @@ export default function getElementCartographique() {
let keys = Object.keys(listOfTableRows); let keys = Object.keys(listOfTableRows);
let randNumber = 0; let randNumber = 0;
if(selectionOverrideOfSectionTable){ if (selectionOverrideOfSectionTable) {
randNumber = selectionOverrideOfSectionTable randNumber = selectionOverrideOfSectionTable
}else{ } else {
randNumber = randomIntFromInterval(0, keys.length) randNumber = randomIntFromInterval(0, keys.length)
} }
@ -83,9 +86,9 @@ export default function getElementCartographique() {
// on regarde les lignes du tableau // on regarde les lignes du tableau
keys = Object.keys(foundLine); keys = Object.keys(foundLine);
if(selectionOverrideOfLineTable){ if (selectionOverrideOfLineTable) {
randNumber = selectionOverrideOfLineTable randNumber = selectionOverrideOfLineTable
}else{ } else {
randNumber = randomIntFromInterval(0, keys.length) randNumber = randomIntFromInterval(0, keys.length)
} }
@ -94,8 +97,9 @@ export default function getElementCartographique() {
let cells = $(foundLine[randNumber]).find('td') let cells = $(foundLine[randNumber]).find('td')
keys = Object.keys(cells); keys = Object.keys(cells);
console.log("cellules trouvées", keys.length) // console.log("cellules trouvées", keys.length)
console.log("\n")
let result = { let result = {
key: '', key: '',
value: '', value: '',
@ -116,21 +120,11 @@ export default function getElementCartographique() {
}) })
if (!result.key) { if (!result.key) {
console.log("ERROR no key found", $(cells).html()) console.error("ERROR no key found", $(cells).html())
throw Error('no key BOOOOOOOOOOH'); console.error('no key BOOOOOOOOOOH');
getElementCartographique();
} }
// 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)
// aller chercher le lien // aller chercher le lien
// si la clé est une description avec des espaces, faire un lien vers la Key // si la clé est une description avec des espaces, faire un lien vers la Key
let pageLink = `FR:Tag:${result.key}=${result.value}` let pageLink = `FR:Tag:${result.key}=${result.value}`
@ -153,6 +147,8 @@ export default function getElementCartographique() {
const filteredHtml = $('.mw-parser-output', html).find('p') const filteredHtml = $('.mw-parser-output', html).find('p')
console.log("filteredHtml", filteredHtml.length) console.log("filteredHtml", filteredHtml.length)
result.long_desc = $(filteredHtml).text().substring(0,250)
console.log("filteredHtml", result.long_desc)
// if (filteredHtml[0] && filteredHtml[0].text()) { // if (filteredHtml[0] && filteredHtml[0].text()) {
// result.long_desc = filteredHtml[0].text().substring(0,200); // result.long_desc = filteredHtml[0].text().substring(0,200);
@ -163,15 +159,22 @@ export default function getElementCartographique() {
// console.log("result.long_desc", result.long_desc) // console.log("result.long_desc", result.long_desc)
// image de description: // image de description:
// let imgSelector = ".description a.image img" // let imgSelector = ".description a.image img"
console.log("✅ cette page existe bien en Français sur le wiki OSM")
sendMessageWikiTagOfTheDay(makePostMessageFromObj(result)) sendMessageWikiTagOfTheDay(makePostMessageFromObj(result))
}, (err)=>{ }, (err) => {
if(err.statusCode == 404){
console.log("\n Cette page n'existe pas encore, c'est le moment de la créer.", `https://wiki.openstreetmap.org/w/index.php?title=FR:${result.key}=${result.value}` ) //FR:Tag:boundary%3Dborder_zone&action=edit)
console.log("Essayez de voir si la version en Anglais existe.")
console.log("-> ", `https://wiki.openstreetmap.org/wiki/Tag:${result.key}=${result.value}`)
}
console.error(err.statusCode) console.error(err.statusCode)
if (err.statusCode == 404) {
console.log("\n Cette page n'existe pas encore, c'est le moment de la créer.", `https://wiki.openstreetmap.org/w/index.php?title=FR:${result.key}=${result.value}`) //FR:Tag:boundary%3Dborder_zone&action=edit)
console.log("Essayez de voir si la version en Anglais existe.")
let englishPage = `https://wiki.openstreetmap.org/wiki/Tag:${result.key}=${result.value}`
let englishKey = `https://wiki.openstreetmap.org/wiki/Key:${result.key}`
console.log("-> ", englishPage)
checkExistenceOfWebPage(englishPage).catch(err=> {
checkExistenceOfWebPage( englishKey)
});
}
}) })
} else { } else {
console.log("result", result) console.log("result", result)
@ -184,10 +187,24 @@ export default function getElementCartographique() {
} }
/**
* check if a page anwsers to a GET request
* @param url
* @returns {*}
*/
function checkExistenceOfWebPage(url) {
return rp(url).then((html) => {
console.log("oui cette page existe!", url)
}, (err) => {
console.log("hé non. WTF ?")
})
}
function makePostMessageFromObj(result) { function makePostMessageFromObj(result) {
return ` return `
# [Le tag OSM du jour : ${result.key}=${result.value}](${result.link}) # [Le tag OSM du jour : ${result.key}=${result.value}](${result.link})
> ${result.description.trim()} ${result.description.trim()}
> ${result.long_desc.trim()}
#osm #openstreetmap #wiki #rtfw #osm #openstreetmap #wiki #rtfw
` `
} }
@ -236,17 +253,15 @@ function getQuery() {
// run // run
const res = getElementCartographique() const res = getElementCartographique()
console.log("res", res) // console.log("res", res)
function sendMessageWikiTagOfTheDay(message) {
function sendMessageWikiTagOfTheDay(message){
let configPost = { let configPost = {
author: 'curator', author: 'curator',
message, message,
// reallySendPost: true, // reallySendPost: true,
reallySendPost: reallySendToot, reallySendPost: reallySendToot,
} }
console.log("configPost.message", configPost.message)
sendPostMastodon(configPost) sendPostMastodon(configPost)
} }