Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
kensand committed Mar 9, 2024
2 parents 91138ae + 3d30f15 commit baf58d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ If running locally, ensure `NodeJS >= 20` is installed.

Otherwise, docker and docker-compose may be desired.

After creating your Lemmy bot account on the server, go into the settings for the user and check
the "bot account" checkbox.

### Configuration

Copy `config.template.json` to `config.json` and configure it.
Expand Down
6 changes: 6 additions & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { mkFeedTask } from "./util";
const configPath = process.env["CONFIG_PATH"] ?? "./config.json";
const startTimeEnv = process.env["START_TIME"];
const startTime = startTimeEnv ? new Date(startTimeEnv) : new Date();
if (startTimeEnv) {
console.log("START_TIME found and translated to:", startTime.toString());
}
else {
console.log("START_TIME not set. Starting tracking at current date:", startTime.toString());
}

const config: Config = JSON.parse(fs.readFileSync(configPath).toString());
const defaultSchedule = config.defaultSchedule ?? { hours: 1 };
Expand Down
54 changes: 34 additions & 20 deletions lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,40 @@ export async function postItem(
title: string,
nsfw: boolean | undefined
) {
const communityId = (await client.getCommunity({ name: c.communityName }))
.community_view.community.id;
const link = item.link ? `${urlPrefix ?? ""}${item.link}` : undefined;
const titleLink = item.title
? link
? `# [${item.title}](${link})\n`
: `# ${item.title}\n`
: "";
await client.createPost({
community_id: communityId,
url: link,
name: title,
nsfw: nsfw,
body: `${titleLink}${NodeHtmlMarkdown.translate(
item.content ?? item.contentSnippet ?? item.summary ?? "",
{},
undefined,
undefined
)}`,
});
try {
const communityId = (await client.getCommunity({ name: c.communityName }))
.community_view.community.id;
const link = item.link ? `${urlPrefix ?? ""}${item.link}` : undefined;
const titleLink = item.title
? link
? `# [${item.title}](${link})\n`
: `# ${item.title}\n`
: "";

if (title.length > 200) {
title = title.slice(0, 197) + '...';
}
const postResponse = await client.createPost({
community_id: communityId,
url: link,
name: title,
nsfw: nsfw,
body: `${titleLink}${NodeHtmlMarkdown.translate(
item.content ?? item.contentSnippet ?? item.summary ?? "",
{},
undefined,
undefined
)}`,
});
if (!postResponse.post_view.post.ap_id) {
throw new Error(`Failed to post ${postResponse}`);
}
return
}
catch (error) {
console.log(`An error occurred during the post creation process.
It is uncertain whether the post was successfully created. ${error}`);
}
}

export function mkFeedTask(
Expand Down

0 comments on commit baf58d8

Please sign in to comment.