
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
snoowrap-native
Advanced tools
A fully-featured Node.js wrapper for the reddit API. (Documentation)
get_user()
, and the method to upvote something is just upvote()
.These features ensure that you can write less boilerplate code and focus more on actually doing what you want to do.
snoowrap's methods are designed to be as simple and consistent as possible. So the following expression:
r.get_submission('2np694').body
...will return a Promise. So will this one:
r.get_submission('2np694').author.name
// --> returns a Promise for the string 'DO_U_EVN_SPAGHETTI'
// (this submission's author's name)
The above will return a Promise for the author's name, without having to deal with callback hell or .then
statements. You can chain multiple API calls together:
r.get_submission('2np694').subreddit.get_moderators()[0].name
// --> returns a Promise for the string 'krispykrackers'
// (this submission's subreddit's first mod's name)
...or chain actions together with fluent syntax:
r.get_subreddit('snoowrap')
.submit_selfpost({title: 'Discussion Thread', text: 'Hello! This is a thread'})
.sticky()
.distinguish()
.ignore_reports()
.assign_flair({text: 'Exciting Flair Text', css_class: 'modpost'})
snoowrap works on Node.js 4+, as well as most common browsers.
'use strict';
const snoowrap = require('snoowrap');
/* Create a new snoowrap requester. If you're uncomfortable storing confidential info in your file, one solution is to
simply store it in a json file and require() it. For more information on how to get valid credentials, see here: https://github.com/not-an-aardvark/reddit-oauth-helper */
const r = new snoowrap({
client_id: 'put your client id here',
client_secret: 'put your client secret here',
refresh_token: 'put your refresh token here',
user_agent: 'put your user-agent string here' // for more information, see: https://github.com/reddit/reddit/wiki/API
});
/* That's the entire setup process, now you can just make requests. I would recommend including async functions in your project
by using babel.js (or some equivalent), but this example file uses vanilla Promises for simplicity. */
// Submitting a link to a subreddit
r.get_subreddit('gifs').submit_link({
title: 'Mt. Cameramanjaro',
url: 'https://i.imgur.com/n5iOc72.gifv'
});
// Printing a list of the titles on the front page
r.get_hot().map(post => post.title).then(console.log);
// Extracting every comment on a thread
r.get_submission('4j8p6d').expand_replies({limit: Infinity, depth: Infinity}).then(console.log)
// Automating moderation tasks
r.get_subreddit('some_subreddit_name').get_modqueue({limit: 100}).filter(/some-removal-condition/.test).forEach(flaggedItem => {
flaggedItem.remove();
flaggedItem.subreddit.ban_user(flaggedItem.author);
});
// Automatically creating a stickied thread for a moderated subreddit
r.get_subreddit('some_subreddit_name')
.create_selfpost({title: 'Daily thread', text: 'Discuss things here'})
.sticky()
.distinguish()
.approve()
.assign_flair({text: 'Daily Thread flair text', css_class: 'daily-thread'})
.reply('This is a comment that appears on that daily thread');
// etc. etc.
// Printing the content of a wiki page
r.get_subreddit('AskReddit').get_wiki_page('bestof').content_md.then(console.log);
For more examples of what can be done with snoowrap, take a look at the documentation.
Reddit's live threads are different from most other content, in that messages are distributed through websockets instead of a RESTful API. snoowrap supports this protocol under the hood by representing the content stream as an EventEmitter. For example, the following script will stream all livethread updates to the console as they appear:
r.get_livethread('whrdxo8dg9n0').stream.on('update', console.log);
For more information, see the LiveThread documentation page.
snoowrap uses the Proxy
object introduced in ES6.
If your target environment does not support Proxies, snoowrap will still function. However, method chaining as described above won't work, so your syntax will need to be a bit heavier.
--harmony_proxies
runtime flag to be enabled. e.g. node --harmony_proxies yourFile.js
)Example of how Proxy support affects behavior:
// This works in environments that support Proxies.
// However, it throws a TypeError if Proxies are not available.
r.get_submission('47v7tm').comments[0].upvote();
// ----------
// This is equivalent and works on all platforms, but the syntax isn't as nice.
r.get_submission('47v7tm').fetch().then(submission => {
return submission.comments[0].upvote();
});
npm install snoowrap --save
var snoowrap = require('snoowrap');
See the contributing guide and the getting started page.
This software is freely distributable under the MIT License.
FAQs
A Node.js wrapper for the reddit API
We found that snoowrap-native demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.