pythoninstagram

Use python to automatically post on Instagram


I am using instabot package on python to build an app that automatically post photos on my profile.

This is the code I wrote

from instabot import Bot
import configparser



# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")

# Setting configuration values
username = str(config['Instagram']['user'])
password = str(config['Instagram']['password'])



bot = Bot()
bot.login(username=username, password=password)
bot.upload_photo("./pic.png", caption="caption")
bot.logout()

the problem is that I get the error

2024-08-03 19:01:54,188 - INFO - Not yet logged in starting: PRE-LOGIN FLOW!
2024-08-03 19:01:54,754 - ERROR - Request returns 429 error!
2024-08-03 19:01:54,754 - WARNING - That means 'too many requests'. I'll go to sleep for 5 minutes.

How can I solve it? Can I use another package?


Solution

  • I solved my problem by changing the python package and using Instagrapi.

    This is the sample code that should work

    from instagrapi import Client
    from instagrapi.exceptions import LoginRequired
    import os
    
    
    username = "YOUR USERNAME"
    password = "YOUR PASSWORD"
    
    cl = Client()
    
    # Load session if it exists, otherwise login and save session
    try:
        cl.load_settings("./instagram_session.json") # INSERT YOUR SESSION MAE
    
    except FileNotFoundError:
        cl.login(username, password)
        cl.dump_settings("./instagram_session.json")
    
    except LoginRequired:
        if os.path.exists("../sessions/instagram_session.json"):
            os.remove("../sessions/instagram_session.json")
    
        print("Login required, old session deleted.")
        cl.login(username, password)
        cl.dump_settings("./instagram_session.json")
    
    
    print(cl.account_info().dict())
    media = cl.photo_upload("../hello.jpg", caption="hello")
    media_id = media.dict()["id"]
    cl.media_comment(media_id, "leave a comment")
    media_pk = media.dict()["media"]["pk"]