From 15bb11bf1ec659ad78898160c1e64b92ad196677 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Mon, 8 Aug 2022 15:47:09 +0200 Subject: [PATCH] check existence of wiki osm page --- helpers/describe_picture_folders.mjs | 15 ++++-- helpers/wiki_osm.mjs | 79 +++++++++++++++++----------- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/helpers/describe_picture_folders.mjs b/helpers/describe_picture_folders.mjs index 887f878..cba40b9 100644 --- a/helpers/describe_picture_folders.mjs +++ b/helpers/describe_picture_folders.mjs @@ -1,12 +1,21 @@ +/** + * lieu où 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 { folders : [ { name: 'default', - descriptions: '' + descriptions: '', + author: 'curator' }, { - name: 'default', - descriptions: '' + name: 'tykayn-sketches', + descriptions: 'Sketch' + }, + { + name: 'memes', + descriptions: 'meme' }, ] } \ No newline at end of file diff --git a/helpers/wiki_osm.mjs b/helpers/wiki_osm.mjs index 2573852..93b5477 100644 --- a/helpers/wiki_osm.mjs +++ b/helpers/wiki_osm.mjs @@ -4,14 +4,17 @@ import rp from "request-promise"; import $ from "cheerio"; import fs from "fs"; import path from 'path'; -import {getRandomElementOfArray, randomIntFromInterval, sendPostMastodon} from "./utils.js"; +import {randomIntFromInterval, sendPostMastodon} from "./utils.js"; const __dirname = path.resolve(); const selectionOverrideOfSectionTable = 2; const selectionOverrideOfLineTable = 7; -const reallySendToot= false; +// const selectionOverrideOfSectionTable = ''; +// const selectionOverrideOfLineTable = ''; + +const reallySendToot = false; function getRandomWikiOSMPage() { @@ -70,9 +73,9 @@ export default function getElementCartographique() { let keys = Object.keys(listOfTableRows); let randNumber = 0; - if(selectionOverrideOfSectionTable){ + if (selectionOverrideOfSectionTable) { randNumber = selectionOverrideOfSectionTable - }else{ + } else { randNumber = randomIntFromInterval(0, keys.length) } @@ -83,9 +86,9 @@ export default function getElementCartographique() { // on regarde les lignes du tableau keys = Object.keys(foundLine); - if(selectionOverrideOfLineTable){ + if (selectionOverrideOfLineTable) { randNumber = selectionOverrideOfLineTable - }else{ + } else { randNumber = randomIntFromInterval(0, keys.length) } @@ -94,8 +97,9 @@ export default function getElementCartographique() { let cells = $(foundLine[randNumber]).find('td') keys = Object.keys(cells); - console.log("cellules trouvées", keys.length) + // console.log("cellules trouvées", keys.length) + console.log("\n") let result = { key: '', value: '', @@ -116,21 +120,11 @@ export default function getElementCartographique() { }) if (!result.key) { - console.log("ERROR no key found", $(cells).html()) - throw Error('no key BOOOOOOOOOOH'); + console.error("ERROR no key found", $(cells).html()) + 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 // si la clé est une description avec des espaces, faire un lien vers la Key let pageLink = `FR:Tag:${result.key}=${result.value}` @@ -153,6 +147,8 @@ export default function getElementCartographique() { const filteredHtml = $('.mw-parser-output', html).find('p') 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()) { // 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) // image de description: // let imgSelector = ".description a.image img" + console.log("✅ cette page existe bien en Français sur le wiki OSM") sendMessageWikiTagOfTheDay(makePostMessageFromObj(result)) - }, (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}`) - } + }, (err) => { 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 { 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) { return ` # [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 ` } @@ -236,17 +253,15 @@ function getQuery() { // run const res = getElementCartographique() -console.log("res", res) +// console.log("res", res) - -function sendMessageWikiTagOfTheDay(message){ +function sendMessageWikiTagOfTheDay(message) { let configPost = { author: 'curator', message, // reallySendPost: true, reallySendPost: reallySendToot, } - console.log("configPost.message", configPost.message) sendPostMastodon(configPost) } \ No newline at end of file