Home > Net >  '429 error: too many requests' when using Instagram API with Python script
'429 error: too many requests' when using Instagram API with Python script

Time:01-06

I am trying to run a script that logs in to Instagram and uploads 10 images that have random text on them that have been generated. However, here is the output I am getting when I try to run the script:

2023-01-02 21:56:48,608 - INFO - Instabot version: 0.117.0 Started
Which account do you want to use? (Type number)
1: ACCOUNTNAME
0: add another account.
-1: delete all accounts.
1
2023-01-02 21:56:51,371 - INFO - Not yet logged in starting: PRE-LOGIN FLOW!
2023-01-02 21:56:51,573 - ERROR - Request returns 429 error!
2023-01-02 21:56:51,573 - WARNING - That means 'too many requests'. I'll go to sleep for 5
minutes.

How can I fix this issue?

Here is the code I am using:

import random
import requests
from PIL import Image, ImageDraw, ImageFont
import time
import instabot

# Generate 10 random images
for i in range(10):
  # Create a black image
  image = Image.new('RGB', (500, 500), (0, 0, 0))
  draw = ImageDraw.Draw(image)

  # Generate a random string of white text
  text = ''.join([
    random.choice(
      'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_ -=[]{};:\'",.<>/?')
    for i in range(10)
  ])
  # Use a default font with a larger size
  font = ImageFont.truetype('cour.ttf', 72)

  # Randomly position each letter on the image
  x = 0
  y = 0
  for letter in text:
    draw.text((x, y), letter, (255, 255, 255), font=font)
    x  = random.randint(30, 50)
    y  = random.randint(-10, 10)

  # Save the image and create a corresponding text file
  image.save(f'generated-images/image{i}.jpg')
  with open(f'generated-images/image{i}.txt', 'w') as f:
    f.write(text)

try:
  # Create an Instabot instance
  bot = instabot.Bot()

  # Login to Instagram
  bot.login()

  # For each image and corresponding text file
  for i in range(10):
    # Open the text file
    with open(f'generated-images/image{i}.txt', 'r') as f:
      # Use the text from the file as the caption
      caption = f.read()

  # Post the image and use the text from the file as the caption
  bot.upload_photo(f'generated-images/image{i}.jpg', caption=caption)

  # Add a delay between requests
  time.sleep(200)
except:
  print('Error')
finally:
  bot.logout()
  print('logging out')

# Log out of Instagram
bot.logout()

It logged into my account successfully with no errors, but when it tries to post the images it gives the '429 error'...

CodePudding user response:

Fixed by adding a delay then waiting until the next day, because the first time I ran the code apparently I sent way too many requests in a day.

  • Related