从一个subreddit获取JSON格式的新帖子

27

如何以JSON格式获取一个subreddit的最新帖子?只需在url后面添加.json (http://www.reddit.com/r/SOME_SUBREDDIT/new.json),将返回以下结果:

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

children数组中不包含任何文章。我发现http://www.reddit.com/r/SOME_SUBREDDIT/new实际上路由到new?sort=rising,而我需要的是new?sort=new

/new?sort=new.json当然也行不通。


在Android中,我们如何使用Reddit API获取订阅subreddits列表? - Nancy thakkar
如何在Android中调用此链接https://www.reddit.com/subreddits/mine/ - Nancy thakkar
2个回答

75

.json修饰符应该放在路径组件的末尾,而不是整个URL。你要查找的URL是:

http://www.reddit.com/r/subreddit/new.json?sort=new

2
我在哪里可以找到Reddit所有可用的JSON修改器列表? - Namit Sinha
1
@NeilWilliams,我可以问一下你在哪里看到的吗?虽然它可以工作,但据我所知,文档说所有调用都需要OAuth,并且我没有看到这种特定用法的记录? - Raymond Camden
我们强烈推荐并倾向于您使用OAuth。但是,上述URL结构在OAuth上并没有任何不同。 - Neil Williams
5
你可以添加"&limit=100"以获得更多的帖子,但这是限制的极限。 - 1j01
如何获取用户订阅的子版块/我的子版块列表? - Nancy thakkar
1
@1j01 +1 对于“极限的极限” - steffen

9
如果您有兴趣获取Reddit所有新帖子的实时流,Pusher提供了一个非官方的Realtime Reddit API。您可以在此博客文章http://blog.pusher.com/pusher-realtime-reddit-api/中了解更多信息。但是,基本上您可以做一些很酷的事情。它也可用于Ruby、Python、PHP等语言。
<!-- 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>

免责声明:我为 Pusher 工作


显然我在尝试这个(从Pusher博客获取的代码),我可以看到它正在执行:subredditChannel.bind('pusher:subscription_succeeded', function(data) { console.log("Subscribed"); });但在那之后,“subredditChannel.bind('new-listing',callback)”没有返回。为什么?你能帮忙吗? - Chinmoy
如何获取用户订阅的子版块/我的子版块列表? - Nancy thakkar
这似乎不再起作用了。在修复代码中的语法错误后,我没有看到任何错误,并且它似乎在进行长轮询。然而,所有的轮询都返回204无内容,而各种新帖子被提交到askreddit。console.log(listing)从未被触发。 - Luc
是的,看起来这个公共频道现在已经关闭了。你必须创建自己的后端才能使其工作。 - Telion

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接