import fs from "fs"; import path from 'path'; import {getRandomElementOfArray, listFilesOfFolder} from './libs/utils.mjs' import Masto from "mastodon"; const __dirname = path.resolve(); const folderMemeUnpublished = 'assets/pictures/meme/not_published/' const folderMemePublished = 'assets/pictures/meme/published/' const list_unpublished_images = listFilesOfFolder(folderMemeUnpublished) let reallySendPost = false; reallySendPost = true; function postLink() { console.log("envoi de post par le compte meme bliss") let selectedImage = 'meme_default.jpg'; 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, visibility, language, sensitive, status: `#meme`, reallySendPost } 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) moveImageToPublishedFolder(selectedImage) }, err => { console.error(err) console.log("erreur T_T") }) }) } else { console.log("send post disabled for meme bliss") console.log("image", configPost.image) console.log("image NON envoyée") } } /** * move to published * @param imageName */ function moveImageToPublishedFolder(imageName){ return fs.renameSync(folderMemeUnpublished + imageName, folderMemePublished + imageName) } postLink();