最新帖子 API 分页

大家好!

我正在通过这个端点从 API 获取最新帖子:https://community.learnaboutgmp.com/posts.json

如何获取更多最新帖子的结果?这可行吗?

我尝试了 https://community.learnaboutgmp.com/posts.json?page=2,但没有生效。

提前感谢!

When I figured that out before I followed the instructions in the “reverse engineering” topic. I thought that I’d done it in GitHub - pfaffman/discourse-downloader: Download a Discourse topic for offline analysis, but I don’t see it there.

You might have to hit /latest.json?page=2 instead of /posts.json and filter out the posts from the result.

https://meta.discourse.org/latest.json?page=2

EDIT: Ohh you want latest posts, not latest topics. Considering /posts by itself doesn’t have a UI, it probably is missing pagination, I’m sure a PR could be created for this. But to get what you want right now you could still parse the /latest.json endpoint and parse out the topic_id and the post number and fetch the post with /t/<topic_id>/<post_number>, but yea I can see why pagination on the /posts endpoint would be helpful.

Okay, now that I’ve looked at the controller for /posts:

def latest
  params.permit(:before)
  ...

there is a “before” parameter, not a “page” parameter, but using it like a “page” is a bit different because it filters out private posts and such. But I think roughly you can use it like this:

  • hit /posts.json and get the first post id (currently it is 22000)
  • subtract 50 (that endpoint only shows at most 50 at a time)
  • hit /posts.json?before=21950
  • repeat

Part of the issue which doesn’t make total sense to me is that we are limiting the sql query to 50 results, but then we do more filtering on it after the initial sql query which is why you will not always get 50 results.

Thanks guys! I’ll play with it.

as the API designer: it works this way to make things easier on the Discourse server; if the server had to do a bunch of work to always fill up 50 posts anything using the API might start being a perf problem.

Basically it forces the consumers to be “good neighbors”, in a weird sense of the term.

关于使用 API 的问题(我通过 discourse_api gem 来调用,太棒了!)——这与分页有关(我也想使用最新帖子端点),但为了当前的示例,我先提供用户相关的部分,因为我是从那里开始工作的。

我想要获取用户的所有活动(主题和回复都可以),所以我尝试了以下方法:

vsoch = client.user_topics_and_replies("vsoch")

但我始终只得到 30 条结果,即使用户显然有更多数据。我尝试修改源代码以添加 page 参数:

# frozen_string_literal: true
module DiscourseApi
  module API
    module UserActions
      def user_replies(username, page=1)
        params = { "username": username, "filter": '5', page: page }
        response = get("/user_actions.json", params)
        response.body["user_actions"]
      end

      def user_topics_and_replies(username, page=1)
        params = { "username": username, "filter": "4,5", page: page }
        response = get("/user_actions.json", params)
        response.body["user_actions"]
      end
    end
  end
end

但这似乎没有任何效果——无论 page 参数设置为何值,返回的始终是相同的 30 条记录。是否有可能获取超过 30 条记录?我是不是方向错了?任何帮助都将不胜感激——如果我能弄清楚如何实现,我很乐意提交一个 PR,让其他人也能受益 :slight_smile: