Search code examples
pythonloopsbotspinterest

The time waiting problem on the Python Pinterest bot


I'm a rookie Python developer and trying to do small projects to improve myself. Nowadays, I'm developing a Pinterest bot in one of these. This simple bot pins the images in the folder to the account with the Pinterest API. The API has a maximum of 10 visual loading limits within one hour, and I don't want to limit the number of images in the file. I've tried a few things, but can't find a way without errors, because I'm inexperienced, think there's something I can't see. I would appreciate it if you could give me an idea.

  • I have written a simple if - else loop and each time after loading ten images in the file, it has 1-hour wait with time.sleep. The API gave a timeout error.

  • I've edited the loop above for 7 minutes. The API gave a timeout error.

  • I've tried to down time.sleep to a minute, it works well, but after ten images, the API limit has been a problem.

  • I've defined the code that runs the API as a function with def and placed it in the loop. I thought that it wouldn't be a problem because it would restart the API after the sleep phase with else. It pined ten images without any issues, but after the sleep back to the beginning, the API gave a timeout error.

Version with loop :

api = pinterest.Pinterest(token="")
board = ''
note = ''
link = ''

image_list = []
images = open("images.txt", "w")
for filename in glob.glob('images/*.jpg'):
    image_list.append(filename)

i = 0
p = 0
while i < len(image_list):
    if p <= 9 and image_list[i] not in images:
        api.pin().create(board, note, link, image_list[i])
        i += 1
        p += 1
        images.write(image_list[i])
    else:
        time.sleep(3600)
        p = 0
        continue 

Version with def :

def dude() :
    i = 0
    api = pinterest.Pinterest(token="")
    board = ''
    note = ''
    link = ''
    api.pin().create(board, note, link, image_list[i])
    time.sleep(420)

i = 0
while i < len(image_list):
    dude()
    i += 1
    print(i)

Solution

  • After trying a lot of things, I was able to solve the problem with the retrying library. First I installed the library with the following code.

    $ pip3 install retrying
    

    After the installation, I changed my code as follows and the bot started working properly without any API or time error.

    from retrying import retry
    
    image_list = []
    images = open("images.txt", "w")
    for filename in glob.glob('images/*.jpg'):
        image_list.append(filename)
    
    @retry
    def dude() :
        api = pinterest.Pinterest(token="")
        board = ''
        note = ''
        link = ''
        api.pin().create(board, note, link, image_list[i])
    
    i = 0
    while i < len(image_list):
        dude()
        i += 1
        time.sleep(420)