multi-account-post-schedule.../helpers/utils.js

143 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-08-04 23:07:59 +02:00
import Masto from "mastodon";
2022-08-08 15:25:09 +02:00
import dotenv from "dotenv";
2022-08-08 16:17:04 +02:00
import fs from "node-fs";
2022-08-08 15:25:09 +02:00
let local_node_env_conf = dotenv.config()
2022-08-08 16:17:04 +02:00
2022-08-08 15:25:09 +02:00
// console.log("conf", local_node_env_conf)
2022-08-04 23:07:59 +02:00
export function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
2022-08-08 16:17:04 +02:00
2022-08-04 23:07:59 +02:00
export function getRandomElementOfArray(listItems) {
return listItems[Math.floor(Math.random() * listItems.length)]
2022-08-05 14:57:53 +02:00
}
let nowDate = new Date()
export let defaultConfigMasto = {
author: 'curator',
visibility: 'public',
language: 'fr',
sensitive: false,
reallySendPost: false,
2022-08-08 16:17:04 +02:00
image: '',
2022-08-05 14:57:53 +02:00
message: "Hey coucou! on est le" + nowDate,
scheduled_at: ""
}
2022-08-08 16:17:04 +02:00
export function tokenForAuthorIsPresentInDotEnv(author) {
console.log(" process.env['TOKEN_' + author.toUpperCase()]", process.env['TOKEN_' + author.toUpperCase()])
return process.env['TOKEN_' + author.toUpperCase()];
}
2022-08-05 14:57:53 +02:00
export function sendPostMastodon(config) {
// override defaults with input argument
config = {
...defaultConfigMasto,
...config,
}
2022-08-08 16:30:55 +02:00
console.log("sendPostMastodon config", config)
2022-08-05 14:57:53 +02:00
2022-08-08 16:17:04 +02:00
if (!config.reallySendPost) {
console.log(" =========== le message ne sera PAS réellement posté sur le compte @" + config.author + "@" + process.env.INSTANCE_MASTODON + " =========== ")
} else {
console.log(" ")
if (process.env.INSTANCE_MASTODON && tokenForAuthorIsPresentInDotEnv(config.author)) {
let visibility = 'public';
let language = 'fr';
let sensitive = false;
let accessToken = process.env['TOKEN_' + config.author.toUpperCase()]
const masto = new Masto({
access_token: accessToken,
api_url: process.env.INSTANCE_MASTODON + '/api/v1/',
});
2022-08-05 14:57:53 +02:00
2022-08-08 16:17:04 +02:00
let params = {
status: config.message,
visibility,
language,
sensitive
}
if (config.cw) {
params['spoiler_text'] = config.cw
}
if (config.scheduled_at && config.scheduled_at_bool) {
let dateschedule = new Date(config.scheduled_at)
params['scheduled_at'] = dateschedule.toISOString()
}
console.log(config)
/**
* envoi sans fichier joint
*/
if (!config.image) {
if (config.reallySendPost) {
masto.post('statuses', params).then(rep => {
// console.log('rep', rep)
console.log("posté, yay!")
}, err => {
console.error(err)
})
}
2022-08-05 14:57:53 +02:00
}
2022-08-08 16:17:04 +02:00
/**
* envoi avec fichier,
* on doit d'abord faire un upload du fichier,
* puis relier son id de media au nouveau post.
*/
if (config.image) {
var id;
console.log("envoi du média", config.image)
2022-08-08 16:30:55 +02:00
// if(config.image === 'osm_default.jpg'){
// id = '108787661095227871';
// params.media_ids = [id]
// masto.post('statuses', params).then(rep => {
// console.log("posté avec l'image osm_default déjà enregistrée")
// // console.log('rep', rep)
// }, err => {
//
// console.error(err)
// console.log("erreur T_T")
// })
//
// }else{
2022-08-08 16:17:04 +02:00
// upload new media
return masto.post('media', {file: fs.createReadStream('assets/' + config.image)})
.then(resp => {
id = resp.data.id;
params.media_ids = [id]
masto.post('statuses', params).then(rep => {
2022-08-08 18:43:26 +02:00
// console.log('rep', rep)
2022-08-08 16:17:04 +02:00
console.log("posté avec une nouvelle image, WOOT")
}, err => {
console.error(err)
console.log("erreur T_T")
})
})
}
2022-08-08 16:30:55 +02:00
// }
2022-08-08 16:17:04 +02:00
} else {
console.error(`pas de token pour l'auteur "${config.author}" ou pas d'instance mastodon définie`)
}
2022-08-05 14:57:53 +02:00
}
}