我正在使用以下代码来确定Instagram用户喜欢的帖子总数。此代码在少数喜欢的情况下在本地工作(总共66个)。它遍历每页20页,直到达到66页。
但是,我有一些用户有超过300个喜欢。出于某种原因,有一天代码会为用户返回308个喜欢,第二天它只返回294.我已经与用户核实过并且他们没有取消喜欢Instagram上的任何帖子。为什么计数会下降?:
instagram = Instagram.client(access_token: #######)
total_likes_array = []
liked1 = instagram.user_liked_media
#.user_liked_media is a method native to the Instagram-ruby-gem
total_likes_array.push(liked1.size)
liked_next_max_id = liked1.pagination.next_max_like_id
#.pagination.next_max_like_id is a method native to the Instagram-ruby-gem
while !liked_next_max_id.nil?
liked_next = instagram.user_liked_media(max_like_id: liked_next_max_id)
#max_like_id is a parameter in the Instagram-ruby-gem
total_likes_array.push(liked_next.size)
liked_next_max_id = liked_next.pagination.next_max_like_id
end
total_likes = total_likes_array.sum
答案 0 :(得分:0)
如果您喜欢的照片已被发布的用户删除,通常会发生这种情况。
答案 1 :(得分:0)
我最终做的是这个,效果很好:
authorized = Authorization.find_by_user_id_and_provider(user, 'instagram')
instagram = Instagram.client(access_token: authorized.oauth_token)
user_id = authorized.user_id
current_instagram_user = InstagramUser.find_by_uid(authorized.uid)
user_data = instagram.user
total_likes_array = Array.new
current_max_liked_id = current_instagram_user.max_liked_id
# current_max_liked_id is the max_liked_id from the last DB entry.
if !current_max_liked_id.nil?
liked_recent = instagram.user_liked_media # needs to run until current_max_liked_id
if !liked_recent.empty?
if current_max_liked_id.to_i <= liked_recent.first.id.to_i
liked_first_max_id = liked_recent.first.id
else
liked_first_max_id = current_max_liked_id
end
liked_recent_max_id = liked_first_max_id # this is the highest like id available in the API request.
until current_max_liked_id.to_i >= liked_recent_max_id.to_i
liked_recent.each do |liked|
if current_max_liked_id.to_i < liked.id.to_i
total_likes_array.push(x.id)
elsif current_max_liked_id.to_i >= liked.id.to_i
break
end
end
liked_recent_max_id = liked_recent.pagination.next_max_like_id
liked_recent = instagram.user_liked_media(max_like_id: liked_recent_max_id)
end
num_likes_out = total_likes_array.size + current_instagram_user.int_likes_out
# use .size above because this is taking the total number of liked.id's in the array.
end
else
liked_first = instagram.user_liked_media
if liked_first.empty?
num_likes_out = 0
liked_first_max_id = nil
else
total_likes_array.push(liked_first.size)
liked_first_max_id = liked_first.first.id
liked_next_max_id = liked_first.pagination.next_max_like_id
while !liked_next_max_id.nil?
liked_next = instagram.user_liked_media(max_like_id: liked_next_max_id)
total_likes_array.push(liked_next.size)
liked_next_max_id = liked_next.pagination.next_max_like_id
end
num_likes_out = total_likes_array.sum
# take .sum above because the array shows total number of likes.
end
end
最终,num_likes_out
是输出的喜欢数量。我使用current_max_liked_id
仅检查自上次喜欢的Instagram喜欢,并且我每天在预定的佣金工作中运行此代码。