diff options
| author | Scott Ostler <scottbot9000@gmail.com> | 2010-08-15 00:14:13 -0400 |
|---|---|---|
| committer | Scott Ostler <scottbot9000@gmail.com> | 2010-08-15 00:14:13 -0400 |
| commit | 07a3e9044ec9805d987ed5f8b28cc6fedaf203f2 (patch) | |
| tree | dd7faab0c001853b16f303b04aab04c19fe58a4c /scripts | |
| parent | db3d22ba1c1731e873a5a9296aad94071cdd163b (diff) | |
add email script
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/emailposter.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/scripts/emailposter.py b/scripts/emailposter.py new file mode 100644 index 0000000..d380ffd --- /dev/null +++ b/scripts/emailposter.py @@ -0,0 +1,105 @@ +import httplib +import mimetypes +import email +import poplib +import time +from email import parser + +def post_multipart(host, selector, fields, files): + """ + Post fields and files to an http host as multipart/form-data. + fields is a sequence of (name, value) elements for regular form fields. + files is a sequence of (name, filename, value) elements for data to be uploaded as files + Return the server's response page. + """ + content_type, body = encode_multipart_formdata(fields, files) + h = httplib.HTTP(host) + h.putrequest('POST', selector) + h.putheader('content-type', content_type) + h.putheader('content-length', str(len(body))) + h.endheaders() + h.send(body) + errcode, errmsg, headers = h.getreply() + return h.file.read() + +def encode_multipart_formdata(fields, files): + """ + fields is a sequence of (name, value) elements for regular form fields. + files is a sequence of (name, filename, value) elements for data to be uploaded as files + Return (content_type, body) ready for httplib.HTTP instance + """ + BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$' + CRLF = '\r\n' + L = [] + for (key, value) in fields: + L.append('--' + BOUNDARY) + L.append('Content-Disposition: form-data; name="%s"' % key) + L.append('') + L.append(value) + for (key, filename, value) in files: + L.append('--' + BOUNDARY) + L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename)) + L.append('Content-Type: %s' % get_content_type(filename)) + L.append('') + L.append(value) + L.append('--' + BOUNDARY + '--') + L.append('') + body = CRLF.join(L) + content_type = 'multipart/form-data; boundary=%s' % BOUNDARY + return content_type, body + +def get_content_type(filename): + return mimetypes.guess_type(filename)[0] or 'application/octet-stream' + + +def is_image(name): + l = name.lower() + return l.endswith('jpg') or l.endswith('gif') or l.endswith('png') or l.endswith('jpeg') + +def upload_image(fname, data): + resp = post_multipart('dump.fm', '/upload/photo', + [['room','dumpfm']], + [['image', fname, data]]) + print resp + +def fetch_msg(): + pop_conn = poplib.POP3_SSL('pop.secureserver.net') + pop_conn.user('post@dump.fm') + pop_conn.pass_('UHR4Moghu5a2') + + numMessages = len(pop_conn.list()[1]) + if numMessages == 0: + print 'no messages' + return + msg_num = 1 + resp, msg_list, octs = pop_conn.retr(msg_num) + msg = "\n".join(msg_list) + parsed = parser.Parser().parsestr(msg) + + + print "Handling %s - %s:" % (parsed['from'], parsed['subject']) + + try: + for data in parsed.walk(): + fname = data.get_filename() + if fname and is_image(fname): + print 'uploading %s' % fname + image_data = data.get_payload(decode = 1) + upload_image(fname, image_data) + break + except Exception, e: + print e + print 'deleting msg #%s' % msg_num + pop_conn.dele(msg_num) + pop_conn.quit() + +while True: + time.sleep(5) + try: + fetch_msg() + except: + pass + + + + |
