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

88 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-08-08 22:49:12 +02:00
import fs from "fs";
import path from 'path';
import {getRandomElementOfArray, listFilesOfFolder} from '../libs/utils.mjs'
2022-08-08 22:58:33 +02:00
import Masto from "mastodon";
2022-08-08 22:49:12 +02:00
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)
2022-08-08 23:05:23 +02:00
const folderMemeUnpublished = 'assets/pictures/meme/not_published/'
const folderMemePublished = 'assets/pictures/meme/published/'
2022-08-08 22:58:33 +02:00
const list_unpublished_images = listFilesOfFolder(folderMemeUnpublished)
2022-08-08 22:49:12 +02:00
const reallySendPost = false;
// const reallySendPost = true;
2022-08-08 22:49:12 +02:00
function postLink() {
console.log("envoi de post par le compte curator")
2022-08-08 22:49:12 +02:00
let selectedImage = 'meme_default.jpg';
2022-08-08 22:58:33 +02:00
if (list_unpublished_images.length) {
2022-08-08 22:49:12 +02:00
selectedImage = getRandomElementOfArray(list_unpublished_images)
2022-08-08 22:58:33 +02:00
}else{
console.log("----- nothing to publish -----")
return;
2022-08-08 22:49:12 +02:00
}
2022-08-08 22:58:33 +02:00
console.log("selectedImage", selectedImage)
let visibility = 'public';
let language = 'fr';
let sensitive = false;
2022-08-08 22:49:12 +02:00
let configPost = {
author: 'meme',
image: selectedImage,
2022-08-08 22:58:33 +02:00
visibility,
language,
sensitive,
status: `#meme`,
2022-08-08 22:49:12 +02:00
reallySendPost
}
2022-08-08 22:58:33 +02:00
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)
2022-08-08 23:05:23 +02:00
moveImageToPublishedFolder(selectedImage)
2022-08-08 22:58:33 +02:00
}, 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")
2022-08-08 22:58:33 +02:00
}
2022-08-08 22:49:12 +02:00
}
2022-08-08 23:05:23 +02:00
/**
* move to published
* @param imageName
*/
function moveImageToPublishedFolder(imageName){
return fs.renameSync(folderMemeUnpublished + imageName, folderMemePublished + imageName)
}
2022-08-08 22:49:12 +02:00
postLink();