使用 Tweepy 给自己发送电子邮件提醒。但是电子邮件内容正在累积每条推文。我想让它覆盖
Using Tweepy to email myself alerts. But the email content is accumulating every Tweet. I want it to overwrite
我正在使用 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
实例。
您需要使用 .set() 方法与 .attach()
另外,你有
mail_content = []
我会将其重置为空字符串
mail_content = ''
我正在使用 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
实例。
您需要使用 .set() 方法与 .attach()
另外,你有
mail_content = []
我会将其重置为空字符串
mail_content = ''