Created
March 12, 2012 07:30
-
-
Save anonymous/2020469 to your computer and use it in GitHub Desktop.
Email-to-Dropbox vocoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import re | |
| import smtplib | |
| import sys | |
| import tempfile | |
| import dropbox | |
| from flask import Flask, request | |
| # These are created on Dropbox's developer site | |
| DROPBOX_APP_KEY = 'FIXME' | |
| DROPBOX_APP_SECRET = 'FIXME' | |
| # these you get through running "main.py setup" | |
| DROPBOX_SESSION_TOKEN = 'FIXME' | |
| DROPBOX_SESSION_SECRET = 'FIXME' | |
| # NB: these are found under the "domains" tab of Mailgun, not your admin login creds | |
| MAILGUN_USERNAME = 'FIXME' | |
| MAILGUN_PASSWORD = 'FIXME' | |
| APPROVED_SENDERS = ['[email protected]', '[email protected]'] | |
| HEADER_FIELDS = ['To', 'From', 'Subject', 'Date', 'Reply-To'] | |
| FROM_EMAIL = '[email protected]' | |
| app = Flask(__name__) | |
| def _remove_starts_with(text, prefix): | |
| return re.sub('%s: .*\n' % prefix, '', text) | |
| def _sanitize_email(text): | |
| for s in HEADER_FIELDS: | |
| text = _remove_starts_with(text, s) | |
| text = re.sub('\[http^\]]+\]', '', text) | |
| text = re.sub('<http[^>]+>', '', text) | |
| return text | |
| def _upload_to_dropbox(wav_filename, subject): | |
| sess = dropbox.session.DropboxSession(DROPBOX_APP_KEY, DROPBOX_APP_SECRET, 'app_folder') | |
| sess.set_token(DROPBOX_SESSION_TOKEN, DROPBOX_SESSION_SECRET) | |
| c = dropbox.client.DropboxClient(sess) | |
| f = open(wav_filename, 'r') | |
| c.put_file('/%s.wav' % subject, f) | |
| def _send_email(sender, subject): | |
| body = """To: %s | |
| From: %s | |
| Subject: Re: %s | |
| Your message was received, transcoded, and uploaded to Dropbox""" % (sender, FROM_EMAIL, subject) | |
| smtp = smtplib.SMTP('smtp.mailgun.org', 587) | |
| smtp.login(MAILGUN_USERNAME, MAILGUN_PASSWORD) | |
| smtp.sendmail(FROM_EMAIL, sender, body) | |
| smtp.quit() | |
| @app.route("/read/", methods=['POST']) | |
| def reader(): | |
| sender_email = request.form['sender'] | |
| subject = request.form['subject'] | |
| text = request.form['stripped-text'] | |
| sender = request.form['from'] | |
| if sender_email not in APPROVED_SENDERS: | |
| print "invalid from address" | |
| return "fail" | |
| text = _sanitize_email(text) | |
| # save the email text out to a temp file (easier than piping through stdin, better debugging) | |
| (fd, filename) = tempfile.mkstemp(text=True) | |
| handle = open(filename, "w") | |
| handle.write(text) | |
| handle.close() | |
| print "Text: %s" % filename | |
| # convert the text to a WAV file using espeak | |
| (fd, wav_filename) = tempfile.mkstemp() | |
| os.system('espeak -f %s -w %s' % (filename, wav_filename)) | |
| print "Voice: %s" % wav_filename | |
| # upload to Dropbox app folder | |
| _upload_to_dropbox(wav_filename, subject) | |
| print "uploaded to Dropbox" | |
| # send a confirmation email | |
| _send_email(sender, subject) | |
| print "Email reply sent" | |
| return "ok" | |
| def _auth_to_dropbox(): | |
| # FIXME these could be factored out | |
| sess = dropbox.session.DropboxSession(DROPBOX_APP_KEY, DROPBOX_APP_SECRET, 'app_folder') | |
| sess.set_token(DROPBOX_SESSION_TOKEN, DROPBOX_SESSION_SECRET) | |
| token = sess.obtain_request_token() | |
| print "Go to: %s" % sess.build_authorize_url(token) | |
| raw_input() | |
| print "Save these: %s" % sess.obtain_access_token(token) | |
| if __name__ == '__main__': | |
| if len(sys.argv) > 1 and sys.argv[1] == 'setup': | |
| _auth_to_dropbox() | |
| else: | |
| app.run(host="0.0.0.0") # FIXME note this is dangerous for long-term use (and definitely don't allow debug) but needed to test Mailgun |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment