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-12-07 23:15:07 +01:00
|
|
|
import sharp from 'sharp';
|
|
|
|
import fs from 'fs';
|
|
|
|
import https from 'https';
|
2022-08-08 16:17:04 +02:00
|
|
|
|
|
|
|
|
2022-12-07 23:15:07 +01:00
|
|
|
let local_node_env_conf = dotenv.config()
|
2022-08-04 23:07:59 +02:00
|
|
|
|
|
|
|
export function randomIntFromInterval(min, max) { // min and max included
|
2022-11-22 10:55:11 +01:00
|
|
|
return Math.floor(Math.random() * (max - min + 1) + min) | 1
|
2022-08-04 23:07:59 +02:00
|
|
|
}
|
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-12-07 23:26:55 +01:00
|
|
|
folder_image: 'assets/',
|
2022-08-05 14:57:53 +02:00
|
|
|
message: "Hey coucou! on est le" + nowDate,
|
2022-11-22 12:47:07 +01:00
|
|
|
scheduled_at: "",
|
|
|
|
content_type: "text/markdown"
|
2022-08-05 14:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-08 16:17:04 +02:00
|
|
|
export function tokenForAuthorIsPresentInDotEnv(author) {
|
2022-12-07 23:15:07 +01:00
|
|
|
console.log(" process.env['TOKEN_' + author.toUpperCase()]", process.env['TOKEN_' + author.toUpperCase()])
|
2022-08-08 16:17:04 +02:00
|
|
|
return process.env['TOKEN_' + author.toUpperCase()];
|
|
|
|
}
|
|
|
|
|
2022-12-07 23:15:07 +01:00
|
|
|
export default function sendPostMastodon(config) {
|
2022-08-05 14:57:53 +02:00
|
|
|
|
|
|
|
// override defaults with input argument
|
|
|
|
config = {
|
|
|
|
...defaultConfigMasto,
|
|
|
|
...config,
|
|
|
|
}
|
|
|
|
|
2022-12-07 23:15:07 +01: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* envoi sans fichier joint
|
|
|
|
*/
|
|
|
|
if (!config.image) {
|
|
|
|
|
|
|
|
if (config.reallySendPost) {
|
|
|
|
|
|
|
|
masto.post('statuses', params).then(rep => {
|
2022-12-07 23:15:07 +01:00
|
|
|
|
2022-08-08 16:17:04 +02:00
|
|
|
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-12-07 23:15:07 +01:00
|
|
|
// upload new media
|
2022-12-07 23:26:55 +01:00
|
|
|
return masto.post('media', {file: fs.createReadStream(config.folder_image + config.image)})
|
2022-12-07 23:15:07 +01:00
|
|
|
.then(resp => {
|
|
|
|
id = resp.data.id;
|
|
|
|
params.media_ids = [id]
|
|
|
|
masto.post('statuses', params).then(rep => {
|
|
|
|
// console.log('rep', rep)
|
|
|
|
console.log("posté avec une nouvelle image, WOOT")
|
|
|
|
}, err => {
|
|
|
|
console.error(err)
|
|
|
|
|
|
|
|
console.log("erreur T_T")
|
2022-08-08 16:17:04 +02:00
|
|
|
})
|
2022-12-07 23:15:07 +01:00
|
|
|
})
|
|
|
|
}
|
2022-08-08 16:17:04 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:49:12 +02:00
|
|
|
/**
|
|
|
|
* @name listFilesOfFolder
|
|
|
|
* lister les noms de fichier que l'on peut publier dans un dossier.
|
|
|
|
* retourne un tableau
|
|
|
|
*/
|
2022-12-07 23:15:07 +01:00
|
|
|
export function listFilesOfFolder(folderPath) {
|
2022-08-08 22:49:12 +02:00
|
|
|
let filesNames = []
|
|
|
|
fs.readdirSync(folderPath).map(fileName => {
|
|
|
|
return filesNames.push(fileName);
|
|
|
|
});
|
|
|
|
|
|
|
|
return filesNames;
|
|
|
|
}
|
2022-08-05 14:57:53 +02:00
|
|
|
|
2022-08-08 22:49:12 +02:00
|
|
|
/**
|
|
|
|
* @name initializeFolderForPictures
|
|
|
|
* crée un dossier d'assets, avec ses sous dossiers not_published et published si ils manquent.
|
|
|
|
* une fois que l'on prendra une image dans le dossier non publié, on la déplacera dans le dossier des images publées.
|
|
|
|
*/
|
2022-12-07 23:15:07 +01:00
|
|
|
export function initializeFolderForPictures(folderName) {
|
2022-08-08 22:49:12 +02:00
|
|
|
try {
|
|
|
|
if (!fs.existsSync(folderName)) {
|
|
|
|
fs.mkdirSync(folderName);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2022-11-22 10:55:11 +01:00
|
|
|
}
|
|
|
|
|
2022-12-07 23:15:07 +01:00
|
|
|
/**
|
|
|
|
* find first image in blog post and return the src value of the img tag
|
|
|
|
* @param htmlContent
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export function findFirstImageInContent(htmlContent) {
|
|
|
|
let result = ''
|
|
|
|
let first = htmlContent.match(/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/)[0];
|
|
|
|
if (first) {
|
|
|
|
result = first.match(/src\=\"(.*)\"/i)
|
|
|
|
if(result.length && result[0]){
|
|
|
|
result = result[0].split('"')
|
|
|
|
result = result[1]
|
|
|
|
}
|
2022-11-22 11:27:54 +01:00
|
|
|
}
|
2022-12-07 23:15:07 +01:00
|
|
|
console.log(result)
|
|
|
|
result = result.replace('http:', 'https:')
|
|
|
|
result = result.replace('https://www.ailesse.info/~tykayn/bazar/kotlife', 'https://www.tykayn.fr/wp-content/uploads/i/kotlife')
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* usage:
|
|
|
|
* downloadImage('https://upload.wikimedia.org/wikipedia/en/thumb/7/7d/Lenna_%28test_image%29.png/440px-Lenna_%28test_image%29.png', 'lena.png')
|
|
|
|
* .then(console.log)
|
|
|
|
* .catch(console.error);
|
|
|
|
* @param url
|
|
|
|
* @param filepath
|
|
|
|
* @returns {Promise<unknown>}
|
|
|
|
*/
|
|
|
|
export function downloadImage(url, filepath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
https.get(url, (res) => {
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
res.pipe(fs.createWriteStream(filepath))
|
|
|
|
.on('error', reject)
|
|
|
|
.once('close', () => resolve(filepath));
|
|
|
|
} else {
|
|
|
|
// Consume response data to free up memory
|
|
|
|
res.resume();
|
|
|
|
reject(new Error(`Request Failed With a Status Code: ${res.statusCode}; \n ${res.statusMessage} `));
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* faire un
|
|
|
|
* @param pictureName
|
|
|
|
* @param width
|
|
|
|
* @param height
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
export function CropPictue(pictureName, width = 500, height = 300) {
|
|
|
|
|
|
|
|
return sharp(pictureName)
|
|
|
|
|
|
|
|
.extract({left: 0, top: 0, width, height})
|
|
|
|
.toFile('thumb_' + pictureName, function (err) {
|
|
|
|
if (err) console.log(err);
|
|
|
|
});
|
2022-08-08 22:49:12 +02:00
|
|
|
}
|