Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bluesky): properly catch unsupported media error #110

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MASTODON_ACCESS_TOKEN,
MASTODON_INSTANCE,
SYNC_BLUESKY,
SYNC_DRY_RUN,
SYNC_MASTODON,
TOUITOMAMOUT_VERSION,
TWITTER_HANDLE,
Expand Down Expand Up @@ -48,6 +49,14 @@ export const configuration = async (): Promise<{
});

console.log(`Touitomamout@v${TOUITOMAMOUT_VERSION}`);

if (SYNC_DRY_RUN) {
ora({
color: "gray",
prefixText: oraPrefixer("✌️ dry run"),
}).info("mode enabled (no post will be posted)");
}

// Init configuration
await createCacheFile();
await runMigrations();
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ export const SYNC_PROFILE_NAME =
(process.env.SYNC_PROFILE_NAME || "false") === "true";
export const SYNC_PROFILE_HEADER =
(process.env.SYNC_PROFILE_HEADER || "false") === "true";
export const SYNC_DRY_RUN = (process.env.SYNC_DRY_RUN || "false") === "true";
export const DEBUG = (process.env.TOUITOMAMOUT_DEBUG || "false") === "true";
export const DAEMON = (process.env.DAEMON || "false") === "true";
export const VOID = "∅";
export const API_RATE_LIMIT = parseInt(process.env.API_RATE_LIMIT ?? "10");
export const API_RATE_LIMIT = parseInt(process.env.API_RATE_LIMIT ?? "30");
export const TOUITOMAMOUT_VERSION = buildInfo.version || "0.0.0";
16 changes: 14 additions & 2 deletions src/helpers/medias/upload-bluesky-media.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BskyAgent } from "@atproto/api";

import { DEBUG } from "../../constants.js";
import { parseBlobForBluesky } from "./parse-blob-for-bluesky.js";

/**
Expand All @@ -13,6 +14,17 @@ export const uploadBlueskyMedia = async (
if (!blueskyClient) {
return null;
}
const { mimeType, blobData } = await parseBlobForBluesky(mediaBlob);
return await blueskyClient?.uploadBlob(blobData, { encoding: mimeType });
return await parseBlobForBluesky(mediaBlob)
.then(
({ blobData, mimeType }) =>
blueskyClient?.uploadBlob(blobData, {
encoding: mimeType,
}),
)
.catch((err) => {
if (DEBUG) {
console.log(err);
}
return null;
});
};
21 changes: 18 additions & 3 deletions src/services/bluesky-sender.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AppBskyEmbedRecord, BskyAgent } from "@atproto/api";
import { Ora } from "ora";

import { VOID } from "../constants.js";
import { DEBUG, VOID } from "../constants.js";
import { getCache } from "../helpers/cache/index.js";
import { savePostToCache } from "../helpers/cache/save-post-to-cache.js";
import { parseBlobForBluesky } from "../helpers/medias/parse-blob-for-bluesky.js";
Expand Down Expand Up @@ -45,14 +45,29 @@ export const blueskySenderService = async (
continue;
}

const blueskyBlob = await parseBlobForBluesky(mediaBlob).catch((err) => {
if (DEBUG) {
console.log(err);
}
log.warn(
`medias: ↯ (${mediaAttachments.length + 1}/${
medias.length
}) skipped for ☁️ bluesky : ${err}`,
);
return null;
});

if (!blueskyBlob) {
continue;
}

// Upload
log.text = `medias: ↑ (${mediaAttachments.length + 1}/${
medias.length
}) uploading`;
const { mimeType, blobData } = await parseBlobForBluesky(mediaBlob);

await client
.uploadBlob(blobData, { encoding: mimeType })
.uploadBlob(blueskyBlob.blobData, { encoding: blueskyBlob.mimeType })
.then(async (mediaSent) => {
const m: BlueskyMediaAttachment = { ...mediaSent };
if (media.type === "image" && media.alt_text) {
Expand Down
8 changes: 5 additions & 3 deletions src/services/posts-synchronizer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Scraper } from "@the-convocation/twitter-scraper";
import { mastodon } from "masto";
import ora from "ora";

import { SYNC_DRY_RUN } from "../constants.js";
import { getCache } from "../helpers/cache/index.js";
import { oraPrefixer } from "../helpers/logs/ora-prefixer.js";
import { makePost } from "../helpers/post/make-post.js";
Expand Down Expand Up @@ -42,9 +43,10 @@ export const postsSynchronizerService = async (
log,
);

await mastodonSenderService(mastodonClient, mastodonPost, medias, log);
await blueskySenderService(blueskyClient, blueskyPost, medias, log);

if (!SYNC_DRY_RUN) {
await mastodonSenderService(mastodonClient, mastodonPost, medias, log);
await blueskySenderService(blueskyClient, blueskyPost, medias, log);
}
if (mastodonClient || blueskyPost) {
synchronizedPostsCountThisRun.inc();
}
Expand Down