How to create an Instagram API client in python: a step-by-step tutorial - part 2

Awesome Image Awesome Image
How to Create an Instagram API Client in Python a Step-by-Step Tutorial

How to analyze Instagram likes - part 2

Hi to you!👋

In the previous post – How to analyze Instagram likes: a simple python app, we set up the runtime environment for working with RapidAPI in Python. We also wrote a simple client that communicates with the Instagram Scraper 2023 API hosted on RapidAPI. The tests went well, so today we will be finishing the client.

Our Goal

To remind you of our goal: We want to create a full-fledged Python application using Instagram data that will:

  • Tell us who is liking the posts
  • Calculate the percentage of followers who like the posts

What value will this application provide?

It will help determine whether the account owner should focus more on encouraging users to follow the profile, getting more likes, or improving the content.

Let’s get to work! 🏋️‍♂️

Necessary Endpoints

To achieve our goal, we need to know exactly what data we need to conduct our analysis. Therefore, we need:

  • To get a listing of posts by user ID
  • To get a list of people liking a particular post
  • To get a list of people following a user

In our application, we use the Instagram Scraper 2023 API, which provides the necessary data without any issues. We will use the following endpoints:

  • User Posts – returns a listing of posts by user ID
  • Post Likes – returns a listing of people liking a particular post
  • User Followers – returns a listing of people following a user

Since we have everything selected, it’s time to implement these endpoints in our RapidApiClient class.

Implementing RapidApiClient class

Currently, this class in Python looks like this:

import http
import requests
from config import RAPIDAPI_HOST, RAPIDAPI_KEY

class RapidApiClient:
    def __init__(self):
        self.headers = {
            'x-rapidapi-key': RAPIDAPI_KEY,
            'x-rapidapi-host': RAPIDAPI_HOST
        }

    def get_user_posts(self, userid, count):
        url = self.__get_api_url(f"/userposts/{userid}/{count}/%7Bend_cursor%7D")
        response = requests.get(url, headers=self.headers)
        return response.json()
       
    def __get_api_url(self, path_and_query):
        return f"https://{RAPIDAPI_HOST}{path_and_query}"

As you can see, there’s nothing complicated here. To fetch more posts, we need to add end_cursor support. The operation of end_cursor is very simple. When we fetch the first batch of posts, the response will include an end_cursor value. To get the next batch of posts, we simply pass this cursor in the next request.

Time to modify the get_user_posts function:

def get_user_posts(self, userid, count, end_cursor=None):
    url = self.__get_api_url(f"/userposts/{userid}/{count}/{self.__get_end_cursor(end_cursor)}")
    response = requests.get(url, headers=self.headers)
    return response.json()

We set the end_cursor parameter as default (None) and wrote a simple function __get_end_cursor:

def __get_end_cursor(self, ec):
    if ec is None:
        return "%7Bend_cursor%7D"
    return ec

This function returns the default value if the cursor is None.

Let’s modify main.py to fetch more than 50 posts:

# Initialize the end_cursor to None
end_cursor = None

# Loop to fetch posts, iterating up to 2 times
for i in range(2):
    # Fetch user posts using the API client, specifying user ID, number of posts to fetch, and end cursor for pagination
    posts = api_client.get_user_posts('11579415180', 50, end_cursor)
    
    # Check if there is no next page of posts
    if not posts["data"]["next_page"]:
        # Break the loop if there are no more pages
        break
    
    # Update the end_cursor to the end cursor from the current batch of posts for the next iteration
    end_cursor = posts["data"]["end_cursor"]
    
    # Print the fetched posts
    print(posts)

This code fetches 100 posts. The first time, when cursor_end = None, it fetches the 50 latest posts. Then, in the second loop iteration, it captures cursor_end from the API response and uses it to fetch 50 more posts.

We’re doing quite well!

Let’s write the function to return users liking the post

def get_post_likes(self, shortcode, count, end_cursor=None):
    url = self.__get_api_url(f"/postlikes/{shortcode}/{count}/{self.__get_end_cursor(end_cursor)}")
    response = requests.get(url, headers=self.headers)
    return response.json()

As you can see, the operation is identical to get_user_posts. So, let’s write another function:

def get_user_followers(self, userid, count, end_cursor=None):
    url = self.__get_api_url(f"/userfollowers/{userid}/{count}/{self.__get_end_cursor(end_cursor)}")
    response = requests.get(url, headers=self.headers)
    return response.json()

This function will return the followers of a user.

RapidApiClient Ready for Action!

At this point, we have a complete client for communicating with the API. We can fetch all the data we need. In the next post, we will write the logic to use this data for analysis. Finally, we will be able to advise the analyzed user on how to develop their profile.

See you next time! 🚀

Leave A Comment