multi-account-post-schedule.../helpers/tkblogPost.js
2022-08-08 22:33:06 +02:00

50 lines
1.4 KiB
JavaScript

import fs from "fs";
import path from 'path';
import {sendPostMastodon} from './utils.js'
const __dirname = path.resolve();
const tkpostsjson = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/tykayn_wptkblog_posts.json", 'utf-8'))
// const reallySendPost = false;
const reallySendPost = true;
export function getRandomElementOfArray(listItems) {
return listItems[Math.floor(Math.random() * listItems.length)]
}
function getRandomLink() {
let filteredLinks = tkpostsjson.filter(elem => elem.post_status === 'publish')
return getRandomElementOfArray(filteredLinks)
}
function postLink() {
let postObject = getRandomLink()
console.log("envoi de post par le Curator")
let filteredExcerpt = postObject.post_content.replace(/<[^>]+>/g, '')
let counterLength = filteredExcerpt.length;
let limitExcerpt = 250
filteredExcerpt = filteredExcerpt.substring(0, limitExcerpt)
if (filteredExcerpt && counterLength > limitExcerpt) {
filteredExcerpt = ' _ ' + filteredExcerpt + '… _'
}
let configPost = {
author: 'tykayn',
image: 'tk_blog_bannieres_oct2010-1.jpg',
message: `# [${postObject.post_title}](${postObject.guid})
* ${postObject.post_date} - ${postObject.guid}
${filteredExcerpt}
* #tykayn #tkblog #blog`,
reallySendPost
}
console.log("configPost.message", configPost.message)
sendPostMastodon(configPost)
}
postLink();