Search code examples
pythonpython-3.xexceptioninstagraminstagram-api

Prevent python script from crashing with try/except using instapy-cli library


I am using a python library that posts to instagram (instapy-cli). However, this will be for an installation and if the wifi cuts out I still want the script to run even if the upload doesn't work. I have wrapped it in a try/except but if the function crashes, it still ends the script.

Here's my code

try:
    with client(username,password) as cli:
        cli.upload(fileName, text)
except Exception as e:
    print(e.output)

In the console I get below mentioned error:

ClientError URLError urlopen error [Errno 11001] getaddrinfo failed> (Code: 0, Response: )

Before I had the code:

try:
    subprocess.call([r'filePath/insta.bat', fileName, caption])
except subprocess.CalledProcessError as e:
    print(e)

which worked exactly as I wanted (if no wifi the script kept running and it failed silently)


Solution

  • I dug into the source code of the library instapy-cli and found in the upload function:

    except ClientLoginError as e:
        print('ClientLoginError {0!s}'.format(e))
        exit(9)
    

    So I forked the repo, commented out the exit function, uninstalled the library, and reinstalled my forked version. Worked like a charm!