multi-account-post-schedule.../helpers/curator_recommendation.mjs

116 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-12-06 12:08:36 +01:00
// import sendPostMastodon from "./libs/utils.mjs";
import axios from "axios"
import sharp from "sharp"
import path from "path"
import fs from "fs"
import os from "os"
2025-01-11 17:36:04 +01:00
import {downloadImage} from "./libs/utils.mjs";
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
/**
* créer un message de comptes à recommander
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
récupérer la liste des abonnés de ces comptes sur mastodon
à partir des abonnés du json documents/recommendations_abonnements.json
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
https://mastodon.cipherbliss.com/api/v1/accounts/2974/following
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
prendre 3 comptes au hasard, récupérer leurs profils, en tirer une présentation et un assemblage des photos de profil.
*/
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
// Largeur totale de l'image combinée
const totalWidth = 800;
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
// Hauteur totale de l'image combinée
const totalHeight = 600;
// Créer un dossier temporaire pour stocker les images téléchargées
const TEMP_DIR_NAME = 'tmp';
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), TEMP_DIR_NAME));
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
async function makePicture() {
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
// Noms des images à combiner
const names = ['Image 1', 'Image 2',
// 'Image 3'
];
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
console.log('tempDir:', tempDir)
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
Promise.all(urls.map((url, index) => {
// Télécharger l'image
return downloadImage(url, tempDir)
}))
.then((filePaths) => {
// Combiner les images en une seule image
return sharp(filePaths[0])
.resize(totalWidth / 3, totalHeight)
.append([
sharp(filePaths[1]).resize(totalWidth / 3, totalHeight),
sharp(filePaths[2]).resize(totalWidth / 3, totalHeight)
])
.toBuffer();
})
.then((buffer) => {
// Enregistrer l'image combinée
const outputFilePath = path.join(tempDir, 'combined.jpg');
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
return new Promise((resolve, reject) => {
fs.writeFile(outputFilePath, buffer, (error) => {
if (error) {
reject(error);
} else {
resolve(outputFilePath);
}
});
});
})
.then((outputFilePath) => {
// Afficher l'image combinée
console.log(`Combined image saved to ${outputFilePath}`);
})
.catch((error) => {
console.error(error);
});
2023-12-06 12:08:36 +01:00
2025-01-11 17:36:04 +01:00
}
async function getFollowers(username, instance) {
const url = `https://${instance}/api/v1/accounts/${username}/following`;
try {
const response = await axios.get(url);
return response.data;
} catch (error) {
console.error(error);
}
}
async function main() {
// prendre les abonnements d'un json donné dans les documents
console.log('followers', followers.length)
//
// const randomFollowers = followers.sort(() => Math.random() - 0.5)
//
// const selection = randomFollowers.slice(0, 3)
// console.log('Random followers:', randomFollowers.length);
//
// selection.forEach(elem => {
// console.log('selection', elem.username, elem.avatar_static)
// })
2023-12-06 12:08:36 +01:00
}
2025-01-11 17:36:04 +01:00
// makePicture();
console.log("recommandations du curateur, 3 comptes")
// console.log('urls[0]', urls[0])
//
// downloadImage(urls[0], tempDir)
main();