Python 怎么可能推特超过280个字符?如果字符串>;280,打印前280个字符,然后在该线程中与其余字符一起再次tweet

Python 怎么可能推特超过280个字符?如果字符串>;280,打印前280个字符,然后在该线程中与其余字符一起再次tweet,python,python-3.x,twitter,tweepy,twitterapi-python,Python,Python 3.x,Twitter,Tweepy,Twitterapi Python,很抱歉标题不连贯,字数有限(讽刺)。我已经创建了一个机器人,当提到它时,它会从列表中随机选择一个字符串进行响应,这很正常 问题是我列表中的大多数字符串都超过280个字符,twitter的字数限制。此外,用户的twitter句柄、“@”和用户名后的空格(字符串的起始位置)也会计入字数限制 这是我想出的代码,但我被难住了 import replies # Separate file where all replies are stored def reply_to_tweets(): pri

很抱歉标题不连贯,字数有限(讽刺)。我已经创建了一个机器人,当提到它时,它会从列表中随机选择一个字符串进行响应,这很正常

问题是我列表中的大多数字符串都超过280个字符,twitter的字数限制。此外,用户的twitter句柄、“@”和用户名后的空格(字符串的起始位置)也会计入字数限制

这是我想出的代码,但我被难住了

import replies # Separate file where all replies are stored
def reply_to_tweets():
    print("Scanning for new mentions...")
    last_seen_id = retrieve_id(FILE)
    mentions = api.mentions_timeline(last_seen_id, tweet_mode="extended")
    myself = api.me()
    for tweet in reversed(mentions): 
        if myself.screen_name in tweet.full_text: # If my @ is in the full text
            if tweet.in_reply_to_status_id is not None:
                continue
            last_seen_id = tweet.id 
            store_id(last_seen_id,FILE) 
            random_choice = random.choice(replies.strings_more_than_280)  # Random string chosen
            def splitter(chosen_string):
                return [char for char in chosen_string] # This part may be obsolete but it gives 
                                                        # me peace of mind as I know it counts 
                                                        # emojis and spaces!

            username = '@' + tweet.user.screen_name 
# There is a space at the beginning of every string in my list, so this should 
# account for the character after the @ where the string should go
            length_of_user = len(username) # WORKS
            
            splitter_in_action = splitter(random_choice)
            length_of_string = len(splitter_in_action)
# This is where the space is counted, since there's a space in the beginning of 
# the string already, then it'll count towards the length of the string when we 
# calculate it using the splitter() function

            difference = length_of_string - length_of_user
            difference2 = difference - length_of_user
            if length_of_string + length_of_user > 280: 
                print(username + random_choice[:difference2]) # status update goes here
                
# Now those last few lines of code, I'm just guessing, I have no idea how to 
# calculate for the difference in my string minus the user's @ and then tweet up
# to 280 chars, then tweet the rest later. It's probably simple but I am perplexed!!!
错误是
[{'code':186,'message':'Tweet需要短一点。}]

  • 编辑:删除了整个错误回调(不确定它到底叫什么),只添加了上面的主要错误

任何帮助都将是巨大的,因为我已经被困在这里一段时间了。这并不是为了给人发垃圾邮件和让人讨厌,只是我想让机器人打印的消息太长了,所以是的,我知道这有很多代码,但我真的希望有人能启发我

这是一项正在进行的工作,需要根据您的用例进行更多的测试。我做了一些研究,并确定最好的方法来分割你的长推特将与。此模块将允许您将tweet清晰地分解成块

在我的测试中,我只尝试了一条tweet和一个twitter句柄,但我相信下面的代码应该适合您的用例,或者为您提供一个坚实的起点,使此功能适合您的代码

如果您对代码有任何疑问,请告诉我

import math
import textwrap

# the tweet in this list is 1471 characters in length
tweets = [
    'Four score and seven years ago our fathers brought forth upon this continent, a new nation, conceived in Liberty, '
    'and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, '
    'testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a '
    'great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those '
    'who here gave their lives that that nation might live. It is altogether fitting and proper that we should '
    'do this. But, in a larger sense, we can not dedicate—we can not consecrate—we can not hallow—this ground. '
    'The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. '
    'The world will little note, nor long remember what we say here, but it can never forget what they did here. '
    'It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have '
    'thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that '
    'from these honored dead we take increased devotion to that cause for which they gave the last full measure of '
    'devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, '
    'shall have a new birth of freedom—and that government of the people, by the people, for the people, shall '
    'not perish from the earth. —Abraham Lincoln']


twitter_handle = ['@MariahCarey']

for handle in twitter_handle:
    handle_length = len(handle)
    for tweet in tweets:

        # obtain length of tweet, which is 1471 characters
        tweet_length = len(tweet)

        # check length
        if tweet_length <= 280:
            # do some here

        elif tweet_length >= 280:

            # divided tweet_length / 280
            # You might consider adjusting this down 
            # depending on how you want to format the 
            # tweet.
            tweet_length_limit = tweet_length / 280

            # determine the number of tweets 
            # math.ceil is used because we need to round up
            tweet_chunk_length = tweet_length / math.ceil(tweet_length_limit) + handle_length

            # chunk the tweet into individual pieces
            tweet_chunks = textwrap.wrap(tweet,  math.ceil(tweet_chunk_length), break_long_words=False)

            # iterate over the chunks 
            for x, chunk in zip(range(len(tweet_chunks)), tweet_chunks):
                if x == 0:
                    print(f'{handle} 1 of {len(tweet_chunks)} {chunk}')
                else:
                    print(f'{handle} {x+1} of {len(tweet_chunks)} {chunk}')
                    

使用
difference=length\u of_string-length\u of_user
计算的差异对您没有多大帮助,因为这里不涉及字符数限制280。所以假设我的tweet的长度是320,而用户的
length\u
是5,那么差值是315。所以当你用
random\u choice[:difference]
发推特时,它会变成
random\u choice[:315]
,仍然超过280个限制。嗨@Ank!很抱歉反应太晚。你发布的那天我确实看过你的建议,但现在是我的考试季节,所以我不得不暂时搁置我的爱好。我脑子里一直在想这个想法,但我只是没有一个方程式可以解释推特中的人物。但我确实想出了一些办法!我可以将我创建的用于返回用户名完整长度的代码(包括@)发送给您吗?也许你能帮我更进一步?你解决这个问题了吗?@Lifeiscomplex还没有什么。我想只是我不懂这里的数学。我对代码进行了进一步编辑,以反映我的更改和想法。@Ank谢谢,我真的很感激!我已经编辑了代码以显示我的新想法。我想在这一点上,我只是不理解我想要完成的事情背后的数学原理。它工作得很好,我真的很感激你不知道。我从你的代码中学到了很多东西,虽然有些东西我还需要一点时间才能理解,但我可以开始理解它的基本思想,如果我进一步看,我会理解的。我已经修改了你的解决方案,推文也发了!现在唯一的问题是,他们只发了一条推,而不是回复提到我的人。但我会弄明白,这只是移动一些if语句的问题。再次感谢各位用户@塞尔吉奥博伊斯:你的问题很有意思。我很高兴我的代码帮助了你。快乐编码!!
# length 275
@MariahCarey 1 of 6 Four score and seven years ago our fathers brought forth upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any

# length 268
@MariahCarey 2 of 6 nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is

# length 278
@MariahCarey 3 of 6 altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate—we can not consecrate—we can not hallow—this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or

# length 276
@MariahCarey 4 of 6 detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It

# length 278
@MariahCarey 5 of 6 is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have

# length 211
@MariahCarey 6 of 6 died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth. —Abraham Lincoln