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, {
|
||||
randomIntFromInterval,
|
||||
getRandomElementOfArray,
|
||||
findPictureAndSendPost
|
||||
findPictureAndSendPost, getRandomLinkGeneral
|
||||
} from './utils.mjs'
|
||||
|
||||
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 list_posts_json = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/qzine_posts_all.json", 'utf-8'))
|
||||
let reallySendPost = false;
|
||||
// reallySendPost = true;
|
||||
|
||||
|
||||
function getRandomLinkQzine() {
|
||||
@ -33,7 +31,16 @@ function getRandomLinkQzine() {
|
||||
|
||||
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)
|
||||
|
||||
let filteredExcerpt = postObject.excerpt.rendered.replace(/<[^>]+>/g, '')
|
||||
@ -57,7 +64,6 @@ function postLink() {
|
||||
* ${postObject.link}
|
||||
${filteredExcerpt}
|
||||
* #qzine #qzineblog #blog #sexe #relations #éducation #plaisir #sociologie`,
|
||||
reallySendPost
|
||||
}
|
||||
|
||||
// console.log('postContent' , postContent)
|
||||
|
@ -2,7 +2,7 @@ import fs from "fs";
|
||||
import path from 'path';
|
||||
import {
|
||||
randomIntFromInterval,
|
||||
findPictureAndSendPost, getRandomLinkGeneral
|
||||
findPictureAndSendPost, getRandomLinkGeneral, hasCliArgument, slugify
|
||||
|
||||
} from './utils.mjs'
|
||||
import Masto from "mastodon";
|
||||
@ -11,14 +11,20 @@ const __dirname = path.resolve();
|
||||
const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
|
||||
const tkpostsjson = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/tykayn_wptkblog_posts.json", 'utf-8'))
|
||||
|
||||
let reallySendPost = false;
|
||||
|
||||
// reallySendPost = true;
|
||||
|
||||
|
||||
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")
|
||||
|
||||
let filteredExcerpt = postObject.post_content.replace(/<[^>]+>/g, '')
|
||||
@ -32,16 +38,16 @@ function postLink() {
|
||||
let configPost = {
|
||||
author: 'tykayn',
|
||||
website: 'tykayn_blog',
|
||||
slug: postObject.guid,
|
||||
slug: slugify(postObject.post_title),
|
||||
postObject,
|
||||
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})
|
||||
|
||||
* ${postObject.guid}
|
||||
" ${filteredExcerpt} "
|
||||
|
||||
> "${filteredExcerpt}"
|
||||
* #tykayn #tkblog #blog`,
|
||||
reallySendPost
|
||||
}
|
||||
// ajouter la première image du post de blog
|
||||
findPictureAndSendPost(postObject.post_content, configPost)
|
||||
|
@ -6,6 +6,8 @@ import https from 'https';
|
||||
|
||||
|
||||
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
|
||||
return Math.floor(Math.random() * (max - min + 1) + min) | 1
|
||||
@ -23,7 +25,7 @@ export let defaultConfigMasto = {
|
||||
visibility: 'public',
|
||||
language: 'fr',
|
||||
sensitive: false,
|
||||
reallySendPost: false,
|
||||
reallySendPost,
|
||||
image: '',
|
||||
folder_image: process.cwd() + '/assets/blog_posts_medias/',
|
||||
message: "Hey coucou! on est le" + nowDate,
|
||||
@ -36,7 +38,6 @@ export let defaultConfigMasto = {
|
||||
|
||||
|
||||
export function tokenForAuthorIsPresentInDotEnv(author) {
|
||||
console.log(" process.env['TOKEN_' + author.toUpperCase()]", process.env['TOKEN_' + author.toUpperCase()])
|
||||
return process.env['TOKEN_' + author.toUpperCase()];
|
||||
}
|
||||
|
||||
@ -47,6 +48,7 @@ export function tokenForAuthorIsPresentInDotEnv(author) {
|
||||
*/
|
||||
export default function sendPostMastodon(config) {
|
||||
|
||||
console.log('send post', config.postObject.post_guid , config.postObject.guid )
|
||||
// override defaults with input argument
|
||||
config = {
|
||||
...defaultConfigMasto,
|
||||
@ -56,7 +58,7 @@ export default function sendPostMastodon(config) {
|
||||
// console.log("sendPostMastodon config", config)
|
||||
|
||||
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 {
|
||||
|
||||
console.log(" ")
|
||||
@ -112,13 +114,14 @@ export default function sendPostMastodon(config) {
|
||||
var id;
|
||||
console.log("envoi du média", config.image)
|
||||
// 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 => {
|
||||
id = resp.data.id;
|
||||
params.media_ids = [id]
|
||||
console.log("\n ✅ image, id", id)
|
||||
masto.post('statuses', params).then(rep => {
|
||||
// console.log('rep', rep)
|
||||
console.log("posté avec une nouvelle image, WOOT")
|
||||
console.log("\n ✅ posté avec une nouvelle image, WOOT")
|
||||
}, err => {
|
||||
console.error(err)
|
||||
|
||||
@ -298,10 +301,14 @@ export function findPictureAndSendPost(postContent, configPost){
|
||||
if (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)
|
||||
.then((res) => {
|
||||
// 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)
|
||||
|
||||
@ -323,4 +330,17 @@ export function findPictureAndSendPost(postContent, configPost){
|
||||
// on envoie avec l'image par défaut
|
||||
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