Publish a Youtube feed to Lemmy

This allows you to automatically publish a Youtube channel to to a Lemmy community.

Example usage:

async function publishMyFeed() {
  try {
    return @pdebie.publishYoutubeToLemmy({
      instance: "lemm.ee",
      auth: @me.secrets.lemmee,
      communityId: 23316,
      youtubeChannelId: "UChZgikssAJkmiQOv_sO-ngQ",
      lastSyncTime: @pdebie.spacexLemmyDb.lastSync,
    });
  }
  finally {
    @pdebie.spacexLemmyDb.lastSync = new Date().toISOString();
  }
}

Get a Youtube channel ID here.

Make sure to set your lastSync properly, since otherwise you'll keep publishing new posts!

Readme
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;
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.