multi-account-post-schedule.../wip/tykayn_art_random.mjs

86 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();
const folderUnpublishedNSFW = 'assets/pictures/tykayn/nsfw/not_published/'
const folderPublishedNSFW = 'assets/pictures/tykayn/nsfw/published/'
const folderUnpublished = 'assets/pictures/tykayn/sfw/not_published/'
const folderPublished = 'assets/pictures/tykayn/sfw/published/'
const kindOfPic="nsfw" // or sfw
const author="tykayn" // or sfw
const list_unpublished_images = listFilesOfFolder(folderUnpublishedNSFW)
let reallySendPost = false;
// reallySendPost = true;
function postLink() {
console.log("envoi de post par le compte ",author)
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 = !!(kindOfPic == 'nsfw') | false;
let configPost = {
author,
image: selectedImage,
visibility,
language,
sensitive,
status: `#${kindOfPic} Illustration par @${author}`,
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( folderUnpublished + 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 in this script for author ", author)
console.log("image", configPost.image)
console.log("image NON envoyée")
}
}
/**
* move to published
* @param imageName
*/
function moveImageToPublishedFolder(imageName){
return fs.renameSync(folderUnpublished + imageName, folderPublished + imageName)
}
postLink();