post with picture up
This commit is contained in:
parent
0e17ab9c6f
commit
af4ddd3c54
File diff suppressed because one or more lines are too long
@ -3,7 +3,7 @@ import path from 'path';
|
|||||||
import sendPostMastodon, {
|
import sendPostMastodon, {
|
||||||
randomIntFromInterval,
|
randomIntFromInterval,
|
||||||
getRandomElementOfArray,
|
getRandomElementOfArray,
|
||||||
findPictureAndSendPost
|
findPictureAndSendPost, getRandomLinkGeneral
|
||||||
} from './utils.mjs'
|
} from './utils.mjs'
|
||||||
|
|
||||||
const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
|
const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
|
||||||
@ -22,8 +22,6 @@ const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
|
|||||||
|
|
||||||
const __dirname = path.resolve();
|
const __dirname = path.resolve();
|
||||||
const list_posts_json = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/qzine_posts_all.json", 'utf-8'))
|
const list_posts_json = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/qzine_posts_all.json", 'utf-8'))
|
||||||
let reallySendPost = false;
|
|
||||||
// reallySendPost = true;
|
|
||||||
|
|
||||||
|
|
||||||
function getRandomLinkQzine() {
|
function getRandomLinkQzine() {
|
||||||
@ -33,7 +31,16 @@ function getRandomLinkQzine() {
|
|||||||
|
|
||||||
function postLink() {
|
function postLink() {
|
||||||
|
|
||||||
let postObject = getRandomLinkQzine()
|
let postObject = {};
|
||||||
|
let idOfPost = 1091;
|
||||||
|
// idOfPost = null;
|
||||||
|
if (idOfPost) {
|
||||||
|
postObject = list_posts_json.filter(elem => elem.id === idOfPost)[0]
|
||||||
|
console.log(postObject)
|
||||||
|
} else {
|
||||||
|
postObject = getRandomLinkGeneral(list_posts_json)
|
||||||
|
}
|
||||||
|
|
||||||
console.log("envoi de post par le compte Qzine - post n°" ,postObject.id)
|
console.log("envoi de post par le compte Qzine - post n°" ,postObject.id)
|
||||||
|
|
||||||
let filteredExcerpt = postObject.excerpt.rendered.replace(/<[^>]+>/g, '')
|
let filteredExcerpt = postObject.excerpt.rendered.replace(/<[^>]+>/g, '')
|
||||||
@ -57,7 +64,6 @@ function postLink() {
|
|||||||
* ${postObject.link}
|
* ${postObject.link}
|
||||||
${filteredExcerpt}
|
${filteredExcerpt}
|
||||||
* #qzine #qzineblog #blog #sexe #relations #éducation #plaisir #sociologie`,
|
* #qzine #qzineblog #blog #sexe #relations #éducation #plaisir #sociologie`,
|
||||||
reallySendPost
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('postContent' , postContent)
|
// console.log('postContent' , postContent)
|
||||||
|
@ -2,7 +2,7 @@ import fs from "fs";
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
randomIntFromInterval,
|
randomIntFromInterval,
|
||||||
findPictureAndSendPost, getRandomLinkGeneral
|
findPictureAndSendPost, getRandomLinkGeneral, hasCliArgument, slugify
|
||||||
|
|
||||||
} from './utils.mjs'
|
} from './utils.mjs'
|
||||||
import Masto from "mastodon";
|
import Masto from "mastodon";
|
||||||
@ -11,14 +11,20 @@ const __dirname = path.resolve();
|
|||||||
const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
|
const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
|
||||||
const tkpostsjson = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/tykayn_wptkblog_posts.json", 'utf-8'))
|
const tkpostsjson = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/tykayn_wptkblog_posts.json", 'utf-8'))
|
||||||
|
|
||||||
let reallySendPost = false;
|
|
||||||
|
|
||||||
// reallySendPost = true;
|
|
||||||
|
|
||||||
|
|
||||||
function postLink() {
|
function postLink() {
|
||||||
|
let postObject = {};
|
||||||
|
let idOfPost = 1930;
|
||||||
|
idOfPost = null;
|
||||||
|
if (idOfPost) {
|
||||||
|
postObject = tkpostsjson.filter(elem => elem.ID === idOfPost)[0]
|
||||||
|
console.log(postObject)
|
||||||
|
} else {
|
||||||
|
|
||||||
let postObject = getRandomLinkGeneral(tkpostsjson)
|
postObject = getRandomLinkGeneral(tkpostsjson)
|
||||||
|
}
|
||||||
console.log("envoi de post par tykayn")
|
console.log("envoi de post par tykayn")
|
||||||
|
|
||||||
let filteredExcerpt = postObject.post_content.replace(/<[^>]+>/g, '')
|
let filteredExcerpt = postObject.post_content.replace(/<[^>]+>/g, '')
|
||||||
@ -32,16 +38,16 @@ function postLink() {
|
|||||||
let configPost = {
|
let configPost = {
|
||||||
author: 'tykayn',
|
author: 'tykayn',
|
||||||
website: 'tykayn_blog',
|
website: 'tykayn_blog',
|
||||||
slug: postObject.guid,
|
slug: slugify(postObject.post_title),
|
||||||
postObject,
|
postObject,
|
||||||
folder_image: folderBlogPostsPreview,
|
folder_image: folderBlogPostsPreview,
|
||||||
image: 'tk_blog_bannieres_oct2010-' + randomIntFromInterval(1, 10) + '.jpg',
|
image: 'tk_blog_bannieres_oct2010-' + randomIntFromInterval(1, 9) + '.jpg',
|
||||||
message: `# [${postObject.post_title}](${postObject.guid})
|
message: `# [${postObject.post_title}](${postObject.guid})
|
||||||
|
|
||||||
* ${postObject.guid}
|
* ${postObject.guid}
|
||||||
" ${filteredExcerpt} "
|
|
||||||
|
> "${filteredExcerpt}"
|
||||||
* #tykayn #tkblog #blog`,
|
* #tykayn #tkblog #blog`,
|
||||||
reallySendPost
|
|
||||||
}
|
}
|
||||||
// ajouter la première image du post de blog
|
// ajouter la première image du post de blog
|
||||||
findPictureAndSendPost(postObject.post_content, configPost)
|
findPictureAndSendPost(postObject.post_content, configPost)
|
||||||
|
@ -6,6 +6,8 @@ import https from 'https';
|
|||||||
|
|
||||||
|
|
||||||
let local_node_env_conf = dotenv.config()
|
let local_node_env_conf = dotenv.config()
|
||||||
|
const myArgs = process.argv.slice(2);
|
||||||
|
let reallySendPost = hasCliArgument('--force');
|
||||||
|
|
||||||
export function randomIntFromInterval(min, max) { // min and max included
|
export function randomIntFromInterval(min, max) { // min and max included
|
||||||
return Math.floor(Math.random() * (max - min + 1) + min) | 1
|
return Math.floor(Math.random() * (max - min + 1) + min) | 1
|
||||||
@ -23,7 +25,7 @@ export let defaultConfigMasto = {
|
|||||||
visibility: 'public',
|
visibility: 'public',
|
||||||
language: 'fr',
|
language: 'fr',
|
||||||
sensitive: false,
|
sensitive: false,
|
||||||
reallySendPost: false,
|
reallySendPost,
|
||||||
image: '',
|
image: '',
|
||||||
folder_image: process.cwd() + '/assets/blog_posts_medias/',
|
folder_image: process.cwd() + '/assets/blog_posts_medias/',
|
||||||
message: "Hey coucou! on est le" + nowDate,
|
message: "Hey coucou! on est le" + nowDate,
|
||||||
@ -36,7 +38,6 @@ export let defaultConfigMasto = {
|
|||||||
|
|
||||||
|
|
||||||
export function tokenForAuthorIsPresentInDotEnv(author) {
|
export function tokenForAuthorIsPresentInDotEnv(author) {
|
||||||
console.log(" process.env['TOKEN_' + author.toUpperCase()]", process.env['TOKEN_' + author.toUpperCase()])
|
|
||||||
return process.env['TOKEN_' + author.toUpperCase()];
|
return process.env['TOKEN_' + author.toUpperCase()];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +48,7 @@ export function tokenForAuthorIsPresentInDotEnv(author) {
|
|||||||
*/
|
*/
|
||||||
export default function sendPostMastodon(config) {
|
export default function sendPostMastodon(config) {
|
||||||
|
|
||||||
|
console.log('send post', config.postObject.post_guid , config.postObject.guid )
|
||||||
// override defaults with input argument
|
// override defaults with input argument
|
||||||
config = {
|
config = {
|
||||||
...defaultConfigMasto,
|
...defaultConfigMasto,
|
||||||
@ -56,7 +58,7 @@ export default function sendPostMastodon(config) {
|
|||||||
// console.log("sendPostMastodon config", config)
|
// console.log("sendPostMastodon config", config)
|
||||||
|
|
||||||
if (!config.reallySendPost) {
|
if (!config.reallySendPost) {
|
||||||
console.log(" =========== le message ne sera PAS réellement posté sur le compte @" + config.author + "@" + process.env.INSTANCE_MASTODON + " =========== ")
|
console.log("\n =========== le message ne sera PAS réellement posté sur le compte @" + config.author + "@" + process.env.INSTANCE_MASTODON + " =========== \n")
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
console.log(" ")
|
console.log(" ")
|
||||||
@ -112,13 +114,14 @@ export default function sendPostMastodon(config) {
|
|||||||
var id;
|
var id;
|
||||||
console.log("envoi du média", config.image)
|
console.log("envoi du média", config.image)
|
||||||
// upload new media
|
// upload new media
|
||||||
return masto.post('media', {file: fs.createReadStream(config.folder_image + config.image)})
|
return masto.post('media', {file: fs.createReadStream(config.image)})
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
id = resp.data.id;
|
id = resp.data.id;
|
||||||
params.media_ids = [id]
|
params.media_ids = [id]
|
||||||
|
console.log("\n ✅ image, id", id)
|
||||||
masto.post('statuses', params).then(rep => {
|
masto.post('statuses', params).then(rep => {
|
||||||
// console.log('rep', rep)
|
// console.log('rep', rep)
|
||||||
console.log("posté avec une nouvelle image, WOOT")
|
console.log("\n ✅ posté avec une nouvelle image, WOOT")
|
||||||
}, err => {
|
}, err => {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
|
||||||
@ -298,10 +301,14 @@ export function findPictureAndSendPost(postContent, configPost){
|
|||||||
if (firstPictureSource) {
|
if (firstPictureSource) {
|
||||||
console.log("firstPictureSource found", firstPictureSource)
|
console.log("firstPictureSource found", firstPictureSource)
|
||||||
|
|
||||||
|
// check if picture already exist
|
||||||
|
console.log('on envoie le média et l image : ' , filePathImage)
|
||||||
downloadImage(firstPictureSource, filePathImage)
|
downloadImage(firstPictureSource, filePathImage)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
// suite du poste avec upload d'image
|
// suite du poste avec upload d'image
|
||||||
console.log('on envoie le media et l image' , filePathImage)
|
|
||||||
|
console.log('média téléchargé, on envoie le post')
|
||||||
|
configPost.image = filePathImage;
|
||||||
|
|
||||||
sendPostMastodon(configPost)
|
sendPostMastodon(configPost)
|
||||||
|
|
||||||
@ -324,3 +331,16 @@ export function findPictureAndSendPost(postContent, configPost){
|
|||||||
sendPostMastodon(configPost)
|
sendPostMastodon(configPost)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find cli argument
|
||||||
|
* @param argument
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
export function hasCliArgument(argument){
|
||||||
|
console.log('myArgs: ', myArgs);
|
||||||
|
return myArgs.indexOf(argument) !== -1
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user