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

73 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2022-08-08 22:21:27 +02:00
import fs from "fs";
import path from 'path';
import sendPostMastodon, {
randomIntFromInterval,
2022-12-14 16:47:57 +01:00
getRandomElementOfArray,
2022-12-14 17:31:03 +01:00
findPictureAndSendPost, getRandomLinkGeneral
} from './libs/utils.mjs'
2022-12-07 17:39:46 +01:00
const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
2022-08-08 22:21:27 +02:00
/**
*
* Poster une image au hasard, et la déplacer dans le dossier "publiés"
*/
/**
* Récupérer les json avec ces url
* https://qzine.fr/wp-json/wp/v2/posts?per_page=100&page=1
* https://qzine.fr/wp-json/wp/v2/posts?per_page=100&page=2
*/
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
function getRandomLinkQzine() {
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() {
2022-12-14 17:31:03 +01:00
let postObject = {};
let idOfPost = 1091;
2022-12-14 18:18:34 +01:00
idOfPost = null;
2022-12-14 17:31:03 +01:00
if (idOfPost) {
postObject = list_posts_json.filter(elem => elem.id === idOfPost)[0]
console.log(postObject)
} else {
postObject = getRandomLinkGeneral(list_posts_json)
}
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',
website: 'qzine',
slug: postObject.title.rendered,
postObject,
folder_image: folderBlogPostsPreview,
2022-12-07 18:00:08 +01:00
image: 'qzine_post_' + randomIntFromInterval(1 , 4) + '.jpg',
message: `!Qzine - ${postObject.title.rendered}
2022-08-08 22:21:27 +02:00
* ${postObject.link}
2022-08-08 22:21:27 +02:00
${filteredExcerpt}
* #qzine #qzineblog #blog #sexe #relations #éducation #plaisir #sociologie`,
}
// console.log('postContent' , postContent)
findPictureAndSendPost(postObject.content.rendered, configPost)
2022-08-08 22:21:27 +02:00
}
postLink();