fix wip aggregate pictures

This commit is contained in:
Tykayn 2025-01-12 17:09:13 +01:00 committed by tykayn
parent 7ef228aa49
commit 4026118826
6 changed files with 4221 additions and 20 deletions

View File

@ -91,9 +91,12 @@ http://localhost:8000
# Notes # Notes
ce site n'a pas besoin d'être hébergé sur le serveur de l'instance qu'il utilise ce site n'a pas besoin d'être hébergé sur le serveur de l'instance qu'il utilise.
Venez causer sur mastodon! Venez causer sur mastodon!
Ceci utilise un fichier sqlite comme base de données, vous pouvez faire des ajouts en base avec un bon IDE sans avoir à
Ceci utilise un fichier sqlite et des jsons comme base de données, vous pouvez faire des ajouts en base avec un bon IDE
sans avoir à
développer d'interface web. développer d'interface web.
Les merge requests sont bienvenues. Les merge requests sont bienvenues.

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

83
gather_pics.js Normal file
View File

@ -0,0 +1,83 @@
const sharp = require('sharp');
const axios = require('axios');
const fs = require('fs').promises;
const path = require('path');
async function downloadImage(url, destination) {
try {
const response = await axios({
url,
method: 'GET',
responseType: 'arraybuffer',
});
await fs.writeFile(destination, response.data);
} catch (error) {
console.error(`Erreur lors du téléchargement de l'image ${url}:`, error);
throw error;
}
}
async function generateImage(imageUrls, outputPath) {
const tempDir = path.join(__dirname, 'temp');
await fs.mkdir(tempDir, {recursive: true});
const images = [];
for (const imageUrl of imageUrls) {
try {
const tempPath = path.join(tempDir, `${Date.now()}-${Math.random().toString(36).substr(2, 9)}.jpg`);
await downloadImage(imageUrl, tempPath);
images.push(sharp(tempPath));
} catch (error) {
console.error(`Impossible de télécharger l'image ${imageUrl}. Elle sera ignorée.`);
}
}
if (images.length === 0) {
throw new Error("Aucune image n'a pu être téléchargée.");
}
const firstImage = await images[0].metadata();
const width = firstImage.width;
const height = firstImage.height;
const composite = sharp({
create: {
width: width * images.length,
height,
channels: 4,
background: {r: 255, g: 255, b: 255, alpha: 1}
}
});
const compositeOperations = await Promise.all(images.map(async (image, index) => ({
input: await image.toBuffer(),
top: 0,
left: width * index
})));
await composite.composite(compositeOperations).toFile(outputPath);
// Supprimer les images temporaires
for (const image of images) {
image.destroy();
}
const files = await fs.readdir(tempDir);
for (const file of files) {
await fs.unlink(path.join(tempDir, file));
}
}
// Exemple d'utilisation
const imageUrls = [
'https://mastodon.cipherbliss.com/system/cache/accounts/avatars/000/012/606/original/37deaa900b17861d.png',
'https://mastodon.cipherbliss.com/system/cache/accounts/avatars/000/012/606/original/37deaa900b17861d.png',
'https://mastodon.cipherbliss.com/system/cache/accounts/avatars/000/012/606/original/37deaa900b17861d.png',
];
const outputPath = path.join(__dirname, 'documents', 'generated_pictures', 'combined_images.jpg');
generateImage(imageUrls, outputPath)
.then(() => console.log('Image générée avec succès!'))
.catch((err) => console.error('Erreur lors de la génération de l\'image:', err));

4136
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,6 @@
"@databases/sqlite": "^4.0.0", "@databases/sqlite": "^4.0.0",
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"debug": "~2.6.9", "debug": "~2.6.9",
"dotenv": "^16.0.1",
"express": "~4.16.1", "express": "~4.16.1",
"http-errors": "~1.6.3", "http-errors": "~1.6.3",
"jade": "~1.11.0", "jade": "~1.11.0",
@ -30,8 +29,10 @@
"axios": "^1.6.7", "axios": "^1.6.7",
"cheerio": "^1.0.0-rc.12", "cheerio": "^1.0.0-rc.12",
"csv-parser": "^3.1.0", "csv-parser": "^3.1.0",
"dotenv": "^16.4.7",
"fs": "^0.0.1-security", "fs": "^0.0.1-security",
"https": "^1.0.0", "https": "^1.0.0",
"mastodon-api": "^1.3.0",
"node-fetch": "^3.2.10", "node-fetch": "^3.2.10",
"nodemon": "^2.0.22", "nodemon": "^2.0.22",
"path": "^0.12.7", "path": "^0.12.7",

View File

@ -1,13 +1,13 @@
import fs from "fs"; import fs from "fs";
import path from 'path'; import path from 'path';
import {getRandomElementOfArray, listFilesOfFolder} from '../libs/utils.mjs' import {getRandomElementOfArray, listFilesOfFolder} from '../helpers/libs/utils.mjs'
import Masto from "mastodon"; import Masto from "mastodon";
import pictureFolderDescriptions from "../helpers/libs/describe_picture_folders.mjs"
const __dirname = path.resolve(); const __dirname = path.resolve();
// choisir un type de publication au hasard dans les dossiers "picture" // choisir un type de publication au hasard dans les dossiers "picture"
import pictureFolderDescriptions from "../libs/describe_picture_folders.mjs"
const typeOfFolder = getRandomElementOfArray(Object.keys(pictureFolderDescriptions)) const typeOfFolder = getRandomElementOfArray(Object.keys(pictureFolderDescriptions))
console.log(typeOfFolder) console.log(typeOfFolder)
@ -17,6 +17,7 @@ const folderMemePublished = 'assets/pictures/meme/published/'
const list_unpublished_images = listFilesOfFolder(folderMemeUnpublished) const list_unpublished_images = listFilesOfFolder(folderMemeUnpublished)
const reallySendPost = false; const reallySendPost = false;
// const reallySendPost = true; // const reallySendPost = true;
@ -28,7 +29,7 @@ function postLink() {
let selectedImage = 'meme_default.jpg'; let selectedImage = 'meme_default.jpg';
if (list_unpublished_images.length) { if (list_unpublished_images.length) {
selectedImage = getRandomElementOfArray(list_unpublished_images) selectedImage = getRandomElementOfArray(list_unpublished_images)
}else{ } else {
console.log("----- nothing to publish -----") console.log("----- nothing to publish -----")
return; return;
} }
@ -57,7 +58,7 @@ function postLink() {
api_url: process.env.INSTANCE_MASTODON + '/api/v1/', api_url: process.env.INSTANCE_MASTODON + '/api/v1/',
}); });
masto.post('media', {file: fs.createReadStream( folderMemeUnpublished + configPost.image)}) masto.post('media', {file: fs.createReadStream(folderMemeUnpublished + configPost.image)})
.then(resp => { .then(resp => {
id = resp.data.id; id = resp.data.id;
configPost.media_ids = [id] configPost.media_ids = [id]
@ -82,7 +83,8 @@ function postLink() {
* move to published * move to published
* @param imageName * @param imageName
*/ */
function moveImageToPublishedFolder(imageName){ function moveImageToPublishedFolder(imageName) {
return fs.renameSync(folderMemeUnpublished + imageName, folderMemePublished + imageName) return fs.renameSync(folderMemeUnpublished + imageName, folderMemePublished + imageName)
} }
postLink(); postLink();