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

63 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-08-08 22:21:27 +02:00
import fs from "fs";
import path from 'path';
2022-12-07 17:39:46 +01:00
import sendPostMastodon, {randomIntFromInterval, findFirstImageInContent} from './utils.mjs'
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-12-07 17:39:46 +01:00
let reallySendPost = false;
reallySendPost = true;
2022-08-08 22:21:27 +02:00
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',
2022-12-07 17:39:46 +01:00
// image: 'qzine_default.jpg',
image: 'qzine_post_' + randomIntFromInterval(1 , 3) + '.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
}
2022-12-07 17:39:46 +01:00
// console.log("configPost.message", configPost.message)
2022-08-08 22:21:27 +02:00
sendPostMastodon(configPost)
}
postLink();