npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

snoots-stream

v2.1.3

Published

Comment and post stream with snoots library

Downloads

4

Readme

Snoots Stream is an event-based wrapper for Snoots, following much of the design decisions of Snoostorm. It handles polling certain Snoots functions for you to make designing Reddit bots and applications easier.

Install

You can install Snoots Stream from NPM using the command below:

npm install snoots-stream

Usage

Let's get started with a simple example

import { Client } from 'snoots';

import { skipDuplicates, skipExisting } from './middlewares';

import { CommentStream } from '.';

// Instantiate a Snoots client (or re-use one)
const client = new Client({...})

// Create a comment stream that fetches comments from r/AskReddit ever 5000ms
const comments = new CommentStream( client, { frequency: 5000 }, 'AskReddit' );

// Add middleware (more on that later)
comments.use(new skipExisting(), new skipDuplicates('id'));

// For every comment, log it to the console
comments.on('item', console.log)

The above example makes use of the CommentStream class, but the same could have been done for SubmissionStream. When more features become available in the snoots library, more streams will be added (such as modmail, modlog, reported content, and so on).

Custom polls

If you want to implement new polling classes, it is relatively simple. The SubmissionStream class is shown below as an example

import { Client, Post } from 'snoots';

import { IAsyncPollOptions } from '../poll/asyncPoll';
import { SnootsListingPoll } from '../poll/snootsListingPoll';

// Ensure that the class correctly extends SnootsListingPoll, with the type of object that the listing returns
export class SubmissionStream extends SnootsListingPoll<Post> {
	constructor(client: Client, options: IAsyncPollOptions, subreddit: string) {
		// ---v--- This is the import line
		super(options, () => client.subreddits.getNewPosts(subreddit));
	}
}

As you can see, all it took was passing a function that calls client.subreddits.getNewPosts, and the rest of the work is done behind the scenes.

Middleware

A limitation of the SnooStorm library is its rigidity in filtering content that is returned. By default, it discards content that has already been processed (which is the correct thing to do 99% of the time), and leaves any further processing such as discarding already-existing content to the end developer.

By implementing a middleware paradigm, Snoots-Stream can cover all use-cases while still being plug-and-play. Suppose you want to discard comments that don't pass a Regex test? All you'd need to do is pass that middleware in

import {matchRegex} from './middlewares'

// Create comment stream, as above
const const comments = ...

// Add middleware
comments.use(matchRegex('body', /^[1-9]\d{0,2}$/g))

// Now we are only logging comments that match the regex
comments.on('item', console.log)

Custom middleware is equally easy to create, and can take one of three forms: middleware, middleware generator function, or middleware class. The regex example is a middleware generator function, and the above discardExisting is a middleware class.

Example middleware that logs whatever it sees (any middleware above it might stop a comment from being logged, though!)

export function loggingMiddleware<T>(
	context: T,
	next: Next
): boolean | Promise<boolean> {
	console.log(context);
	return next();
}
comments.use(loggingMiddleware);

or as a class (not necessary in this case, but just as an example)

export class loggingMiddleware<T extends Content> extends MiddlewareClass<T> {
	method(context: T, next: Next): boolean | Promise<boolean> {
		console.log(context);
		return next();
	}
}
comments.use(new loggingMiddleware());

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the GPL-3.0-only License. See LICENSE.md for more information.