Photostitch sample.
[gae-samples.git] / photostitch / photostitch / handle_mail.py
blob85b6987c1745fac019977d91be323da37ba89414
1 #!/usr/bin/python2.5
3 # Copyright 2010 Google Inc. All Rights Reserved.
5 """Handle incoming email for Cloud Photo Stitcher.
7 Expects a zip file of images and stitches them.
9 mail to batch_name@photostitch.appspot.com.
10 """
12 try: # (code before imports) pylint: disable-msg=C6205
13 import auto_import_fixer # (unused import) pylint: disable-msg=W0611,C6204
14 except ImportError:
15 pass
17 import logging
18 import email
20 import config
21 from google.appengine.ext import webapp
22 from google.appengine.ext.webapp import util
23 from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
24 from google.appengine.ext.webapp.util import run_wsgi_app
25 from stitch_request import StitchRequest
27 class EmailHandler(InboundMailHandler):
28 def receive(self, mail_message):
29 logging.info("Received a message from: " + mail_message.sender)
30 images = mail_message.bodies('image/jpeg')
31 req = StitchRequest(mail_message.sender, 'batchx', config.gs_bucket)
32 for file, attachment in mail_message.attachments:
33 logging.info('attachment: %s' % file)
34 file = file.lower().replace(' ', '')
35 if not (file.endswith('.jpg') or file.endswith('.jpeg')):
36 continue
37 logging.info('writing %s, %d bytes' % (file, len(attachment)))
38 req.AddFile(file, attachment)
39 logging.info('%s' % req.toJson())
41 def main():
42 application = webapp.WSGIApplication([EmailHandler.mapping()],
43 debug=True)
44 util.run_wsgi_app(application)
47 if __name__ == '__main__':
48 main()