Get the latest news and tutorials on open source to improve yourself as a hacker.

Post Top Ad

Operating Systems


Post Top Ad

Not owning a smartphone at the moment, I am not able to delete or upload any post in my Instagram account as the Web version does not support such features at the moment. Being a heavy computer nerd, I decided to hack through the Instagram requests myself and figure out a way to fully purge my account directly from my computer.

Tools required to follow my humble research

  • Python 3.7 or later
  • virtualenv
  • pip
  • cURL

How to setup the environment for coding the script in python

First of all create a fresh directory for the project's root named python-instagram or any name which fits best for you. With the help of the virtualenv utility, create a fresh virtual environment with the main purpose of installing the required packages for powering the script.
cd python-instagram
virtualenv -p /usr/local/bin/python3.7 env # your python path here
Activate the fresh virtual environment with the help of the command shown below.
source env/bin/activate
Then install the instagram_private_api and ipython by running the command shown below on your local console while being inside the project's root.
pip install instagram_private_api ipython
Once you have managed to successfully install the above packages on your local environment, create the two scripts shown below with you favorite text editor. I prefer to use vim.
The first one serves as the main module for storing the whole code, while the second one stores configurations such as username and password which are required for the authentication.

Authenticate to your Instagram account

The package instagram_private_api helps to authenticate and launch requests to your account. Keeping it practical, launch the Ipython interactive shell through the terminal console like shown below.
Then import the class Client from the instagram_private_api like shown in the following instruction. 

Create an object of type Client by making use of the code shown below.

Once you have managed to create the object, try to find out the authenticated username and the unique id for your own account.

My results are shown below.
The authenticated user id is of importance as it is part of the media_id argument which is required to delete a specific post. 

Delete a single post from Instagram

Thankful to the instagram_private_api library, media deletion is a very easy and straightforward process. The object of type  instagram_private_api.client.Client has a method named delete_media which requires one positional argument, specifically the media_id.

Running the delete_media without the required positional argument produces an error. Type the following piece of code in your previous Ipython session and see the result for yourself.

Once I managed to execute the piece of code shown above, I got the following error.

As you can see, the media_id is a required positional argument. To find out the id of an Instagram's post, copy it's url, attach it to the following and then launch it in your web browser.
The request produces JSON data, useful to find out the media_id attribute. Mine is listed in the following section.
A simple look at the above output makes one come to the conclusion that the authenticated user id is part of the media_id attribute.

After having retrieved the value of the media_id, run the following command in your previous Ipython interactive session with the main purpose of deleting the post.

On success, you should receive the following output.

Although the above code does what it is intended to do very well, the fetching of the media_id is done manually.

Fetch the media_id values in bulk automatically

With the main purpose of retrieving the media_id values in bulk, we need to study the requests taking place in our Instagram account with the help of Developer Tools

Go to your Instagram account, and do a right click on one of the posts. Then click on Inspect. Under the Network tab, you will be able to see the requests taking place. Make sure XHR is clicked.

Do a refresh on the page. An output similar to the one shown below will be displayed.
XHR Requests 

There are many requests taking place, but to our interest are the ones which contain JSON data on our posts. Check the ones which have ?query_hash= and make sure to select the one that has information on the nodes, similar to the one shown in the following screenshot.
JSON data returned by the request

In order to understand the structure of the specific request, we need to copy it as cURL
cURL request

Then we can reproduce the request by executing it on our console.
Executing cURL request in the console.

The above request returns the same JSON data as the XHR request. 

Request the JSON data with python

Having the request as cURL, makes it easier for us to imitate it with the help of python. Launch a new Ipython interactive shell at the project's root and run the following command with the main purpose of importing the library for performing requests.

Then extract the value of the cookie header from the cURL request data and save it inside a dictionary like shown below.

Once you have managed to properly setup the cookie value, launch the request by following the code shown below.

As for the value of the query_hash, it can be easily extracted from the cURL request data. Once the request has been accomplished successfully, get the returned JSON data and save it into some variable like shown below. 

The returned JSON data contains information on the posts, known as nodes. Each one of the nodes contains a unique id, truly import for our research.

Fetch the returned nodes by filtering JSON data with the help of the command shown below.

Once you have managed to get the list of nodes, make use of a for loop to find out the unique id for each. 

After executing the above code on my Ipython interactive shell, I managed to get the unique id for each one of the twelve nodes returned in the JSON data.

unique id for each one of the twelve posts

Delete the posts in bulk

Having the id for each one of the twelve posts, we can try to perform a bulk deletion with the help of a python for loop.

First we need to attach the unique user's id to each one of the twelve ids and store them inside a python list object.

Print the list ids to make sure the data has the right form.
media_id of each post

Then, with the help of a for loop, run the delete_media method on each one of them. 

Final thoughts

Although we haven't fully purged our Instagram account, we managed to perform a bulk deletion through this first part by using a few lines of python code. As I continue my research on this topic, in the second part I will code a custom script with the main purpose of performing a full purge on any account.


No comments:

Post a Comment

Post Top Ad