132 lines
3.3 KiB
JavaScript
132 lines
3.3 KiB
JavaScript
|
|
// 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"
|
|
import https from "https"
|
|
|
|
|
|
const downloadImage = async (url, name) => {
|
|
const outputPath = path.join(__dirname, 'images', name);
|
|
|
|
try {
|
|
const stats = await fs.promises.stat(outputPath);
|
|
|
|
if (stats.isFile()) {
|
|
console.log(`${name} already downloaded`);
|
|
return;
|
|
}
|
|
} catch (error) {
|
|
if (error.code == 'ENOENT') {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
|
const buffer = Buffer.from(response.data, 'binary');
|
|
|
|
await fs.promises.writeFile(outputPath, buffer);
|
|
|
|
console.log(`Downloaded ${name} and saved to ${outputPath}`);
|
|
};
|
|
|
|
async function makePicture(){
|
|
|
|
|
|
// URL des images à combiner
|
|
const urls = [
|
|
'https://mastodon.cipherbliss.com/system/cache/accounts/avatars/111/167/114/936/998/027/original/a80c6e973a5ebfa4.jpg',
|
|
'https://mastodon.cipherbliss.com/system/cache/accounts/avatars/111/322/992/248/243/542/original/23520c84291a6c41.png',
|
|
'https://mastodon.cipherbliss.com/system/cache/accounts/avatars/111/472/478/271/279/314/original/938c74ffb49473d1.jpg'
|
|
];
|
|
|
|
// Noms des images à combiner
|
|
const names = ['Image 1', 'Image 2', 'Image 3'];
|
|
|
|
// Largeur totale de l'image combinée
|
|
const totalWidth = 800;
|
|
|
|
// 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 ='my-temp-dir';
|
|
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), TEMP_DIR_NAME));
|
|
|
|
console.log('tempDir:',tempDir)
|
|
|
|
Promise.all(urls.map((url, index) => {
|
|
// Télécharger l'image
|
|
return downloadImage(url)
|
|
}))
|
|
.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');
|
|
|
|
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);
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
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() {
|
|
const username = '1';
|
|
const instance ='mastodon.cipherbliss.com';
|
|
|
|
const followers = await getFollowers(username, instance);
|
|
|
|
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)
|
|
})
|
|
}
|
|
|
|
// main();
|
|
makePicture();
|