便利かなーと思って何となくやってみた。
(MacOSX10.4, python 2.5)
コード
実行すると、最新の返信20個を表示してから180秒(3分)毎にリプライ確認。
(GMailまわりは前に書いたコードで参考にしたサイトのものを流用っていうかコピペ)
#!/usr/bin/env python # *-# -*- coding: utf-8 -*- #use py25-socket-ssl, py25-twitter import twitter as tw from threading import Timer import smtplib from email.MIMEText import MIMEText from email.Utils import formatdate import time TWITTER_CONF = { 'user':'your_twitter_name', 'password':'your_twitter_password', } TWITTER_CONF = { 'user':'your_twitter_name', 'password':'your_twitter_password', } GMAIL_ACCOUNT = "hoge@gmail.com" GMAIL_PASS = "your_gmail_password" FROM_ADDRESS = "hoge@gmail.com" TO_ADDRESS = "hoge@gmail.com" class Get_twitter_replies: def __init__(self): self.last_get_replise_list = "" print "\ntwitter replies >\n%s\n\nwaiting for a response..." % \ self.get_replies_text() def get_replies_text(self): twitter = tw.Api(TWITTER_CONF['user'], TWITTER_CONF['password']) get_rep = twitter.GetReplies() # get last 20 replies_list = [] for data in reversed(get_rep): reply = '%s %s' % (data.GetUser().GetScreenName(), data.GetText()) replies_list.append(reply) result_list = [] for reply in replies_list: if not reply in self.last_get_replise_list: result_list.append(reply) result_str = "\n".join(result_list).encode('utf-8') self.last_get_replise_list = replies_list return result_str def send_rep_gmail(self, rep_text): time_str = time.strftime('%Y-%m-%d %H:%M:%S') num_rep = len(rep_text.split("\n")) subject = "You've got %d new twitter replies. %s" %(num_rep, time_str) body = "%s\n\nhttp://twitter.com/#replies" % rep_text msg = MIMEText(body) msg['Subject'] = subject msg['From'] = FROM_ADDRESS msg['To'] = TO_ADDRESS msg['Date'] = formatdate() s = smtplib.SMTP('smtp.gmail.com', 587) s.ehlo() s.starttls() s.ehlo() s.login(GMAIL_ACCOUNT, GMAIL_PASS) s.sendmail(FROM_ADDRESS, [TO_ADDRESS], msg.as_string()) s.quit() def check(self): rep_text = self.get_replies_text() if not rep_text == "": self.send_rep_gmail(rep_text) print "found it!", print "check > %s" % time.strftime('%Y-%m-%d %H:%M:%S') if __name__ == "__main__": g_tw_rep = Get_twitter_replies() sec = 180.0 while True: t = Timer(sec, g_tw_rep.check) t.run()
とりあえず動いたが...