Search code examples
pythoninstagraminstagram-api

Instabot API for Python raises error after running code for the 2nd time


I am currently working with the Instabot API for python and I ran across the following issue:

I wrote a small program:

from instabot import Bot

bot = Bot()
bot.login(username = "[my username]", password = "[my passowrd]")

bot.follow("lego")

which worked fine after running it for the very first time. However, after running the program for a second time, this time following another account, it raised an error ("KeyError: ds_user").

This error can be fixed by deleting the config folder inside the project folder. Unfortunately, this isn't a very sustainable solution, as it makes working on the code really arduous. I therefore would like to know if there is any solution for getting the program to run multiple times without having to delete the config folder over and over again.

I am receiving the following traceback (code is running in an anaconda environment called "Instagram Automation"):

Traceback (most recent call last):
  File "e:/Programme/OneDrive/Dokumente/Projekte/Instagram Automation/main.py", line 4, in <module>
    bot.login(username = "[my username]", password = "[my password]")
  File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\bot\bot.py", line 443, in login
    if self.api.login(**args) is False:
  File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\api\api.py", line 240, in login
    self.load_uuid_and_cookie(load_cookie=use_cookie, load_uuid=use_uuid)
  File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\api\api.py", line 199, in load_uuid_and_cookie
    return load_uuid_and_cookie(self, load_uuid=load_uuid, load_cookie=load_cookie)
  File "E:\Programme\Anaconda\envs\Instagram Automation\lib\site-packages\instabot\api\api_login.py", line 352, in load_uuid_and_cookie
    cookie_username = self.cookie_dict["ds_user"]
KeyError: 'ds_user'  

Solution

  • As far as I can see, the only way on your side to fight the symptoms is to always delete the JSON file in the config folder, e.g:

    import os
    if os.path.isfile("path/to/config/file.json"):
        os.remove("path/to/config/file.json")
    
    import instabot
    # rest of your code goes here
    

    The developers of instabot should fix the source of the problem, for example by using self.cookie_dict.get("ds_user", "some default value") instead of self.cookie_dict["ds_user"]