generalize posting with first found picture in content
This commit is contained in:
parent
656e1e43fd
commit
bd8095e822
@ -179,7 +179,7 @@
|
||||
"post_author": 1,
|
||||
"post_date": "2015-11-22 20:42:54",
|
||||
"post_date_gmt": "2015-11-22 19:42:54",
|
||||
"post_content": "<p><a href=\"http://tykayn.fr/wp-content/uploads/i/2015/11novembre/journee_langues_preview.jpg\" title=\"journee_langues_preview.jpg\"><img src=\"http://blog.artlemoine.com//public/i/2015/11novembre/.journee_langues_preview_m.jpg\" alt=\"journee_langues_preview.jpg\" style=\"display:block; margin:0 auto;\" title=\"journee_langues_preview.jpg, nov. 2015\" /></a>\nUne ptite illu sur qzine pour ma part aujourdhui. Je l'ai aussi mise <a href=\"http://tykayn.deviantart.com/art/tongues-day-573234936\" hreflang=\"fr\">sur mon deviantart</a> si vous voulez voir d'autres de mes gribouilles :3\n<a href=\"http://qzine.fr/blog/maitriser-les-langues/\">http://qzine.fr/blog/maitriser-les-langues/</a>\nD'ailleurs sur qzine.fr on se prépare avec plein d'artistes pour le projet foufou de publier un calendrier de l'avent qui va réchauffer l'hiver, ça va être beau *-* <br /></p>",
|
||||
"post_content": "<p><a href=\"http://tykayn.fr/wp-content/uploads/i/2015/11novembre/journee_langues_preview.jpg\" title=\"journee_langues_preview.jpg\"><img src=\"https://www.tykayn.fr/wp-content/uploads/i/2015/11novembre/.journee_langues_preview_m.jpg\" alt=\"journee_langues_preview.jpg\" style=\"display:block; margin:0 auto;\" title=\"journee_langues_preview.jpg, nov. 2015\" /></a>\nUne ptite illu sur qzine pour ma part aujourdhui. Je l'ai aussi mise <a href=\"http://tykayn.deviantart.com/art/tongues-day-573234936\" hreflang=\"fr\">sur mon deviantart</a> si vous voulez voir d'autres de mes gribouilles :3\n<a href=\"http://qzine.fr/blog/maitriser-les-langues/\">http://qzine.fr/blog/maitriser-les-langues/</a>\nD'ailleurs sur qzine.fr on se prépare avec plein d'artistes pour le projet foufou de publier un calendrier de l'avent qui va réchauffer l'hiver, ça va être beau *-* <br /></p>",
|
||||
"post_title": "une rousse une langue",
|
||||
"post_excerpt": "",
|
||||
"post_status": "publish",
|
||||
|
@ -1,11 +1,11 @@
|
||||
import fs from "fs";
|
||||
import path from 'path';
|
||||
import sendPostMastodon, {randomIntFromInterval, findFirstImageInContent} from './utils.mjs'
|
||||
import sendPostMastodon, {randomIntFromInterval, findFirstImageInContent, findPictureAndSendPost} from './utils.mjs'
|
||||
|
||||
const __dirname = path.resolve();
|
||||
const tkpostsjson = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/cipherbliss_tkwp_posts.json", 'utf-8'))
|
||||
let reallySendPost = false;
|
||||
reallySendPost = true;
|
||||
// reallySendPost = true;
|
||||
|
||||
|
||||
export function getRandomElementOfArray(listItems) {
|
||||
@ -35,12 +35,14 @@ function postLink() {
|
||||
|
||||
let configPost = {
|
||||
author: 'tykayn',
|
||||
website: 'cipherbliss',
|
||||
slug: postObject.post_name,
|
||||
postObject,
|
||||
folder_image: folderBlogPostsPreview,
|
||||
image: 'cipherbliss_post_' + randomIntFromInterval(1 , 3) + '.jpg',
|
||||
message: `# [${postObject.post_title}](https://www.cipherbliss.com/${postObject.post_name})
|
||||
|
||||
* ${postObject.post_date}
|
||||
|
||||
> ${filteredExcerpt}
|
||||
" ${filteredExcerpt} "
|
||||
* #tykayn #cipherbliss #blog`,
|
||||
reallySendPost
|
||||
}
|
||||
@ -48,7 +50,7 @@ function postLink() {
|
||||
console.log("configPost.image", configPost.image)
|
||||
|
||||
|
||||
sendPostMastodon(configPost)
|
||||
findPictureAndSendPost(postObject.content.rendered, configPost)
|
||||
console.log("post ID: ", postObject.ID)
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,13 @@
|
||||
import fs from "fs";
|
||||
import path from 'path';
|
||||
import sendPostMastodon, {randomIntFromInterval, findFirstImageInContent} from './utils.mjs'
|
||||
import sendPostMastodon, {
|
||||
randomIntFromInterval,
|
||||
findFirstImageInContent,
|
||||
downloadImage,
|
||||
findPictureAndSendPost
|
||||
} from './utils.mjs'
|
||||
|
||||
const folderBlogPostsPreview = process.cwd() + '/assets/blog_posts_medias/'
|
||||
|
||||
/**
|
||||
*
|
||||
@ -18,7 +24,7 @@ import sendPostMastodon, {randomIntFromInterval, findFirstImageInContent} from '
|
||||
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;
|
||||
// reallySendPost = true;
|
||||
|
||||
|
||||
export function getRandomElementOfArray(listItems) {
|
||||
@ -26,14 +32,14 @@ export function getRandomElementOfArray(listItems) {
|
||||
}
|
||||
|
||||
|
||||
function getRandomLink() {
|
||||
function getRandomLinkQzine() {
|
||||
let filteredLinks = list_posts_json.filter(elem => elem.status === 'publish')
|
||||
return getRandomElementOfArray(filteredLinks)
|
||||
}
|
||||
|
||||
function postLink() {
|
||||
|
||||
let postObject = getRandomLink()
|
||||
let postObject = getRandomLinkQzine()
|
||||
console.log("envoi de post par le compte Qzine - post n°" ,postObject.id)
|
||||
|
||||
let filteredExcerpt = postObject.excerpt.rendered.replace(/<[^>]+>/g, '')
|
||||
@ -47,17 +53,21 @@ function postLink() {
|
||||
|
||||
let configPost = {
|
||||
author: 'qzine',
|
||||
// image: 'qzine_default.jpg',
|
||||
website: 'qzine',
|
||||
slug: postObject.title.rendered,
|
||||
postObject,
|
||||
folder_image: folderBlogPostsPreview,
|
||||
image: 'qzine_post_' + randomIntFromInterval(1 , 4) + '.jpg',
|
||||
message: `# [ !Qzine - ${postObject.title.rendered}](${postObject.link})
|
||||
message: `!Qzine - ${postObject.title.rendered}
|
||||
|
||||
* ${postObject.date} - ${postObject.link}
|
||||
* ${postObject.link}
|
||||
${filteredExcerpt}
|
||||
* #qzine #qzineblog #blog #sexe #relations #éducation #plaisir #sociologie`,
|
||||
reallySendPost
|
||||
}
|
||||
// console.log("configPost.message", configPost.message)
|
||||
sendPostMastodon(configPost)
|
||||
|
||||
// console.log('postContent' , postContent)
|
||||
findPictureAndSendPost(postObject.content.rendered, configPost)
|
||||
}
|
||||
|
||||
postLink();
|
@ -1,6 +1,9 @@
|
||||
import fs from "fs";
|
||||
import path from 'path';
|
||||
import sendPostMastodon, {findFirstImageInContent, randomIntFromInterval, downloadImage} from './utils.mjs'
|
||||
import {
|
||||
randomIntFromInterval,
|
||||
findPictureAndSendPost, getRandomLinkGeneral
|
||||
} from './utils.mjs'
|
||||
import Masto from "mastodon";
|
||||
|
||||
const __dirname = path.resolve();
|
||||
@ -9,22 +12,13 @@ const tkpostsjson = JSON.parse(fs.readFileSync(__dirname + "/assets/documents/ty
|
||||
|
||||
let reallySendPost = false;
|
||||
|
||||
reallySendPost = true;
|
||||
// reallySendPost = true;
|
||||
|
||||
|
||||
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()
|
||||
let postObject = getRandomLinkGeneral(tkpostsjson)
|
||||
console.log("envoi de post par tykayn")
|
||||
|
||||
let filteredExcerpt = postObject.post_content.replace(/<[^>]+>/g, '')
|
||||
@ -37,47 +31,20 @@ function postLink() {
|
||||
|
||||
let configPost = {
|
||||
author: 'tykayn',
|
||||
website: 'tykayn_blog',
|
||||
slug: postObject.guid,
|
||||
postObject,
|
||||
folder_image: folderBlogPostsPreview,
|
||||
image: 'tk_blog_bannieres_oct2010-' + randomIntFromInterval(1, 10) + '.jpg',
|
||||
message: `# [${postObject.post_title}](${postObject.guid})
|
||||
|
||||
* ${postObject.post_date} - ${postObject.guid}
|
||||
${filteredExcerpt}
|
||||
* ${postObject.guid}
|
||||
" ${filteredExcerpt} "
|
||||
* #tykayn #tkblog #blog`,
|
||||
reallySendPost
|
||||
}
|
||||
|
||||
// ajouter la première image du post de blog
|
||||
let firstPictureSource = findFirstImageInContent(postObject.post_content);
|
||||
if (firstPictureSource) {
|
||||
console.log("firstPictureSource found", firstPictureSource)
|
||||
|
||||
downloadImage(firstPictureSource, folderBlogPostsPreview + 'media_post_' + postObject.post_name + '.jpg')
|
||||
.then((res) => {
|
||||
// suite du poste avec upload d'image
|
||||
console.log('on envoie le media et l image')
|
||||
configPost.folder_image = folderBlogPostsPreview;
|
||||
configPost.image = 'media_post_' + postObject.post_name + '.jpg';
|
||||
sendPostMastodon(configPost)
|
||||
|
||||
},
|
||||
(err) => {
|
||||
console.log('pas dimage trouvée pour l URL ', firstPictureSource, err)
|
||||
sendPostMastodon(configPost)
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log('erreur avec cette URL ', firstPictureSource, err)
|
||||
sendPostMastodon(configPost)
|
||||
})
|
||||
} else {
|
||||
|
||||
// no image provided
|
||||
|
||||
console.log("pas d'image dans le corps du texte")
|
||||
// on envoie avec l'image par défaut
|
||||
sendPostMastodon(configPost)
|
||||
}
|
||||
|
||||
findPictureAndSendPost(postObject.post_content, configPost)
|
||||
|
||||
}
|
||||
|
||||
|
@ -25,10 +25,13 @@ export let defaultConfigMasto = {
|
||||
sensitive: false,
|
||||
reallySendPost: false,
|
||||
image: '',
|
||||
folder_image: 'assets/',
|
||||
folder_image: process.cwd() + '/assets/blog_posts_medias/',
|
||||
message: "Hey coucou! on est le" + nowDate,
|
||||
scheduled_at: "",
|
||||
content_type: "text/markdown"
|
||||
content_type: "text/markdown",
|
||||
website: 'qzine',
|
||||
slug: 'default_post_title',
|
||||
postObject : {},
|
||||
}
|
||||
|
||||
|
||||
@ -37,6 +40,11 @@ export function tokenForAuthorIsPresentInDotEnv(author) {
|
||||
return process.env['TOKEN_' + author.toUpperCase()];
|
||||
}
|
||||
|
||||
/**
|
||||
* send post to mastodon with config
|
||||
* @param config
|
||||
* @returns {*}
|
||||
*/
|
||||
export default function sendPostMastodon(config) {
|
||||
|
||||
// override defaults with input argument
|
||||
@ -128,6 +136,32 @@ export default function sendPostMastodon(config) {
|
||||
}
|
||||
}
|
||||
|
||||
// Slugify a string
|
||||
export function slugify(str)
|
||||
{
|
||||
str = str.replace(/^\s+|\s+$/g, '');
|
||||
|
||||
// Make the string lowercase
|
||||
str = str.toLowerCase();
|
||||
|
||||
// Remove accents, swap ñ for n, etc
|
||||
var from = "ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;";
|
||||
var to = "AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------";
|
||||
for (var i=0, l=from.length ; i<l ; i++) {
|
||||
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
|
||||
}
|
||||
|
||||
// Remove invalid chars
|
||||
str = str.replace(/[^a-z0-9 -]/g, '')
|
||||
// Collapse whitespace and replace by -
|
||||
.replace(/\s+/g, '-')
|
||||
// Collapse dashes
|
||||
.replace(/-+/g, '-');
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @name listFilesOfFolder
|
||||
* lister les noms de fichier que l'on peut publier dans un dossier.
|
||||
@ -162,9 +196,17 @@ export function initializeFolderForPictures(folderName) {
|
||||
* @param htmlContent
|
||||
* @returns {string}
|
||||
*/
|
||||
export function findFirstImageInContent(htmlContent) {
|
||||
export function findFirstImageInContent(htmlContent='') {
|
||||
let result = ''
|
||||
let first = htmlContent.match(/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/)[0];
|
||||
let foundPictures = htmlContent.match(/<img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/);
|
||||
let first = '';
|
||||
if(foundPictures && foundPictures[0]){
|
||||
|
||||
first = foundPictures[0]
|
||||
|
||||
}else{
|
||||
console.log('pas d image trouvée dans le contenu ', htmlContent)
|
||||
}
|
||||
if (first) {
|
||||
result = first.match(/src\=\"(.*)\"/i)
|
||||
if(result.length && result[0]){
|
||||
@ -172,12 +214,18 @@ export function findFirstImageInContent(htmlContent) {
|
||||
result = result[1]
|
||||
}
|
||||
}
|
||||
console.log(result)
|
||||
result = result.replace('http:', 'https:')
|
||||
result = result.replace('https://www.ailesse.info/~tykayn/bazar/kotlife', 'https://www.tykayn.fr/wp-content/uploads/i/kotlife')
|
||||
result = clearLink(result)
|
||||
console.log('clearLink', result)
|
||||
return result;
|
||||
}
|
||||
|
||||
function clearLink(linkString){
|
||||
linkString = linkString.replace('http:', 'https:')
|
||||
linkString = linkString.replace('https://www.ailesse.info/~tykayn/bazar/kotlife', 'https://www.tykayn.fr/wp-content/uploads/i/kotlife')
|
||||
linkString = linkString.replace('https://blog.artlemoine.com/public/i', 'https://www.tykayn.fr/wp-content/uploads/i')
|
||||
|
||||
return linkString
|
||||
}
|
||||
/**
|
||||
* usage:
|
||||
* downloadImage('https://upload.wikimedia.org/wikipedia/en/thumb/7/7d/Lenna_%28test_image%29.png/440px-Lenna_%28test_image%29.png', 'lena.png')
|
||||
@ -220,4 +268,62 @@ export function CropPictue(pictureName, width = 500, height = 300) {
|
||||
.toFile('thumb_' + pictureName, function (err) {
|
||||
if (err) console.log(err);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* prendre un post parmi tous ceux du blog, dans ceux qui ont été publiés
|
||||
* @returns {*}
|
||||
*/
|
||||
export function getRandomLinkGeneral(tkpostsjson) {
|
||||
let filteredLinks = []
|
||||
if (tkpostsjson[0].post_status) {
|
||||
|
||||
filteredLinks = tkpostsjson.filter(elem => elem.post_status === 'publish')
|
||||
} else if (tkpostsjson[0].status) {
|
||||
filteredLinks = tkpostsjson.filter(elem => elem.status === 'publish')
|
||||
}
|
||||
return getRandomElementOfArray(filteredLinks)
|
||||
}
|
||||
|
||||
export function getRandomElementOfArray(listItems) {
|
||||
return listItems[Math.floor(Math.random() * listItems.length)]
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* trouver l'image du contenu si il y en a
|
||||
* @param postContent
|
||||
* @param configPost
|
||||
*/
|
||||
export function findPictureAndSendPost(postContent, configPost){
|
||||
|
||||
let firstPictureSource = findFirstImageInContent(postContent);
|
||||
let filePathImage = `${configPost.folder_image}_${configPost.website}_media_post_${slugify(configPost.slug)}.jpg`
|
||||
if (firstPictureSource) {
|
||||
console.log("firstPictureSource found", firstPictureSource)
|
||||
|
||||
downloadImage(firstPictureSource, filePathImage)
|
||||
.then((res) => {
|
||||
// suite du poste avec upload d'image
|
||||
console.log('on envoie le media et l image' , filePathImage)
|
||||
|
||||
sendPostMastodon(configPost)
|
||||
|
||||
},
|
||||
(err) => {
|
||||
console.log('pas dimage trouvée pour l URL ', firstPictureSource, err)
|
||||
sendPostMastodon(configPost)
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log('erreur avec cette URL ', firstPictureSource, err)
|
||||
sendPostMastodon(configPost)
|
||||
})
|
||||
} else {
|
||||
|
||||
// no image provided
|
||||
|
||||
console.log("pas d'image dans le corps du texte", configPost.image)
|
||||
// on envoie avec l'image par défaut
|
||||
sendPostMastodon(configPost)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user