Skip to content
James-Bennett-295 edited this page Jan 20, 2024 · 10 revisions

Getting a channel ID

You need to use a channel's ID when subscribing to it. Here is how to get the ID of a channel:

  • Open the channel on YouTube
  • Click on the arrow to the right of the channel description
  • Click "Share channel" then "Copy channel ID"

Examples

TypeScript example
import { PollingNotifier, JsonStorage } from "youtube-notifs";

const notifier = new PollingNotifier({
    interval: 15,
    storage: new JsonStorage("youtube-notifs.json")
});

notifier.onNewVideos = (videos) => {
    for (const video of videos) {
        console.dir(video);
    }
}

notifier.subscribe("UCS0N5baNlQWJCUrhCEo8WlA");

notifier.start();
JavaScript example
const { PollingNotifier, JsonStorage } = require("youtube-notifs");

const notifier = new PollingNotifier({
    interval: 15,
    storage: new JsonStorage("youtube-notifs.json")
});

notifier.onNewVideos = (videos) => {
    for (const video of videos) {
        console.dir(video);
    }
}

notifier.subscribe("UCS0N5baNlQWJCUrhCEo8WlA");

notifier.start();

Storage

To allow this package's data to be stored in different ways, such as in memory or in a SQL database, it has the abstract class StorageInterface. This can be extended, providing methods to bulk get/set/delete for named sets of key-value pairs meaning data can be stored in any way.

Some default storage interfaces are provided:

  • JsonStorage: Stores the data in a single JSON file.
  • MemoryStorage: Stores the data in memory so it will be lost when the program ends.
  • DebugStorage: Same as MemoryStorage but logs gets/sets/deletes to the console.

If you would like to store data in a different way, you can create your own storage class.

StorageInterface extension templates:

TypeScript template
import { KeyValPairs, StorageInterface, Store } from "youtube-notifs";

class CustomStorage extends StorageInterface {
    async get(store: Store, keys: string[]): Promise<KeyValPairs> {
        // . . .
    }
    async set(store: Store, pairs: KeyValPairs): Promise<void> {
        // . . .
    }
    async del(store: Store, keys: string[]): Promise<void> {
        // . . .
    }
}
JavaScript template
const { StorageInterface } = require("youtube-notifs");

class CustomStorage extends StorageInterface {
    async get(store, keys) {
        // . . .
    }
    async set(store, pairs) {
        // . . .
    }
    async del(store, keys) {
        // . . .
    }
}

You can then create an instance of your class and provide it to the PollingNotifier, which will use its functions.

Clone this wiki locally