Instagram, save posts on the gallery automatically using python

If you are an Instagram person you may know that Instagram doesn't have the functionality of saving the posts like photos and videos into our own home gallery, anyway, you can save posts into your saved collections on Instagram itself. But what if you can save the posts you find interesting to your computer gallery or phone gallery automatically by clicking on the same 'save' button on Instagram? It will be more interesting and useful, Python is one of the best programming language for stuff like this. So we are using a simple python script to achieve this task. 

So, first of all, we need to install the module named Instaloader which is a tool used to get the metadata from Instagram. you can install directly using pip

$ pip install instaloader

Now login to your Instagram account using the command interface

$ instaloader --login <insta_username>

After login, it will save a session on your computer, so it is easy to authenticate

Now in your ide write the code.

insta_save_posts.py

import instaloader
 
def save_insta_collection():

    username = "my_username"
    loader = instaloader.Instaloader() 
    loader.load_session_from_file(username)
    profile = instaloader.Profile.from_username(loader.context,username)
                                    
                                                  
    post_list = []

    try:
        for saved_posts in profile.get_saved_posts():
            post_list.append(saved_posts)
        loader.download_post(post_list[0],'mysavedcollection')

    except IndexError:
        print("You have no saved posts yet.")
     
    save_insta_collection()  # recursion

save_insta_collection()

What actually this script doing is monitoring the saved collections in a particular Instagram account and then downloading the latest post that the user saved.

First, we defined the function 'insta_save_gallery()' and inside the function, 'loader.load_session_from_file(username)' loads the session that is saved in the system at the time of login, then we added a 'profile' variable which holds the properties of a profile page, 'post_list' is a list used to store all the objects of the saved posts in the Instagram account. At last, the script downloads the latest post with its caption as a txt file from Instagram to the path where your script belongs (You can set the path to pictures folder by specifying the path). 'my_saved_collection' is the folder where all the files are stored. We are recursively doing this to check if a new post is saved.

  C:\Users\myname\Pictures> python insta_save_posts.py
Loaded session from C:\Users\myname\AppData\Local\Instaloader\session my_saved_collections\2021-05-29_07-18-58_UTC.jpg

Press CTRL + C to exit

conclusion:

Instaloader is a tool used to download posts from Instagram like images, videos, captions, hashtags, and many more. It works as a command-line as well as a module, so it can be used in many ways, for more visit https://instaloader.github.io/installation.html .