snoots

Snoots @ v1.0.0-dev.29

Welcome to the snoots documentation!

Helpful links:


Getting Started

User Bots

To get started making a bot with snoots:

  1. Create an application
  2. Come up with a user agent. See here for more info.
  3. Create a new Client instance.
    const client = new Client({
    userAgent: "<the user agent>",
    creds: {
    clientId: "<the client id>",
    clientSecret: "<the client secret>",
    },
    auth: {
    username: "<the reddit account username>",
    password: "<the reddit account password>",
    },
    });
  4. Use the api! If you need some help, check out the examples.

Crawler / Scraper

If you just want to retrieve information from the Reddit api without controlling a user account you can do that!

  1. Create an application1
  2. Come up with a user agent. See here for more info
  3. Create a new Client instance.
    const client = new Client({
    userAgent: "<the user agent>",
    creds: {
    clientId: "<the client id>",
    clientSecret: "<the client secret>",
    },
    });
  4. Use the api! If you need some help, check out the examples.

1 If you really don't want to make an application you can skip this step and leave off the creds parameter, but note that performing requests without an application means you have a much lower ratelimit.

Examples

Print a comment tree to stdout.

async function printTree(cmt: Comment, indent: string = "") {
const body = cmt.body.replace(/\n/g, "\n" + indent);
console.log(`${indent}(${cmt.id}) ${cmt.author}: ${body}`);
for await (const reply of cmt.replies) {
await printTree(reply, indent + " ");
}
}

(async () => {
const comment = await client.comments.fetch("gqe92yr");
await printTree(comment);
})();

For full project examples check out the examples directory

More examples will come as snoots evolves!

Migrating from Snoowrap

There are some major differences between snoots and snoowrap. Here are some of the largest:

  1. Objects are not lazy loaded.
  2. Promise chaining properties is not allowed.
  3. All parameters are camelCase, not snake_case.
  4. Listings are not arrays, but they can be iterated using for await.
  5. Sub-objects are not auto-populated (like Post and Comment's author). In snoowrap the author field is a user object, but in snoots it's just a string.

What does this mean in practice? Here are some examples:

  1. Reading the title of the newest post from r/funny:

    // snoowrap
    const title = await client.getSubreddit("funny").getNew()[0].title;

    // snoots (literal translation)
    const sub = await client.subreddits.fetch("funny");
    const post = await sub.getNewPosts().first();
    const title = post?.title;

    // snoots (preferred method, 1 fewer api call)
    const post = await client.subreddits.getNewPosts("funny").first();
    const title = post?.title;
  2. Listing the authors of the currently hot posts in r/funny:

    // snoowrap
    const posts = await client.getSubreddit("funny").getHot().fetchAll();
    for (const post of posts) {
    console.log(post.author.name);
    }

    // snoots (literal translation)
    const sub = await client.subreddits.fetch("funny");
    const posts = sub.getHotPosts();
    for await (const post of posts) {
    console.log(p.author);
    }

    // snoots (preferred method, 1 fewer api call)
    const posts = client.subreddits.getHotPosts("funny");
    for await (const post of posts) {
    console.log(p.author);
    }

Generated using TypeDoc