0

I am using the InstagramAPI module from this github to create a script that posts pictures at certain time. A snippet below shows part of the code:

Insta = InstagramAPI(account_name, password)
Insta.login()
InstagramAPI.uploadPhoto(post_path, caption)
InstagramAPI.logout()

This works fine, unless the picture is in the wrong format. If it is in the wrong format, nothing is posted and this is printed:

Request return 400 error!
{u'status': u'fail', u'message': u"Uploaded image isn't in the right format"}

I have a function that will re-size if it isn't in the correct format. However, it is just print and not an error. Therefor, I can not put this in a try/except. So if the file isn't in the right format, it just skips and posts nothing.

Does anyone know of a way I could have my code save the print output to a variable so I can check if it contains "Uploaded image isn't in the right format" and raise an error if so?

5
  • 1
    Why the 3.x tag? u is purely a Python2 thing. Commented Jun 18, 2018 at 19:15
  • Read the API docs and find out what the return value is. Commented Jun 18, 2018 at 19:16
  • 2
    If this random person's unofficial Instagram interface module is low-quality and swallows exceptions to print to stdout, have you considered not using it, or forking it? Commented Jun 18, 2018 at 19:18
  • @MadPhysicist the issue is that the return value is not in the actual code itself, but a print statement returned from Instagram (I believe) so it cannot be forked and changed. Commented Jun 19, 2018 at 16:25
  • 1
    @user53558. But I bet if you read the docs, you'd find that the function returns something different when the printout happens. Commented Jun 19, 2018 at 16:28

1 Answer 1

1

InstagramAPI.uploadPhoto() returns False if it unsuccessfully tries to post your photo. You can do something like:

Insta = InstagramAPI(account_name, password)
Insta.login()
success = Insta.uploadPhoto(post_path, caption)
if not success:
    resizeImage()  # your resize image logic
else:
    Insta.logout()

This will check to see if it was uploaded successfully, and if not, you can resize the image and try to upload again.

In your comment, you say that it returns False regardless of success, but I find that hard to believe. In uploadPhoto() we see this:

    response = self.s.post(self.API_URL + "upload/photo/", data=m.to_string())
    if response.status_code == 200:
        if self.configure(upload_id, photo, caption):
            self.expose()
    return False

This will only return False if the response code does not equal 200 or if self.configure() returns a falsey value.

self.configure() only generates a request body and calls:

return self.SendRequest('media/configure/?', self.generateSignature(data))

which will return true if your request to the Instagram API was successful:

if response.status_code == 200:
    self.LastResponse = response
    self.LastJson = json.loads(response.text)
    return True
Sign up to request clarification or add additional context in comments.

6 Comments

Hey, thanks for that tip. Unfortunately it seems like it returns false even when it successfully posts my photo. However, when it isn't successful it still prints that string out, but when it is successful nothing prints out. success = False in both cases though.
edited - I find it hard to believe that success == False for successes
I'm not sure what it is, but it definitely returns False. Just gave it another try, making sure to save the return of the function as a variable and print it. Here is the output for .login() and .uploadPhoto(). Request return 429 error! {u'status': u'fail', u'message': u'Please wait a few minutes before you try again.'} Login success! False but my photo is there if I check Instagram.
well 429 is too many requests, so you're probably just spamming the api... what is probably happening is that your post is uploaded correctly, but the self.configure() call fails somehow
So it turns out I haven't updated the package since that change was implemented. Updating the package fixed the problem and resulted in exactly what you said. You're a hero, thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.