gpt4 book ai didi

python - 使用 Tweepy 给自己发送电子邮件提醒。但是电子邮件内容正在累积每条推文。我想让它覆盖

转载 作者:行者123 更新时间:2023-12-05 03:28:44 27 4
gpt4 key购买 nike

我正在使用 Tweepy 和流媒体实时跟踪推文。每当有人用某些关键词发帖时,我都会尝试通过电子邮件将推文发送给自己:

class StreamCollector(tweepy.Stream):

def on_status(self, status):
if not hasattr(status, 'retweeted_status') and status.in_reply_to_screen_name == None and status.is_quote_status == Fal\
se:
if status.author.followers_count > 10:
print('Twitter Handle: @'+status.author.screen_name)
print('Followers:',status.author.followers_count)
print('Tweet:',status.text)
print('\n')

mail_content = 'Tweet: {0}\nFollowers: {1}\nTweet Link: https://twitter.com/{2}/status/{3}'.format(status.text,\
status.author.followers_count,status.author.screen_name,status.id_str)

message['Subject'] = '@'+status.author.screen_name #The subject line


message.attach(MIMEText(mail_content, 'plain')) #Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password

text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
mail_content = []
session.quit()

stream = StreamCollector(api_key,api_key_secret,access_token, access_token_secret)
stream.filter(track=["trigger words"])

一切正常 - 除了 - mail_content 似乎附加在每条新推文中 - 但我希望它被覆盖。目前,每封新电子邮件还包含所有先前推文的 mail_content

谁能解释为什么会这样,我该如何解决?

最佳答案

看起来 message 被重用了,你继续做 attach(payload) .

调用set_content()相反。

# message.attach(MIMEText(mail_content, 'plain'))  # -
message.set_content(mail_content) # +

您可能也应该每次都创建一个新的 message 实例。

关于python - 使用 Tweepy 给自己发送电子邮件提醒。但是电子邮件内容正在累积每条推文。我想让它覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71174737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com