diff --git a/helpers/utils.js b/helpers/utils.js index 7b2be25..cc65a9c 100644 --- a/helpers/utils.js +++ b/helpers/utils.js @@ -1,4 +1,7 @@ import Masto from "mastodon"; +import dotenv from "dotenv"; +let local_node_env_conf = dotenv.config() +// console.log("conf", local_node_env_conf) export function randomIntFromInterval(min, max) { // min and max included return Math.floor(Math.random() * (max - min + 1) + min) @@ -31,6 +34,9 @@ export function sendPostMastodon(config) { } // require('dotenv').config(); + if(! config.reallySendPost){ + console.log(" =========== le message ne sera PAS réellement posté sur le compte @" + config.author + "@"+ process.env.INSTANCE_MASTODON +" =========== ")} + console.log(" ") if (process.env['TOKEN_' + config.author.toUpperCase()]) { let visibility = 'public'; diff --git a/helpers/wiki_osm.mjs b/helpers/wiki_osm.mjs index 642e5b2..2573852 100644 --- a/helpers/wiki_osm.mjs +++ b/helpers/wiki_osm.mjs @@ -4,11 +4,14 @@ import rp from "request-promise"; import $ from "cheerio"; import fs from "fs"; import path from 'path'; -import {getRandomElementOfArray, randomIntFromInterval} from "./utils.js"; +import {getRandomElementOfArray, randomIntFromInterval, sendPostMastodon} from "./utils.js"; const __dirname = path.resolve(); -const selectionOverrideOfLineTable = 12; +const selectionOverrideOfSectionTable = 2; +const selectionOverrideOfLineTable = 7; + +const reallySendToot= false; function getRandomWikiOSMPage() { @@ -66,9 +69,11 @@ export default function getElementCartographique() { const listOfTableRows = $('.wikitable', html); let keys = Object.keys(listOfTableRows); - let randNumber = randomIntFromInterval(1, keys.length) - if(selectionOverrideOfLineTable){ - randNumber = selectionOverrideOfLineTable + let randNumber = 0; + if(selectionOverrideOfSectionTable){ + randNumber = selectionOverrideOfSectionTable + }else{ + randNumber = randomIntFromInterval(0, keys.length) } console.log("rand", randNumber) @@ -78,7 +83,11 @@ export default function getElementCartographique() { // on regarde les lignes du tableau keys = Object.keys(foundLine); - randNumber = randomIntFromInterval(0, keys.length) + if(selectionOverrideOfLineTable){ + randNumber = selectionOverrideOfLineTable + }else{ + randNumber = randomIntFromInterval(0, keys.length) + } console.log("lignes de tableau", keys.length) console.log("ligne sélectionnée", randNumber) @@ -90,10 +99,11 @@ export default function getElementCartographique() { let result = { key: '', value: '', - description: '' + description: '', + long_desc: '', } cells.each((i, element) => { - console.log("cell element", i, $(element).text().trim()) + // console.log("cell element", i, $(element).text().trim()) if (i === 0) { result.key = $(element).text().trim(); } @@ -103,8 +113,6 @@ export default function getElementCartographique() { if (i === 3) { result.description = $(element).text().replace(' ', '').replace(' ', '').trim(); } - // let cell = $(element).find("td"); - // console.log($(cell[0])); }) if (!result.key) { @@ -144,27 +152,29 @@ export default function getElementCartographique() { const descriptionStrophe = ''; const filteredHtml = $('.mw-parser-output', html).find('p') - // console.log("filteredHtml", filteredHtml) + console.log("filteredHtml", filteredHtml.length) - if (filteredHtml[0] && filteredHtml[0].text()) { - result.long_desc = filteredHtml[0].text().substring(0,200); - } else if (filteredHtml[1] && filteredHtml[1].text()) { - result.long_desc = filteredHtml[1].text().substring(0,200); - - } + // if (filteredHtml[0] && filteredHtml[0].text()) { + // result.long_desc = filteredHtml[0].text().substring(0,200); + // } else if (filteredHtml[1] && filteredHtml[1].text()) { + // result.long_desc = filteredHtml[1].text().substring(0,200); + // } // console.log("result", result) - console.log("result.long_desc", result.long_desc) + // console.log("result.long_desc", result.long_desc) // image de description: - let imgSelector = ".description a.image img" - const message = makePostMessageFromObj(result) - console.log("message", message) + // let imgSelector = ".description a.image img" + sendMessageWikiTagOfTheDay(makePostMessageFromObj(result)) + }, (err)=>{ - console.error(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) }) } else { console.log("result", result) - const message = makePostMessageFromObj(result) - console.log("message", message) } @@ -226,4 +236,17 @@ function getQuery() { // run const res = getElementCartographique() -console.log("res", res) \ No newline at end of file +console.log("res", res) + + + +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