diff --git a/.gitignore b/.gitignore
index df8ae9a..375c8cd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,4 @@ mastodon_multi_accounts.db
assets/pictures/meme/not_published/*
assets/pictures/meme/published/*
#!/assets/documents/qzine_posts_all.json
+assets/blog_posts_medias/*
\ No newline at end of file
diff --git a/assets/pictures/tykayn/nsfw/not_published/pouets_disparus_tykayn_mastodon.jpg b/assets/pictures/tykayn/nsfw/not_published/pouets_disparus_tykayn_mastodon.jpg
new file mode 100644
index 0000000..95ca2a8
Binary files /dev/null and b/assets/pictures/tykayn/nsfw/not_published/pouets_disparus_tykayn_mastodon.jpg differ
diff --git a/helpers/tykayn_blog.mjs b/helpers/tykayn_blog.mjs
index 0dd440f..19ded46 100644
--- a/helpers/tykayn_blog.mjs
+++ b/helpers/tykayn_blog.mjs
@@ -1,10 +1,14 @@
import fs from "fs";
import path from 'path';
-import sendPostMastodon, {randomIntFromInterval} from './utils.mjs'
+import sendPostMastodon, {findFirstImageInContent, randomIntFromInterval, downloadImage} from './utils.mjs'
+import Masto from "mastodon";
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;
@@ -21,11 +25,10 @@ function getRandomLink() {
function postLink() {
let postObject = getRandomLink()
- console.log("envoi de post par le Curator")
+ console.log("envoi de post par tykayn")
let filteredExcerpt = postObject.post_content.replace(/<[^>]+>/g, '')
let counterLength = filteredExcerpt.length;
-
let limitExcerpt = 250
filteredExcerpt = filteredExcerpt.substring(0, limitExcerpt)
if (filteredExcerpt && counterLength > limitExcerpt) {
@@ -34,7 +37,7 @@ function postLink() {
let configPost = {
author: 'tykayn',
- image: 'tk_blog_bannieres_oct2010-' + randomIntFromInterval(1 , 10) + '.jpg',
+ image: 'tk_blog_bannieres_oct2010-' + randomIntFromInterval(1, 10) + '.jpg',
message: `# [${postObject.post_title}](${postObject.guid})
* ${postObject.post_date} - ${postObject.guid}
@@ -42,8 +45,39 @@ ${filteredExcerpt}
* #tykayn #tkblog #blog`,
reallySendPost
}
- console.log("configPost.message", configPost.message)
- sendPostMastodon(configPost)
+
+ // 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.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)
+ }
+
+
}
postLink();
\ No newline at end of file
diff --git a/helpers/utils.mjs b/helpers/utils.mjs
index d4e39f1..81ea799 100644
--- a/helpers/utils.mjs
+++ b/helpers/utils.mjs
@@ -1,13 +1,12 @@
import Masto from "mastodon";
import dotenv from "dotenv";
-import fs from "node-fs";
-import loadedHtml, {load} from "cheerio";
-import $ from "cheerio";
+import sharp from 'sharp';
+import fs from 'fs';
+import https from 'https';
+
let local_node_env_conf = dotenv.config()
-// console.log("conf", local_node_env_conf)
-
export function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min) | 1
}
@@ -33,11 +32,11 @@ export let defaultConfigMasto = {
export function tokenForAuthorIsPresentInDotEnv(author) {
- console.log(" process.env['TOKEN_' + author.toUpperCase()]", process.env['TOKEN_' + author.toUpperCase()])
+ console.log(" process.env['TOKEN_' + author.toUpperCase()]", process.env['TOKEN_' + author.toUpperCase()])
return process.env['TOKEN_' + author.toUpperCase()];
}
-export default function sendPostMastodon(config){
+export default function sendPostMastodon(config) {
// override defaults with input argument
config = {
@@ -45,7 +44,7 @@ export default function sendPostMastodon(config){
...config,
}
- console.log("sendPostMastodon config", 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 + " =========== ")
@@ -77,7 +76,6 @@ export default function sendPostMastodon(config){
let dateschedule = new Date(config.scheduled_at)
params['scheduled_at'] = dateschedule.toISOString()
}
- console.log(config)
/**
* envoi sans fichier joint
@@ -87,7 +85,7 @@ export default function sendPostMastodon(config){
if (config.reallySendPost) {
masto.post('statuses', params).then(rep => {
- // console.log('rep', rep)
+
console.log("posté, yay!")
}, err => {
console.error(err)
@@ -104,34 +102,21 @@ export default function sendPostMastodon(config){
var id;
console.log("envoi du média", config.image)
- // if(config.image === 'osm_default.jpg'){
- // id = '108787661095227871';
- // params.media_ids = [id]
- // masto.post('statuses', params).then(rep => {
- // console.log("posté avec l'image osm_default déjà enregistrée")
- // // console.log('rep', rep)
- // }, err => {
- //
- // console.error(err)
- // console.log("erreur T_T")
- // })
- //
- // }else{
- // upload new media
- return masto.post('media', {file: fs.createReadStream('assets/' + config.image)})
- .then(resp => {
- id = resp.data.id;
- params.media_ids = [id]
- masto.post('statuses', params).then(rep => {
- // console.log('rep', rep)
- console.log("posté avec une nouvelle image, WOOT")
- }, err => {
- console.error(err)
+ // upload new media
+ return masto.post('media', {file: fs.createReadStream('assets/' + config.image)})
+ .then(resp => {
+ id = resp.data.id;
+ params.media_ids = [id]
+ masto.post('statuses', params).then(rep => {
+ // console.log('rep', rep)
+ console.log("posté avec une nouvelle image, WOOT")
+ }, err => {
+ console.error(err)
- console.log("erreur T_T")
- })
+ console.log("erreur T_T")
})
- }
+ })
+ }
// }
@@ -147,7 +132,7 @@ export default function sendPostMastodon(config){
* lister les noms de fichier que l'on peut publier dans un dossier.
* retourne un tableau
*/
-export function listFilesOfFolder(folderPath){
+export function listFilesOfFolder(folderPath) {
let filesNames = []
fs.readdirSync(folderPath).map(fileName => {
return filesNames.push(fileName);
@@ -161,7 +146,7 @@ export function listFilesOfFolder(folderPath){
* crée un dossier d'assets, avec ses sous dossiers not_published et published si ils manquent.
* une fois que l'on prendra une image dans le dossier non publié, on la déplacera dans le dossier des images publées.
*/
-export function initializeFolderForPictures(folderName){
+export function initializeFolderForPictures(folderName) {
try {
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
@@ -171,13 +156,67 @@ export function initializeFolderForPictures(folderName){
}
}
-export function findFirstImageInContent(htmlContent){
- let firstImgSource = ''
- let loadedHtmlCheerio = load(htmlContent)
- let imageObj = loadedHtmlCheerio(htmlContent).find('img').get()
-
- if($(imageObj) && $(imageObj).attr('src')){
- firstImgSource = $(imageObj).attr('src')
+/**
+ * find first image in blog post and return the src value of the img tag
+ * @param htmlContent
+ * @returns {string}
+ */
+export function findFirstImageInContent(htmlContent) {
+ let result = ''
+ let first = htmlContent.match(/]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>/)[0];
+ if (first) {
+ result = first.match(/src\=\"(.*)\"/i)
+ if(result.length && result[0]){
+ result = result[0].split('"')
+ result = result[1]
+ }
}
- return firstImgSource;
+ 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')
+ return result;
+}
+
+/**
+ * usage:
+ * downloadImage('https://upload.wikimedia.org/wikipedia/en/thumb/7/7d/Lenna_%28test_image%29.png/440px-Lenna_%28test_image%29.png', 'lena.png')
+ * .then(console.log)
+ * .catch(console.error);
+ * @param url
+ * @param filepath
+ * @returns {Promise}
+ */
+export function downloadImage(url, filepath) {
+ return new Promise((resolve, reject) => {
+ https.get(url, (res) => {
+ if (res.statusCode === 200) {
+ res.pipe(fs.createWriteStream(filepath))
+ .on('error', reject)
+ .once('close', () => resolve(filepath));
+ } else {
+ // Consume response data to free up memory
+ res.resume();
+ reject(new Error(`Request Failed With a Status Code: ${res.statusCode}; \n ${res.statusMessage} `));
+
+ }
+ });
+ });
+}
+
+/**
+ * faire un
+ * @param pictureName
+ * @param width
+ * @param height
+ * @returns {Promise