import fs from "fs"; import axios from "axios"; const nombre_comptes_recommandés = 3 /** * recommander 3 comptes au hasard parmi les comptes listés dans document/recommendations_abonnements_tk.json */ async function recommendRandomAccount() { // Charger le fichier JSON const recommendations = JSON.parse(fs.readFileSync('documents/recommendations_abonnements_tk.json', 'utf8')); // Sélectionner 3 comptes au hasard const selectedAccounts = []; for (let i = 0; i < nombre_comptes_recommandés; i++) { const randomIndex = Math.floor(Math.random() * recommendations.length); const account = recommendations[randomIndex]; selectedAccounts.push(account); } // Récupérer les informations des comptes sélectionnés const accountsInfo = await Promise.all(selectedAccounts.map(async (account) => { console.log('account', account) const nickname = account.accountAddress; const instance = nickname.split('@')[1]; const username = nickname.split('@')[0]; const url = `https://mastodon.cipherbliss.com/api/v2/search?q=${nickname}&resolve=true&limit=1`; const token = process.env.USERTOKEN; // Remplacez par votre jeton d'accès try { const response = await axios.get(url, { headers: { Authorization: `Bearer ${token}`, }, }); const accountInfo = response.data.accounts[0]; console.log('accountInfo', accountInfo) return { name: accountInfo.display_name, nickname: accountInfo.acct, description: accountInfo.note, instance, username, }; } catch (error) { console.log('error', error) console.error(`Erreur lors de la récupération des informations du compte ${nickname}: ${error.message}`); return null; } })); // Fabriquer le message let message = 'Suivez ces comptes Mastodon: \n\n'; accountsInfo.forEach((accountInfo, index) => { if (accountInfo) { const description = accountInfo.description; let truncatedDescription = '' if (description && description.length) { truncatedDescription = description.substring(0, 147); } message += `* **${accountInfo.name}** (@${accountInfo.nickname}) : ${accountInfo.description}. https://${accountInfo.instance}/${accountInfo.username}\n`; } }); message += ' #followFriday #curation' return sanitize(message); } async function main() { let post = await recommendRandomAccount(); console.log('post', post); } function sanitize(text) { const sanitizedText = text.replace(/<\/?[^>]+(>|$)/g, ''); const sanitizedTextWithoutEntities = sanitizedText.replace(/&#(\d+);/g, (match, code) => { return String.fromCharCode(code); }); return sanitizedTextWithoutEntities } main();