[英]Tweet Strings via Tweepy
我正在使用tweepy來自動發布一個URL列表。 但是,如果我的列表太長(它可以從推文到推文),我不被允許。 無論如何,當內容太長時,tweepy可以創建一個推文線程嗎? 我的tweepy代碼如下所示:
import tweepy
def get_api(cfg):
auth = tweepy.OAuthHandler(cfg['consumer_key'],
cfg['consumer_secret'])
auth.set_access_token(cfg['access_token'],
cfg['access_token_secret'])
return tweepy.API(auth)
def main():
# Fill in the values noted in previous step here
cfg = {
"consumer_key" : "VALUE",
"consumer_secret" : "VALUE",
"access_token" : "VALUE",
"access_token_secret" : "VALUE"
}
api = get_api(cfg)
tweet = "Hello, world!"
status = api.update_status(status=tweet)
# Yes, tweet is called 'status' rather confusing
if __name__ == "__main__":
main()
您的代碼與您嘗試解決的問題無關。 main()
似乎不會帶任何參數(推文文本?),但是你沒有說明你目前是如何嘗試接近這個問題的。 請考慮以下代碼:
import random
TWEET_MAX_LENGTH = 280
# Sample Tweet Seed
tweet = """I'm using tweepy to automatically tweet a list of URLs. However if my list is too long (it can vary from tweet to tweet) I am not allowed."""
# Creates list of tweets of random length
tweets = []
for _ in range(10):
tweets.append(tweet * (random.randint(1, 10)))
# Print total initial tweet count and list of lengths for each tweet.
print("Initial Tweet Count:", len(tweets), [len(x) for x in tweets])
# Create a list for formatted tweet texts
to_tweet = []
for tweet in tweets:
while len(tweet) > TWEET_MAX_LENGTH:
# Take only first 280 chars
cut = tweet[:TWEET_MAX_LENGTH]
# Save as separate tweet to do later
to_tweet.append(cut)
# replace the existing 'tweet' variable with remaining chars
tweet = tweet[TWEET_MAX_LENGTH:]
# Gets last tweet or those < 280
to_tweet.append(tweet)
# Print total final tweet count and list of lengths for each tweet
print("Formatted Tweet Count:", len(to_tweet), [len(x) for x in to_tweet])
為了便於解釋,它盡可能地分開。 要點是,可以從用作推文的文本列表開始。 變量TWEET_MAX_LENGTH
定義了每條推文的分割位置以允許多條推文。
to_tweet
列表將按照初始列表的順序包含每條推文,並擴展為多個<= TWEET_MAX_LENGTH
長度字符串的推文。
您可以使用該列表輸入您發布的實際tweepy函數。 這種方法非常不受歡迎,並且不會對維護拆分推文的順序進行任何檢查。 根據您實施最終推文功能的方式,這可能是一個問題,但也是一個單獨的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.