From e936dcc5289e477fefc72dbbef3d940ace155d24 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Mon, 8 Aug 2022 22:58:33 +0200 Subject: [PATCH] script for meme bliss --- helpers/meme_poster_random.js | 54 ++++++++++++++++++++++++++++------- helpers/utils.js | 2 +- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/helpers/meme_poster_random.js b/helpers/meme_poster_random.js index 10b3ade..b2b8c9d 100644 --- a/helpers/meme_poster_random.js +++ b/helpers/meme_poster_random.js @@ -1,36 +1,70 @@ import fs from "fs"; import path from 'path'; -import {getRandomElementOfArray, listFilesOfFolder, sendPostMastodon} from './utils.js' +import {getRandomElementOfArray, listFilesOfFolder} from './utils.js' +import Masto from "mastodon"; const __dirname = path.resolve(); -const list_unpublished_images = listFilesOfFolder('assets/pictures/meme/not_published') +const folderMemeUnpublished = 'assets/pictures/meme/not_published' +const list_unpublished_images = listFilesOfFolder(folderMemeUnpublished) const reallySendPost = false; + // const reallySendPost = true; function postLink() { - let postObject = getRandomLink() console.log("envoi de post par le compte meme bliss") let selectedImage = 'meme_default.jpg'; - if(list_unpublished_images.length){ + if (list_unpublished_images.length) { selectedImage = getRandomElementOfArray(list_unpublished_images) + }else{ + console.log("----- nothing to publish -----") + return; } + console.log("selectedImage", selectedImage) + let visibility = 'public'; + let language = 'fr'; + let sensitive = false; let configPost = { author: 'meme', image: selectedImage, - message: ` -#meme`, + visibility, + language, + sensitive, + status: `#meme`, reallySendPost } - console.log("configPost.message", configPost.message) - sendPostMastodon(configPost).finally(data => { - console.log("post has been sent, time to move image from unpublished folder: " ,selectedImage ) - }) + console.log("configPost", configPost) + let id = ''; + if (reallySendPost) { + + + let accessToken = process.env['TOKEN_' + configPost.author.toUpperCase()] + const masto = new Masto({ + access_token: accessToken, + api_url: process.env.INSTANCE_MASTODON + '/api/v1/', + }); + + masto.post('media', {file: fs.createReadStream( folderMemeUnpublished + configPost.image)}) + .then(resp => { + id = resp.data.id; + configPost.media_ids = [id] + masto.post('statuses', configPost).then(rep => { + // console.log('rep', rep) + console.log(`posté avec une nouvelle image, ${configPost.image} WOOT`) + console.log("post has been sent, time to move image from unpublished folder: ", selectedImage) + }, err => { + console.error(err) + console.log("erreur T_T") + }) + }) + } else { + console.log("send post disabled for meme bliss") + } } postLink(); \ No newline at end of file diff --git a/helpers/utils.js b/helpers/utils.js index 0d5fea8..fd26064 100644 --- a/helpers/utils.js +++ b/helpers/utils.js @@ -144,7 +144,7 @@ export function sendPostMastodon(config) { * lister les noms de fichier que l'on peut publier dans un dossier. * retourne un tableau */ -export function listFilesOfFolder(foldername){ +export function listFilesOfFolder(folderPath){ let filesNames = [] fs.readdirSync(folderPath).map(fileName => { return filesNames.push(fileName);