2

我試圖在某個時間段後獲得facebook頁面上的所有連接(交互)。我使用考拉寶石和過濾請求與「自:1.month.ago.to_i」似乎工作正常。但是,這一次給了我25個結果。如果我將限制更改爲446(看起來最大),那會更好。但是,如果我使用.next_page在給定的時間範圍內給我下一組結果,它只會給我下一組結果而不遵守時間範圍。如何獲得自一定時間段以來的所有連接

例如,假設我不增加限制,並且每個請求有25個結果。我做類似的事情:

@api.get_connections(@fan_page_id, "feed", {since: 1.month.ago.to_i}) 

我們假設有30個結果,第一個請求得到了25(默認限制)。那麼,如果我這樣做:

@api.get_connections(@fan_page_id, "feed", {since: 1.month.ago.to_i}).next_page 

,而不是返回的最後5個結果,它返回25個,其中20個都沒有「因爲:1.month.ago.to_i」。我有一個while循環遍歷頁面,但我不知道該停止在哪裏,因爲只要我繼續調用.next_page,它就會不斷返回結果給我。

  1. 有沒有更好的方法來做到這一點?
  2. 如果沒有,檢查以確保我在循環中查看的帖子的最佳方式仍然在我想要的時間範圍內,如果不是,則會突然出現?

這裏是我的代碼:

def perform(fan_page_id, pagination_options = {}) 
    @since_date = pagination_options[:since_date] if pagination_options[:since_date] 
    @limit = pagination_options[:limit] if pagination_options[:limit] 

    @oauth = Koala::Facebook::OAuth.new 
    @api = Koala::Facebook::API.new @oauth.get_app_access_token 

    fb_page = @api.get_object(fan_page_id) 
    @fan_page_id = fb_page["id"] 

    # Collect all the users who liked, commented, or liked *and* commented on a post 
    process_posts(@api.get_connections(@fan_page_id, "feed", {since: @since_date})) do |post| 

     ## do stuff based on each post 

    end 
    end 


    private 
    # Take each post from the specified feed and perform the provided 
    # code on each post in that feed. 
    # 
    # @param [Koala::Facebook::API::GraphCollection] feed An API response containing a page's feed 
    def process_posts(feed, options = {}) 
     raise ArgumentError unless block_given? 

     current_feed = feed 

     begin 

     current_feed.each { |post| yield(post) } 
     current_feed = current_feed.next_page 

     end while current_feed.any? 
    end 

回答

0
current = @api.get_connections(@fan_page_id, "feed", {since: 1.month.ago.to_i}) 

next = current.next_page 

next = next.next_page 

..... 

請嘗試這些,我認爲他們的工作。

相關問題