multi-account-post-schedule.../helpers/postQzine.mjs

49 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-08-08 22:21:27 +02:00
import fs from "fs";
import path from 'path';
2022-11-21 16:15:32 +01:00
import sendPostMastodon from './utils.mjs'
2022-08-08 22:21:27 +02:00
const __dirname = path.resolve();
2022-11-21 16:15:32 +01:00
const list_posts_json = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/qzine_posts_all.json", 'utf-8'))
2022-08-08 22:21:27 +02:00
// const reallySendPost = false;
const reallySendPost = true;
export function getRandomElementOfArray(listItems) {
return listItems[Math.floor(Math.random() * listItems.length)]
}
function getRandomLink() {
2022-11-21 16:15:32 +01:00
let filteredLinks = list_posts_json.filter(elem => elem.status === 'publish')
2022-08-08 22:21:27 +02:00
return getRandomElementOfArray(filteredLinks)
}
function postLink() {
let postObject = getRandomLink()
2022-11-21 16:15:32 +01:00
console.log("envoi de post par le compte Qzine - post n°" ,postObject.id)
2022-08-08 22:21:27 +02:00
2022-11-21 16:15:32 +01:00
let filteredExcerpt = postObject.excerpt.rendered.replace(/<[^>]+>/g, '')
2022-08-08 22:21:27 +02:00
let counterLength = filteredExcerpt.length;
let limitExcerpt = 250
filteredExcerpt = filteredExcerpt.substring(0, limitExcerpt)
if (filteredExcerpt && counterLength > limitExcerpt) {
filteredExcerpt = '> ' + filteredExcerpt + '… '
}
let configPost = {
author: 'qzine',
image: 'qzine_default.jpg',
2022-11-21 16:15:32 +01:00
message: `# [ !Qzine - ${postObject.title.rendered}](${postObject.link})
2022-08-08 22:21:27 +02:00
2022-11-21 16:15:32 +01:00
* ${postObject.date} - ${postObject.link}
2022-08-08 22:21:27 +02:00
${filteredExcerpt}
* #qzine #qzineblog #blog #sexe #relations #éducation #plaisir #sociologie`,
reallySendPost
}
console.log("configPost.message", configPost.message)
sendPostMastodon(configPost)
}
postLink();