multi-account-post-schedule.../helpers/wip/curation_image_random.mjs

88 lines
2.7 KiB
JavaScript

import fs from "fs";
import path from 'path';
import {getRandomElementOfArray, listFilesOfFolder} from '../libs/utils.mjs'
import Masto from "mastodon";
const __dirname = path.resolve();
// choisir un type de publication au hasard dans les dossiers "picture"
import pictureFolderDescriptions from "../libs/describe_picture_folders.mjs"
const typeOfFolder = getRandomElementOfArray(Object.keys(pictureFolderDescriptions))
console.log(typeOfFolder)
const folderMemeUnpublished = 'assets/pictures/meme/not_published/'
const folderMemePublished = 'assets/pictures/meme/published/'
const list_unpublished_images = listFilesOfFolder(folderMemeUnpublished)
const reallySendPost = false;
// const reallySendPost = true;
function postLink() {
console.log("envoi de post par le compte curator")
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();