From 70e4dce6ea03fc90cff927ac1dfcf7f4aa970a4f Mon Sep 17 00:00:00 2001 From: Tykayn Date: Fri, 5 Aug 2022 14:57:53 +0200 Subject: [PATCH] post function in utils --- helpers/tkblogPost.js | 91 ++----------------------------------------- helpers/utils.js | 91 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 89 deletions(-) diff --git a/helpers/tkblogPost.js b/helpers/tkblogPost.js index 68b590e..26c7615 100644 --- a/helpers/tkblogPost.js +++ b/helpers/tkblogPost.js @@ -6,95 +6,10 @@ import Masto from "mastodon"; import path from 'path'; const __dirname = path.resolve(); const tkpostsjson = JSON.parse(fs.readFileSync(__dirname +"/assets/documents/tykayn_wptkblog_posts.json", 'utf-8')) +import {sendPostMastodon} from './utils.js' - -let nowDate = new Date() - - -let defaultConfigMasto = { - author: 'curator', - visibility: 'public', - language: 'fr', - sensitive: false, - reallySendPost: false, - message: "Hey coucou! on est le" + nowDate, - scheduled_at: "" -} - -export function sendPostMastodon(config) { - - // override defaults with input argument - config = { - ...defaultConfigMasto, - ...config, - } - // require('dotenv').config(); - - if (process.env['TOKEN_' + config.author.toUpperCase()]) { - - let visibility = 'public'; - let language = 'fr'; - let sensitive = false; - - let accessToken = process.env['TOKEN_' + config.author.toUpperCase()] - const masto = new Masto({ - access_token: accessToken, - api_url: process.env.INSTANCE_MASTODON + '/api/v1/', - }); - - let params = { - status: config.message, - visibility, - language, - sensitive - } - if (config.cw) { - params['spoiler_text'] = config.cw - } - if (config.scheduled_at && config.scheduled_at_bool) { - let dateschedule = new Date(config.scheduled_at) - params['scheduled_at'] = dateschedule.toISOString() - } - console.log(config) - - /** - * envoi sans fichier joint - */ - if (!config.fichier) { - - if (config.reallySendPost) { - - masto.post('statuses', params).then(rep => { - // console.log('rep', rep) - console.log("posté, yay!") - }, err => { - console.error(err) - }) - } - } - - /** - * envoi avec fichier, - * on doit d'abord faire un upload du fichier, - * puis relier son id de media au nouveau post. - */ - // if (config.fichier) { - // - // masto.post('statuses', params).then(rep => { - // console.log('rep', rep) - // }, err => { - // console.error(err) - // }) - // res.render('index', {bodyReq: config}) - // } - - } else { - console.error('pas de token pour ' + config.author, process.env.TOKEN_curator) - } -} - export function getRandomElementOfArray(listItems) { return listItems[Math.floor(Math.random() * listItems.length)] } @@ -126,8 +41,8 @@ function postLink() { * ${postObject.post_date} - ${postObject.guid} ${filteredExcerpt} * #tykayn #tkblog #blog`, - reallySendPost: true, - // reallySendPost: false, + // reallySendPost: true, + reallySendPost: false, } console.log("configPost.message", configPost.message) sendPostMastodon(configPost) diff --git a/helpers/utils.js b/helpers/utils.js index 4875d52..7b2be25 100644 --- a/helpers/utils.js +++ b/helpers/utils.js @@ -5,4 +5,93 @@ export function randomIntFromInterval(min, max) { // min and max included } export function getRandomElementOfArray(listItems) { return listItems[Math.floor(Math.random() * listItems.length)] -} \ No newline at end of file +} + + + +let nowDate = new Date() + +export let defaultConfigMasto = { + author: 'curator', + visibility: 'public', + language: 'fr', + sensitive: false, + reallySendPost: false, + message: "Hey coucou! on est le" + nowDate, + scheduled_at: "" +} + + +export function sendPostMastodon(config) { + + // override defaults with input argument + config = { + ...defaultConfigMasto, + ...config, + } + // require('dotenv').config(); + + if (process.env['TOKEN_' + config.author.toUpperCase()]) { + + let visibility = 'public'; + let language = 'fr'; + let sensitive = false; + + let accessToken = process.env['TOKEN_' + config.author.toUpperCase()] + const masto = new Masto({ + access_token: accessToken, + api_url: process.env.INSTANCE_MASTODON + '/api/v1/', + }); + + let params = { + status: config.message, + visibility, + language, + sensitive + } + if (config.cw) { + params['spoiler_text'] = config.cw + } + if (config.scheduled_at && config.scheduled_at_bool) { + let dateschedule = new Date(config.scheduled_at) + params['scheduled_at'] = dateschedule.toISOString() + } + console.log(config) + + /** + * envoi sans fichier joint + */ + if (!config.fichier) { + + if (config.reallySendPost) { + + masto.post('statuses', params).then(rep => { + // console.log('rep', rep) + console.log("posté, yay!") + }, err => { + console.error(err) + }) + } + } + + /** + * envoi avec fichier, + * on doit d'abord faire un upload du fichier, + * puis relier son id de media au nouveau post. + */ + // if (config.fichier) { + // + // masto.post('statuses', params).then(rep => { + // console.log('rep', rep) + // }, err => { + // console.error(err) + // }) + // res.render('index', {bodyReq: config}) + // } + + } else { + console.error('pas de token pour ' + config.author, process.env.TOKEN_curator) + } +} + +