AppEngineã®InBoundMailã使ã£ã¦Twitterãæ´æ°ããã
æ´æ°ã®APIã«ã¯Tweepyã使ã£ã¦ã¿ãã
InBoundMailã®ãµã³ãã«ã«ã¤ãã¦ã¯ãCookBookの例ãè¦ã¦ãããã¨ãã¦ãæ¬æã®æ¥æ¬èªã®æåã³ã¼ãããã¿ããã¾ããã£ã¦ããªãã£ãããªãã ãããã£ã¦ããªãã£ããã©ãBã¨ã³ã³ã¼ãã®æååã«ãªã£ã¦ããã®ã§ãè¦ã¯MIMEé¢é£ã§ãããã¨ã
ã¨ããããã§ãemail.Headerã¨ãunicodeé¢é£ã®ã¡ã½ãããç¨ãã¦ä¸è¨ã®ããã«ãããã§twitterå´ã¸ã®æç¨¿ã«æåããã
#!/usr/bin/env python # -*- coding: utf-8 -*- import logging from google.appengine.ext import webapp from google.appengine.ext.webapp.mail_handlers import InboundMailHandler from google.appengine.ext.webapp.util import run_wsgi_app from email.Header import decode_header import tweepy class MailHandler(InboundMailHandler): def receive(self, message): basic_auth = tweepy.BasicAuthHandler("username", "password") api = tweepy.API(basic_auth) bodies = message.bodies(content_type='text/plain') body_line = "" for body in bodies: h = decode_header(body[1].decode()) body_line = body_line + unicode(h[0][0], "iso-2022-jp") api.update_status(body_line.encode("utf-8")) application = webapp.WSGIApplication([MailHandler.mapping()], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main()