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

51 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-08-04 23:07:59 +02:00
// import fetch from "node-fetch"
// import rp from "request-promise";
// import $ from "cheerio";
import fs from "fs";
import Masto from "mastodon";
import path from 'path';
const __dirname = path.resolve();
const tkpostsjson = JSON.parse(fs.readFileSync(__dirname +"/assets/documents/tykayn_wptkblog_posts.json", 'utf-8'))
2022-08-05 14:57:53 +02:00
import {sendPostMastodon} from './utils.js'
2022-08-04 23:07:59 +02:00
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){
2022-08-05 09:24:54 +02:00
filteredExcerpt = ' _'+filteredExcerpt+'…_'
2022-08-04 23:07:59 +02:00
}
let configPost = {
author: 'curator',
message: `# [${postObject.post_title}](${postObject.guid})
* ${postObject.post_date} - ${postObject.guid}
${filteredExcerpt}
* #tykayn #tkblog #blog`,
2022-08-05 14:57:53 +02:00
// reallySendPost: true,
reallySendPost: false,
2022-08-04 23:07:59 +02:00
}
2022-08-05 14:35:47 +02:00
console.log("configPost.message", configPost.message)
2022-08-04 23:07:59 +02:00
sendPostMastodon(configPost)
}
postLink();