如何获取超过 25 条帖子

发布于 2024-12-12 09:38:04 字数 641 浏览 0 评论 0原文

我正在尝试使用restfb获取所有帖子消息,我的代码如下

public Connection<Post> publicSearchMessages(Date fromDate, Date toDate) {
    Connection<Post> messages = publicFbClient.fetchConnection("search",
            Post.class,
            Parameter.with("q", "Watermelon"),
            Parameter.with("since", fromDate),
            Parameter.with("until", toDate),
            Parameter.with("type", "post"));

    return messages;
}

这仅提供最新的25条帖子消息。

Parameter.with("limit",100)

如果我设置 limit 参数,它会给出 100 条消息,但我不想限制为获取帖子消息。那么,

我是否可以在不设置限制参数的情况下获得符合搜索条件的帖子消息的完整列表?

I'm trying to get all post messages using restfb, my code is as follows

public Connection<Post> publicSearchMessages(Date fromDate, Date toDate) {
    Connection<Post> messages = publicFbClient.fetchConnection("search",
            Post.class,
            Parameter.with("q", "Watermelon"),
            Parameter.with("since", fromDate),
            Parameter.with("until", toDate),
            Parameter.with("type", "post"));

    return messages;
}

This only gives latest 25 post messages.

Parameter.with("limit",100 )

If i set limit parameter, it gives 100 messages but i don't want to limit to fetching post messages. So,

Is there anyway I can get a full list of post messages matching the search criteria without setting limit parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九厘米的零° 2024-12-19 09:38:04

也许你可以尝试使用循环。 FB 每次无法获取超过 1000 个,因此您可以使用循环来获取整个 feed。像这样使用偏移量:

Parameter.with("limit", 1000));
Parameter.with("offset", offset));

Offset 将是一个变量,其值将是 1000,2000,3000...

Maybe you can try using a loop. FB can't get more than 1000 each time, so you can use the loop to get the whole feeds. Use the offset like this:

Parameter.with("limit", 1000));
Parameter.with("offset", offset));

Offset will be a variable and its value will be 1000,2000,3000...

山川志 2024-12-19 09:38:04

无法从 FB 获取无限的结果。默认限制设置为 25。如您所知,您可以使用 limit 参数更改此设置。我还没有找到限制网络搜索的上限。也许,您可以将其设置为非常高的金额。

There is no way to fetch unlimited results from FB. The default limit is set to 25. As you know, you can change this using the limit parameter. I have not found an upper border for limit searching the web. Maybe, you can set it to a very high amount.

意中人 2024-12-19 09:38:04

正如我最近测试的那样,您不必指定任何内容。 Connection 类以这种方式实现 Iterable:

  • 获取 25 个结果
  • hasNext 检查是否有下一个项目要处理,
  • 如果没有,它将获取下一页 25 个结果

所以基本上你需要做的就是这样:

Connection<Post> messages = publicFbClient.fetchConnection("search",
        Post.class,
        Parameter.with("q", "Watermelon"),
        Parameter.with("since", fromDate),
        Parameter.with("until", toDate),
        Parameter.with("type", "post"));

for (List<Post> feedConnectionPage : messages) {
        for (Post post : myFeedConnectionPage) {
                 // do stuff with post
        }
}

如果你想要某种方法返回结果我会非常小心,因为您可能会返回数千个结果,并且爬行它们可能需要一些时间(从几秒到几分钟甚至几小时),并且结果对象数组将非常大。更好的想法是使用一些异步调用并定期检查方法的结果。

尽管似乎参数“since”被忽略了。帖子是从最新到最旧的顺序获取的,我认为它在进行分页时以某种方式遗漏了这个参数。

希望我对你说得更清楚:)

As I tested recently, You don't have to specify anything. Connection class implements Iterable in this way:

  • fetch 25 results
  • hasNext check if there is next item to process
  • if not, it will fetch next page of 25 results

So basically all you need to do is this:

Connection<Post> messages = publicFbClient.fetchConnection("search",
        Post.class,
        Parameter.with("q", "Watermelon"),
        Parameter.with("since", fromDate),
        Parameter.with("until", toDate),
        Parameter.with("type", "post"));

for (List<Post> feedConnectionPage : messages) {
        for (Post post : myFeedConnectionPage) {
                 // do stuff with post
        }
}

If you want to have some kind of method that returns results I would be very careful, because you can be returning thousands of results and crawling through them may take some time (from seconds to minutes or even hours) and result object array is going to be really big. Better idea is to use some asynchronous call and check out results of method periodically.

Though it seems that parameter "since" is ignored. Posts are fetched from newest to oldest and I think that it somehow leave out this parameter when doing paging.

Hope I made it more clear for you :)

眼睛会笑 2024-12-19 09:38:04

我们在 Post 中有一个 Iterator 对象。所以我们可以这样做:

Connection<Post> messages = publicFbClient.fetchConnection(...) ;
someMethodUsingPage(messages);
    while (messages.hasNext()) {
        messages = facebookClient.fetchConnectionPage(messages.getNextPageUrl(), Post.class);
        someMethodUsingPage(messages);
    }

然后在每条消息中我们将有接下来的 25 条消息。

We have an Iterator object in Post. So we can do it like this:

Connection<Post> messages = publicFbClient.fetchConnection(...) ;
someMethodUsingPage(messages);
    while (messages.hasNext()) {
        messages = facebookClient.fetchConnectionPage(messages.getNextPageUrl(), Post.class);
        someMethodUsingPage(messages);
    }

Then in each messages we'll have next 25 messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文