1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import process from "node:process";
import { youtubeFeed } from "https://esm.town/v/pdebie/youtubeFeed";
export async function publishYoutubeToLemmy(
{ instance, communityId, auth, youtubeChannelId, syncInfo, filter }: {
instance: string;
communityId: number;
auth: string;
youtubeChannelId: string;
syncInfo: Record<string, string>;
filter?: (title: string) => boolean;
},
) {
const { LemmyHttp } = await import("npm:lemmy-js-client@0.18.1");
let client = new LemmyHttp(`https://${instance}`, {
fetchFunction: fetch,
});
const items =
(await youtubeFeed(
youtubeChannelId,
syncInfo[youtubeChannelId],
)).filter((i) => filter ? filter(i.title) : true);
const latest = items[0];
syncInfo[youtubeChannelId] = new Date().toISOString();
if (!latest) {
return;
}
const ret = await client.createPost({
community_id: communityId,
name: latest.title,
url: latest.link,
auth: process.env.lemmee,
});
return ret;
}