Search code examples
ruby-on-railsrubyinstagraminstagram-api

Why is Instagram Likes Count incorrect?


I'm using the following code to determine the total number of posts that an instagram user has liked. This code worked locally on a small number of likes (66 total). It iterated through pages of 20 each until it reached 66.

However, I have some users that have upwards of 300 likes. For some reason one day the code returns 308 likes for the user, the next day it only returns 294. I've checked with the user and they have not de-liked any posts on instagram. Why would the count be going down?:

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

Solution

  • It usually happens if the photo you liked has been deleted by the user who posted it.