Getting NEW posts from a subreddit in JSON
Asked Answered
R

2

27

How would I go about getting the new posts of a subreddit in JSON? Just tacking on .json to the url (http://www.reddit.com/r/SOME_SUBREDDIT/new.json) returns the following:

{
    kind: "Listing"
    -
    data: {
        modhash: ""
        children: [ ]
        after: null
        before: null
    }
}

The children array doesn't contain any posts. I've come to find that http://www.reddit.com/r/SOME_SUBREDDIT/new actually routes to new?sort=rising when what I need is new?sort=new.

and /new?sort=new.json of course wont work.

Reveille answered 31/5, 2011 at 5:23 Comment(2)
How we get the list of subscribe subreddits using reddit api in android?Klockau
How to call this link reddit.com/subreddits/mine in androidKlockau
C
75

The .json modifier should be put at the end of the path component, not the entire URL. The URL you're looking for is:

http://www.reddit.com/r/subreddit/new.json?sort=new
Corfu answered 31/5, 2011 at 5:57 Comment(6)
Where Do i look for a list of all available json modifiers for Reddit ?Alternative
@NeilWilliams Can I ask where you saw that? While it works, as far as I know the docs say all calls require OAuth, and nowhere do I see this particular usage documented?Fasciation
We'd definitely recommend and prefer you use OAuth these days. The URL structure described above is no different on OAuth though.Corfu
You can also add &limit=100 to get some more posts, but that's the limit of the limitKilby
How to get list of subreddits/mysubreddits subscribe by user ?Klockau
@Kilby +1 for "the limit of the limit"Plurality
R
9

If you are interested in getting a realtime stream of all of the Reddit new post, Pusher has an unofficial Realtime Reddit API. You can read more about it on this blog post http://blog.pusher.com/pusher-realtime-reddit-api/.

But it basically you do nifty things like. It's also available in Ruby, Python, PHP, etc.

<!-- Include the Pusher JavaScript library -->
<script src="http://js.pusher.com/2.2/pusher.min.js"></script>

<script>
  // Open a Pusher connection to the Realtime Reddit API
  var pusher = new Pusher("50ed18dd967b455393ed");

  // Subscribe to the /r/AskReddit subreddit (lowercase)
  var subredditChannel = pusher.subscribe("askreddit");

  // Listen for new stories
  subredditChannel.bind("new-listing", function(listing) {
    // Output listing to the browser console
    console.log(listing);
  };
</script>

Disclaimer: I work for Pusher

Remediosremedy answered 12/8, 2014 at 12:8 Comment(4)
Apparently I was trying this ( got from Pusher blog ) , I can see it's executing subredditChannel.bind('pusher:subscription_succeeded', function(data) { console.log("Subscribed"); }); But after that no return from "subredditChannel.bind("new-listing",callback)". Why so ? can you help me with this ?Redtop
How to get list of subreddits/mysubreddits subscribe by user ?Klockau
This seems not to work anymore. After fixing the syntax error in the code, I see no errors and I see it doing what seems like long polling. All of the polls come back with 204 No Content, though, while various new posts were submitted to askreddit. The console.log(listing) is never triggered.Myrilla
Yea. Seems like this public channel is down now. You have to create your own backend for this to work.Prosciutto

© 2022 - 2024 — McMap. All rights reserved.