some changes
This commit is contained in:
parent
3efbd21985
commit
0231851726
1 changed files with 166 additions and 158 deletions
90
main.js
90
main.js
|
@ -8,43 +8,45 @@ const axios = require("axios");
|
||||||
const mastodonInstance = process.env.MASTODON_INSTANCE;
|
const mastodonInstance = process.env.MASTODON_INSTANCE;
|
||||||
const mastodonUser = process.env.MASTODON_USER;
|
const mastodonUser = process.env.MASTODON_USER;
|
||||||
|
|
||||||
// Bluesky agent
|
async function main() {
|
||||||
const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT });
|
// Bluesky agent
|
||||||
await agent.login({
|
const agent = new BskyAgent({ service: process.env.BLUESKY_ENDPOINT });
|
||||||
|
const loginResponse = await agent.login({
|
||||||
identifier: process.env.BLUESKY_HANDLE,
|
identifier: process.env.BLUESKY_HANDLE,
|
||||||
password: process.env.BLUESKY_PASSWORD,
|
password: process.env.BLUESKY_PASSWORD,
|
||||||
});
|
});
|
||||||
|
if (!loginResponse.success) console.error("🔒 login failed");
|
||||||
|
|
||||||
// File to store the last processed Mastodon post ID
|
// File to store the last processed Mastodon post ID
|
||||||
const lastProcessedPostIdFile = path.join(
|
const lastProcessedPostIdFile = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
"data",
|
"data",
|
||||||
"lastProcessedPostId.txt"
|
"lastProcessedPostId.txt"
|
||||||
);
|
);
|
||||||
|
|
||||||
// Variable to store the last processed Mastodon post ID
|
// Variable to store the last processed Mastodon post ID
|
||||||
let lastProcessedPostId = loadLastProcessedPostId();
|
let lastProcessedPostId = loadLastProcessedPostId();
|
||||||
|
|
||||||
// Function to load the last processed post ID from the file
|
// Function to load the last processed post ID from the file
|
||||||
function loadLastProcessedPostId() {
|
function loadLastProcessedPostId() {
|
||||||
try {
|
try {
|
||||||
return fs.readFileSync(lastProcessedPostIdFile, "utf8").trim();
|
return fs.readFileSync(lastProcessedPostIdFile, "utf8").trim();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error loading last processed post ID:", error);
|
console.error("Error loading last processed post ID:", error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to save the last processed post ID to the file
|
// Function to save the last processed post ID to the file
|
||||||
function saveLastProcessedPostId() {
|
function saveLastProcessedPostId() {
|
||||||
try {
|
try {
|
||||||
fs.writeFileSync(lastProcessedPostIdFile, `${lastProcessedPostId}`);
|
fs.writeFileSync(lastProcessedPostIdFile, `${lastProcessedPostId}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error saving last processed post ID:", error);
|
console.error("Error saving last processed post ID:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createBlueskyMessage(text) {
|
async function createBlueskyMessage(text) {
|
||||||
const richText = new RichText({ text });
|
const richText = new RichText({ text });
|
||||||
await richText.detectFacets(agent);
|
await richText.detectFacets(agent);
|
||||||
|
|
||||||
|
@ -52,26 +54,28 @@ async function createBlueskyMessage(text) {
|
||||||
text: richText.text,
|
text: richText.text,
|
||||||
facets: richText.facets
|
facets: richText.facets
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function postToBluesky(textParts, images) {
|
async function postToBluesky(textParts, images) {
|
||||||
const blueSkyImages = await images.reduce(async (list, {url, alt}) => {
|
const blueSkyImages = []
|
||||||
const imageResponse = await axios.get({
|
/*const blueSkyImages = await images.reduce(async (list, {url, alt}) => {
|
||||||
url,
|
const {data: imageData} = await axios.get(url, {
|
||||||
method: 'GET',
|
responseType: 'arraybuffer',
|
||||||
responseType: 'blob',
|
|
||||||
});
|
});
|
||||||
|
//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 [
|
return [
|
||||||
...list,
|
...list,
|
||||||
{
|
{
|
||||||
alt,
|
alt,
|
||||||
image: uploadResponse.blob,
|
image: data.blob,
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}, []);
|
}, []);*/
|
||||||
|
|
||||||
const rootMessageResponse = await agent.post({
|
const rootMessageResponse = await agent.post({
|
||||||
...(await createBlueskyMessage(textParts[0])),
|
...(await createBlueskyMessage(textParts[0])),
|
||||||
|
@ -98,13 +102,13 @@ async function postToBluesky(textParts, images) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeHtmlTags(input) {
|
function removeHtmlTags(input) {
|
||||||
return input.replace(/<[^>]*>/g, "");
|
return input.replace(/<[^>]*>/g, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
function splitText(text, maxLength) {
|
function splitText(text, maxLength) {
|
||||||
// Split the text by spaces
|
// Split the text by spaces
|
||||||
const words = text.split(" ");
|
const words = text.split(" ");
|
||||||
|
|
||||||
|
@ -129,10 +133,10 @@ function splitText(text, maxLength) {
|
||||||
result.push(currentChunk);
|
result.push(currentChunk);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to periodically fetch new Mastodon posts
|
// Function to periodically fetch new Mastodon posts
|
||||||
async function fetchNewPosts() {
|
async function fetchNewPosts() {
|
||||||
const response = await axios.get(
|
const response = await axios.get(
|
||||||
`${mastodonInstance}/users/${mastodonUser}/outbox?page=true`
|
`${mastodonInstance}/users/${mastodonUser}/outbox?page=true`
|
||||||
);
|
);
|
||||||
|
@ -147,19 +151,20 @@ async function fetchNewPosts() {
|
||||||
reversed.forEach((item) => {
|
reversed.forEach((item) => {
|
||||||
const currentTimestampId = Date.parse(item.published);
|
const currentTimestampId = Date.parse(item.published);
|
||||||
|
|
||||||
|
if (currentTimestampId > newTimestampId) {
|
||||||
|
newTimestampId = currentTimestampId;
|
||||||
|
}
|
||||||
|
|
||||||
if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) {
|
if (currentTimestampId > lastProcessedPostId && lastProcessedPostId != 0) {
|
||||||
try {
|
try {
|
||||||
console.log('📧 posting to BlueSky', currentTimestampId)
|
console.log('📧 posting to BlueSky', currentTimestampId)
|
||||||
const textParts = splitText(removeHtmlTags(item.object.content), 300);
|
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/'))
|
.filter(attachment => attachment.type === 'Document' && attachment.mediaType.startsWith('image/'))
|
||||||
.map(({name: alt, url}) => ({ alt, url }));
|
.map(({name: alt, url}) => ({ alt, url }));
|
||||||
postToBluesky(textParts, images);
|
|
||||||
|
|
||||||
if (currentTimestampId > newTimestampId) {
|
postToBluesky(textParts, images);
|
||||||
newTimestampId = currentTimestampId;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('🔥 can\'t post to Bluesky', currentTimestampId, error)
|
console.error('🔥 can\'t post to Bluesky', currentTimestampId, error)
|
||||||
}
|
}
|
||||||
|
@ -170,8 +175,11 @@ async function fetchNewPosts() {
|
||||||
lastProcessedPostId = newTimestampId;
|
lastProcessedPostId = newTimestampId;
|
||||||
saveLastProcessedPostId();
|
saveLastProcessedPostId();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchNewPosts();
|
||||||
|
// Fetch new posts every 5 minutes (adjust as needed)
|
||||||
|
setInterval(fetchNewPosts, (process.env.INTERVAL_MINUTES ?? 5) * 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchNewPosts();
|
main()
|
||||||
// Fetch new posts every 5 minutes (adjust as needed)
|
|
||||||
setInterval(fetchNewPosts, (process.env.INTERVAL_MINUTES ?? 5) * 60 * 1000);
|
|
Loading…
Reference in a new issue