Created
February 19, 2024 15:19
-
-
Save gr36/bbddf18a17ce903e8b2dc321d96e1801 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const axios = require('axios'); | |
// Function to fetch all posts of a user | |
async function fetchUserPosts(instanceURL, accessToken, username) { | |
try { | |
const response = await axios.get(`${instanceURL}/api/v1/accounts/${username}/statuses`, { | |
headers: { | |
Authorization: `Bearer ${accessToken}`, | |
}, | |
}); | |
// Check if user exists and has posts | |
if (response.data && response.data.length > 0) { | |
// Return necessary data for each post | |
return response.data.map(post => ({ | |
id: post.id, | |
content: post.content, | |
created_at: post.created_at, | |
replies_count: post.replies_count, | |
reblogs_count: post.reblogs_count, | |
favourites_count: post.favourites_count, | |
link: post.url, // Assuming the URL field provides the link to the post | |
// Add more fields as needed | |
})); | |
} else { | |
throw new Error(`No posts found for user '${username}'.`); | |
} | |
} catch (error) { | |
console.error('Error fetching user posts:', error.response ? error.response.data : error.message); | |
return []; | |
} | |
} | |
// Main function to fetch posts of a user and update the JSON file | |
async function fetchAndUpdateUserPosts(instanceURL, accessToken, username) { | |
try { | |
const fetchedPosts = await fetchUserPosts(instanceURL, accessToken, username); | |
const filePath = `./_cache/mastodon_posts.json`; | |
// Check if the file exists | |
if (fs.existsSync(filePath)) { | |
// Read existing posts from file | |
const existingPosts = JSON.parse(fs.readFileSync(filePath, 'utf-8')); | |
// Update or add new posts | |
fetchedPosts.forEach(newPost => { | |
const index = existingPosts.findIndex(oldPost => oldPost.id === newPost.id); | |
if (index !== -1) { | |
// Update existing post | |
existingPosts[index] = newPost; | |
} else { | |
// Add new post | |
existingPosts.push(newPost); | |
} | |
}); | |
// Write updated posts back to the file | |
fs.writeFileSync(filePath, JSON.stringify(existingPosts, null, 2)); | |
console.log(`Posts for user ${username} fetched and updated successfully.`); | |
} else { | |
// Write fetched posts to a new file | |
fs.writeFileSync(filePath, JSON.stringify(fetchedPosts, null, 2)); | |
console.log(`Posts for user ${username} fetched and saved successfully.`); | |
} | |
} catch (error) { | |
console.error('Error:', error); | |
} | |
} | |
// Define instance URL, access token, and username | |
const instanceURL = 'https://social.lol'; // replace with your Mastodon instance URL | |
const accessToken = process.env.MASTODON_ACCESS_TOKEN; // replace with your Mastodon access token | |
const username = 'USER_ID'; // specify the username for which you want to fetch posts | |
// Call the function to fetch and update posts for the specified user | |
fetchAndUpdateUserPosts(instanceURL, accessToken, username); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment