- Home
- Description
- How to
- Execute requests
- Users
- Subreddits
- Multireddits
- Comments
- Posts
- Voting
- Searching
- Random
- Queries
- Filters (User)
- Filters (Subreddit)
NRAW.js simplifies the use of Reddit's API with an easy-to-use chainable functions.
Start by installing NRAW.js:
$ npm install nraw
Then require it in your server:
var r = require("nraw");
Now, make a new instance of the r
-object, which you can do by providing a User-agent.
var Reddit = new r("Testbot v0.0.1 by Mobilpadde");
Or you can fill in three parameters, which are a user-agent, a cookie and the modhash of a Reddit user:
var Reddit = new r("Testbot v0.0.1 by Mobilpadde", cookie, modhash);
Executing requests can be done in two ways:
Reddit.user("Mobilpadde").exec(function(data){
// Some super awesome code
})
Or
Reddit.user("Mobilpadde", function(data){
// Some super awesome code
})
In the examples below, the first method will be applied.
Get the 25 latest posts (Links, self-posts or comments) of a given user:
Reddit.user("Mobilpadde").exec(function(data){
// Some super awesome code
})
"That's stupid! Who'd ever need the latest 25 posts of a user?", you say? Well, NRAW.js is smart enough to handle queries too!
Let's make a basic request that gets the top five posts (Links, self-posts and comments) of a user:
Reddit.user("Mobilpadde").sort("top").limit(5).exec(function(data){
// Some super awesome code
})
Pretty cool, 'eh?
"But what if I want to see seven of my liked posts and comments?" - That's super easy too! Simply use the login
-function and the liked
-filter:
Reddit.login(user, pass).user("Mobilpadde").liked().limit(7).exec(function(data){
// Some super awesome code
})
Get the 25 latest posts (Links and self-posts) of a given subreddit:
Reddit.subreddit("CatReactionGifs").exec(function(data){
// Some super awesome code
})
"That's not useful at all! I hate you" - You.
Well, like last time, we have queries that can be applied, but wait! There's more! Subreddits even have the ability to be filtered!
Let's make a request of the 25 most controversial posts from the last year from a given subreddit, but only the ones after the post with the id t3_2k0r3o
:
Reddit.subreddit("CatReactionGifs").controversial().after("t3_2k0r3o").exec(function(data){
// Some super awesome code
})
Or how about we make a request, that finds the 42 top comments of a given subreddit, using the filter comments
:
Reddit.subreddit("CatReactionGifs").comments().top().limit(42).exec(function(data){
// Some super awesome code
})
"Can I get a single post if I know its id?" - Easy peasy!
Reddit.subreddit("CatReactionGifs").post("2zmdf9").exec(function(data){
// Some super awesome code
})
"Whoa, that's pretty awesome, but can I post a link?" - Of course you can! We just need to login and use the post
-function:
Reddit.login(user, pass).subreddit("CatReactionGifs").post().link("How I feel when there's only one pizza slice left", "http://i.imgur.com/CFSwHdq.gif").exec(function(data){
// Some super awesome code
})
"Wow! C-c-can I subscribe to subreddits then?" - Yea you can! Though we'll have to get our hands a bit dirty:
Reddit.subreddit("CatReactionGifs", function(info){
Reddit.login(user, pass).subreddit(info.data.children[0].data.subreddit_id).subscribe(function(data){
// Some super awesome code
})
})
"Awesome! But what if I don't like a subreddit anymore?" - Well, that's a bit tougher! Ha! Gotcha! You should've seen your face! Priceless! Don't worry, it's super easy too:
Reddit.subreddit("DogReactionGifs", function(info){
Reddit.login(user, pass).subreddit(info.data.children[0].data.subreddit_id).unsubscribe(function(data){
// Some super awesome code
})
})
We can even search through a subreddit:
Reddit.subreddit("CatReactionGifs").search("Cat").exec(function(data){
// Some super awesome code
})
"What if I want a random thread from a given subreddit?" - Pure easiness! Simply do:
Reddit.subreddit("cats").random().exec(function(data){
// Some super awesome code
})
You can also get multireddits:
Reddit.user("Mobilpadde").multireddit("kittehs").exec(function(data){
// Some super awesome code
})
How about we play around with some comments for a while?
Let's get all the new comments (Login so we don't have to wait 30 seconds before we can request new comments):
Reddit.login(user, pass).comments().exec(function(data){
// Some super awesome code
})
"Well, now for a tough one! Can I post comments?" - Yes! Yes you can! All you need is an id of the parent (In this case we're gonna use t3_31cvo9
):
Reddit.login(user, pass).post().comment("t3_31cvo9", "I love you!").exec(function(data){
// Some super awesome code
})
"What if I want to see a specific comment and I have its id?" - Well, that's super easy like everything else:
Reddit.subreddit("CatReactionGifs").post("2zmdf9").comment("cpkmvc4").exec(function(data){
// Some super awesome code
})
"I want to delete my comment, please help!" - Alrighty! It's as easy as pie:
Reddit.login(user, pass).comment("t1_cq0ev3j").delete().exec(function(data){
// Some super awesome code
})
You can also delete a post if you misspelled something:
Reddit.login(user, pass).post("t3_31cvo9").delete().exec(function(data){
// Some super awesome code
})
"But I want the related posts of a post to which I know the id" - That's even easier! Take a look at this:
Reddit.post("2v5oi5").related().exec(function(data){
// Some super awesome code
})
Upvoting:
Reddit.login(user, pass).post("t3_2v5oi5").upvote().exec(function(data){
// Some super awesome code
})
Unvoting:
Reddit.login(user, pass).post("t3_2v5oi5").unvote().exec(function(data){
// Some super awesome code
})
Downvoting:
Reddit.login(user, pass).post("t3_2v5oi5").downvote().exec(function(data){
// Some super awesome code
})
Ps. add .comment("t1_coestfz")
, if you want to vote on a comment instead of a post.
You can also search for every thread - in every subreddit - containing the word cat
:
Reddit.search("cat").exec(function(data){
// Some super awesome code
})
How 'bout getting a random thread, you say?
Reddit.random().exec(function(data){
// Some super awesome code
})
- after - postId
- before - postId
- count - 1- 100
- limit - 1 - 100
- from - "hour", "week", "month", "year", "all"
- sort - "hot", "top", "new", "controversial"
- comments
- disliked
- hidden
- liked
- overview
- saved
- submitted
- comments
- controversial
- hot
- gilded
- new
- promoted
- rising
- top