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

Question:

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:ProgrammeAnacondaenvsInstagram Automationlibsite-packagesinstabotbotbot.py", line 443, in login
    if self.api.login(**args) is False:
  File "E:ProgrammeAnacondaenvsInstagram Automationlibsite-packagesinstabotapiapi.py", line 240, in login
    self.load_uuid_and_cookie(load_cookie=use_cookie, load_uuid=use_uuid)
  File "E:ProgrammeAnacondaenvsInstagram Automationlibsite-packagesinstabotapiapi.py", line 199, in load_uuid_and_cookie
    return load_uuid_and_cookie(self, load_uuid=load_uuid, load_cookie=load_cookie)
  File "E:ProgrammeAnacondaenvsInstagram Automationlibsite-packagesinstabotapiapi_login.py", line 352, in load_uuid_and_cookie
    cookie_username = self.cookie_dict["ds_user"]
KeyError: 'ds_user'  
Asked By: Felix Back

||

Answers:

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"]

Answered By: TheEagle
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.