some changes

This commit is contained in:
René Schimmelpfennig 2024-04-17 19:18:17 +02:00
parent 3efbd21985
commit 0231851726
No known key found for this signature in database
GPG key ID: EABA8F8CCF842F0B

36
main.js
View file

@ -8,12 +8,14 @@ const axios = require("axios");
const mastodonInstance = process.env.MASTODON_INSTANCE;
const mastodonUser = process.env.MASTODON_USER;
async function main() {
// Bluesky agent
const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT });
await agent.login({
const loginResponse = await agent.login({
identifier: process.env.BLUESKY_HANDLE,
password: process.env.BLUESKY_PASSWORD,
});
if (!loginResponse.success) console.error("🔒 login failed");
// File to store the last processed Mastodon post ID
const lastProcessedPostIdFile = path.join(
@ -55,23 +57,25 @@ async function createBlueskyMessage(text) {
}
async function postToBluesky(textParts, images) {
const blueSkyImages = await images.reduce(async (list, {url, alt}) => {
const imageResponse = await axios.get({
url,
method: 'GET',
responseType: 'blob',
const blueSkyImages = []
/*const blueSkyImages = await images.reduce(async (list, {url, alt}) => {
const {data: imageData} = await axios.get(url, {
responseType: 'arraybuffer',
});
//const base64Data = Buffer.from(imageData, 'binary').toString('base64')
console.log('🐈', url)
const uploadResponse = await agent.uploadBlob(imageResponse.data);
// new Uint8Array(imageData), {encoding: 'utf8'}
const {data} = await agent.uploadBlob(url);
return [
...list,
{
alt,
image: uploadResponse.blob,
image: data.blob,
}
];
}, []);
}, []);*/
const rootMessageResponse = await agent.post({
...(await createBlueskyMessage(textParts[0])),
@ -147,19 +151,20 @@ async function fetchNewPosts() {
reversed.forEach((item) => {
const currentTimestampId = Date.parse(item.published);
if (currentTimestampId > newTimestampId) {
newTimestampId = currentTimestampId;
}
if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) {
try {
console.log('📧 posting to BlueSky', currentTimestampId)
const textParts = splitText(removeHtmlTags(item.object.content), 300);
const images = item.attachment
const images = item.object.attachment
.filter(attachment => attachment.type === 'Document' && attachment.mediaType.startsWith('image/'))
.map(({name: alt, url}) => ({ alt, url }));
postToBluesky(textParts, images);
if (currentTimestampId > newTimestampId) {
newTimestampId = currentTimestampId;
}
postToBluesky(textParts, images);
} catch (error) {
console.error('🔥 can\'t post to Bluesky', currentTimestampId, error)
}
@ -175,3 +180,6 @@ async function fetchNewPosts() {
fetchNewPosts();
// Fetch new posts every 5 minutes (adjust as needed)
setInterval(fetchNewPosts, (process.env.INTERVAL_MINUTES ?? 5) * 60 * 1000);
}
main()