Instabot 发布图片但不发布标题

Instabot Posts Images but Not Captions

我正在构建一个从 Unsplash API 获取图像的机器人,然后在 Instagram 上重新post发送它们。每个图像都带有一个使用函数 getQuote() 生成的标题。我面临的问题是字幕没有上传。我尝试通过放置简单的字符串而不是从函数获取数据来隔离问题,但无济于事,因为 Instabot 没有 post Instagram 上的标题。 下面是上传功能:

def upload(path): 
bot.upload_photo(path, caption="Follow for more!")
toast.show_toast("Notification",str(num)+" images are uploaded",duration=300,threaded=True)
print(str(num)+" images are uploaded")
print("----------------------------------")
try:
    os.remove(os.path.join(os.getcwd(), path+""))
except:
    os.remove(os.path.join(os.getcwd(), path+".REMOVE_ME"))

最后4行删除已经上传的图片。 在理想情况下,bot.upload 会有一个 caption=getQuote() 昨天,在测试机器人上传的速度时(当时字幕有效),Instagram 切断了我的访问权限,但今天我仍然可以上传图片,但没有字幕。在 Instagram 上手动更改字幕是可能的,所以我怀疑某些 server-side 禁令。如果我只是创建一个调用函数 getQuote 的打印语句 - 我会得到足够的响应,这意味着引号已成功从 quotes.json 文件中加载。

下面是我的全部代码:

    from instabot import Bot 
    from skimage.io import imread_collection
    from glob import glob
    from win10toast import ToastNotifier
    import random
    import json
    import os
    import urllib.request
    import time
    import requests
    import smtplib
    import smtplib
    from tkinter import *
    from fake_useragent import UserAgent
    ua = UserAgent()
    
    num=0
    window = Tk()
    window.title("Photos Uploaded")
    window.iconbitmap("Instagram/Instagram.ico")
    lbl = Label(window, text=num, font=("Arial Bold", 100))
    lbl.grid(column=0, row=0)
    window.update()
    #This functions checks if the site is up
    def url_ok(url):
        r = requests.head(url)
        return r.status_code == 200
    
    #Below functions sends emails 
    def sendMessage(Num):
        gmail_user = ''
        gmail_password = ''
        sent_from = gmail_user
        to = ['']
        subject = 'Upload Count'
        body = "Upload Count"
        email_text = Num
        try:
            server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
            server.ehlo()
            server.login(gmail_user, gmail_password)
            server.sendmail(sent_from, to, email_text)
            server.close()
        except:
            print("Could not send a push-notification!")
    
    with open("Instagram/NumberOfImagesUploaded.txt", "r") as f:
       num = int(float(f.read()))
    #This functions loads the number of already uploaded images from a file
    def ChangeNum(n):
        num=n
        with open("Instagram/NumberOfImagesUploaded.txt", "w") as f:
            f.write(str(int(n)))
    
    links=[]
    r=''
    with open(os.path.abspath("Instagram/Data/links.txt"), 'r') as filehandle:
        for line in filehandle:
            currentPlace = line[:-1]
            links.append(currentPlace)
    print("Started at: " +time.ctime()+" with a list of "+str(len(links))+" links.")
#This functions gets images from the API    
def GetImage():
        r = requests.get('https://source.unsplash.com/featured/3840x2160')
        while r.url == "https://images.unsplash.com/source-404?fit=crop&fm=jpg&h=800&q=60&w=1200":
            print("API requests are over capita! Waiting one hour. Continuing at "+str(time.ctime(time.time() + 3600)+"..."))
            time.sleep(3600)
            print("Trying again... "+time.ctime())
            r = requests.get('https://source.unsplash.com/featured/3840x2160')
        try:
            urllib.request.urlretrieve(str(r.url), os.path.abspath("Instagram/media")+"\"+str(random.randrange(1,10000))+".jpg")
        except:
            print("API requests are over capita! Waiting in 10 hours. Continuing at "+str(time.ctime(time.time() + 36000)+"..."))
            time.sleep(36000)
            urllib.request.urlretrieve(str(r.url), os.path.abspath("Instagram/media")+"\"+str(random.randrange(1,10000))+".jpg")
        while r.url in links:
            r = requests.get('https://source.unsplash.com/featured/3840x2160')
       
        links.append(r.url)
        with open(os.path.abspath("Instagram/Data/links.txt"), 'w') as filehandle:
            for listitem in links:
                filehandle.write('%s\n' % listitem)
    GetImage()
    print("Backup image loaded at: " +time.ctime())
    toast = ToastNotifier()
    col_dir =os.path.abspath("Instagram/media")+"\"+("*.jpg")
    col = glob(col_dir)
    print(col_dir)
    with open(os.path.abspath("Instagram/Data/quotes.json")) as f:
      data = json.load(f)

    def getQuote():
        quote = random.choice(data)
        return ('"'+quote['text']+'" - '+quote['author']+" \n\n Follow us for more! \n \n #photography #travelphotography #naturephotography #streetphotography #foodphotography #portraitphotography #photographylovers #landscapephotography #weddingphotography #blackandwhitephotography #filmphotography #canonphotography #fashionphotography #mobilephotography #photographyislife #nikonphotography #photographyeveryday #photographylover #photographysouls #architecturephotography #photographyislifee #wildlifephotography #nightphotography #iphonephotography #macrophotography #urbanphotography #bnwphotography #analogphotography #dogphotography #photographylife")
    print(getQuote())
    bot = Bot() 
    bot.login(username = "",  password = "")
    def upload(path): 
        bot.upload_photo(path, caption="Follow for more!")
        toast.show_toast("Notification",str(num)+" images are uploaded",duration=300,threaded=True)
        print(str(num)+" images are uploaded")
        print("----------------------------------")
        try:
            os.remove(os.path.join(os.getcwd(), path+""))
        except:
            os.remove(os.path.join(os.getcwd(), path+".REMOVE_ME"))
    while True:
        GetImage()
        col = glob(col_dir)
        ranTime=random.randrange(1800,3600)
        print("Images in stack: "+str(col))
        print("Waiting 30 to 60 minutes. Resuming at "+str(time.ctime(time.time() + ranTime)))
        #time.sleep(ranTime)
        print("Upload sequence has begun...")
        lbl.configure(text=num)
        window.update()
        if num % 50 == 0:
            sendMessage(str(num)+" images uploaded.")
        for i in col:
            num+=1
            lbl.configure(text=num)
            window.update()
            ChangeNum(num)
            upload(i)
            imageDir = os.listdir(os.getcwd())
            for item in imageDir:
                if item.endswith(".REMOVE_ME"):
                    os.remove(os.path.join(os.getcwd(), item))

这是我的文件结构的图片

如何让机器人开始上传带字幕的图片?

首先,了解 Instagram 字幕的限制很重要!最大字幕长度为 2,200 个字符(相当长!)。您还可以使用最多 30 个主题标签。所以只需满足 2 个条件:

  1. 字幕长度应少于 2200 个字符
  2. 标题应少于 30 个主题标签