fix missing dotenv load

This commit is contained in:
Tykayn 2022-08-08 15:25:09 +02:00 committed by tykayn
parent 39694e428f
commit 4933e2c904
2 changed files with 54 additions and 25 deletions

View File

@ -1,4 +1,7 @@
import Masto from "mastodon"; 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 export function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min) return Math.floor(Math.random() * (max - min + 1) + min)
@ -31,6 +34,9 @@ export function sendPostMastodon(config) {
} }
// require('dotenv').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()]) { if (process.env['TOKEN_' + config.author.toUpperCase()]) {
let visibility = 'public'; let visibility = 'public';

View File

@ -4,11 +4,14 @@ 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} from "./utils.js"; import {getRandomElementOfArray, randomIntFromInterval, sendPostMastodon} from "./utils.js";
const __dirname = path.resolve(); const __dirname = path.resolve();
const selectionOverrideOfLineTable = 12; const selectionOverrideOfSectionTable = 2;
const selectionOverrideOfLineTable = 7;
const reallySendToot= false;
function getRandomWikiOSMPage() { function getRandomWikiOSMPage() {
@ -66,9 +69,11 @@ export default function getElementCartographique() {
const listOfTableRows = $('.wikitable', html); const listOfTableRows = $('.wikitable', html);
let keys = Object.keys(listOfTableRows); let keys = Object.keys(listOfTableRows);
let randNumber = randomIntFromInterval(1, keys.length) let randNumber = 0;
if(selectionOverrideOfLineTable){ if(selectionOverrideOfSectionTable){
randNumber = selectionOverrideOfLineTable randNumber = selectionOverrideOfSectionTable
}else{
randNumber = randomIntFromInterval(0, keys.length)
} }
console.log("rand", randNumber) console.log("rand", randNumber)
@ -78,7 +83,11 @@ export default function getElementCartographique() {
// on regarde les lignes du tableau // on regarde les lignes du tableau
keys = Object.keys(foundLine); 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("lignes de tableau", keys.length)
console.log("ligne sélectionnée", randNumber) console.log("ligne sélectionnée", randNumber)
@ -90,10 +99,11 @@ export default function getElementCartographique() {
let result = { let result = {
key: '', key: '',
value: '', value: '',
description: '' description: '',
long_desc: '',
} }
cells.each((i, element) => { cells.each((i, element) => {
console.log("cell element", i, $(element).text().trim()) // console.log("cell element", i, $(element).text().trim())
if (i === 0) { if (i === 0) {
result.key = $(element).text().trim(); result.key = $(element).text().trim();
} }
@ -103,8 +113,6 @@ export default function getElementCartographique() {
if (i === 3) { if (i === 3) {
result.description = $(element).text().replace(' ', '').replace(' ', '').trim(); result.description = $(element).text().replace(' ', '').replace(' ', '').trim();
} }
// let cell = $(element).find("td");
// console.log($(cell[0]));
}) })
if (!result.key) { if (!result.key) {
@ -144,27 +152,29 @@ export default function getElementCartographique() {
const descriptionStrophe = ''; const descriptionStrophe = '';
const filteredHtml = $('.mw-parser-output', html).find('p') const filteredHtml = $('.mw-parser-output', html).find('p')
// console.log("filteredHtml", filteredHtml) console.log("filteredHtml", filteredHtml.length)
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);
} else if (filteredHtml[1] && filteredHtml[1].text()) { // } else if (filteredHtml[1] && filteredHtml[1].text()) {
result.long_desc = filteredHtml[1].text().substring(0,200); // result.long_desc = filteredHtml[1].text().substring(0,200);
// }
}
// console.log("result", result) // console.log("result", result)
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"
const message = makePostMessageFromObj(result) sendMessageWikiTagOfTheDay(makePostMessageFromObj(result))
console.log("message", message)
}, (err)=>{ }, (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 { } else {
console.log("result", result) console.log("result", result)
const message = makePostMessageFromObj(result)
console.log("message", message)
} }
@ -226,4 +236,17 @@ function getQuery() {
// run // run
const res = getElementCartographique() const res = getElementCartographique()
console.log("res", res) console.log("res", res)
function sendMessageWikiTagOfTheDay(message){
let configPost = {
author: 'curator',
message,
// reallySendPost: true,
reallySendPost: reallySendToot,
}
console.log("configPost.message", configPost.message)
sendPostMastodon(configPost)
}