| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import mimetypes |
|---|
| 4 | import httplib |
|---|
| 5 | import email |
|---|
| 6 | import email.Header |
|---|
| 7 | import urllib |
|---|
| 8 | import time |
|---|
| 9 | import base64 |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | def get_parts(msgbody): |
|---|
| 13 | """ |
|---|
| 14 | Find out body,attached image, and return them. |
|---|
| 15 | msgbody : mime message of email |
|---|
| 16 | """ |
|---|
| 17 | |
|---|
| 18 | msg = email.message_from_string(msgbody) |
|---|
| 19 | |
|---|
| 20 | subj_l = email.Header.decode_header(msg.get('subject'))[0] |
|---|
| 21 | enc = 'us-ascii' |
|---|
| 22 | if subj_l[1]: |
|---|
| 23 | enc = subj_l[1] |
|---|
| 24 | subj = unicode(subj_l[0],enc).encode('utf-8') |
|---|
| 25 | |
|---|
| 26 | body = "" |
|---|
| 27 | images = [] |
|---|
| 28 | for part in msg.walk(): |
|---|
| 29 | if part.get_content_maintype() == 'multipart': |
|---|
| 30 | continue |
|---|
| 31 | tp = part.get_content_type() |
|---|
| 32 | if not body and tp.find('text') != -1: |
|---|
| 33 | # body like part was found !! |
|---|
| 34 | body = part |
|---|
| 35 | continue |
|---|
| 36 | if tp.find('image') != -1: |
|---|
| 37 | # Image found! |
|---|
| 38 | images.append(part) |
|---|
| 39 | return body, subj,images |
|---|
| 40 | |
|---|
| 41 | def encode_multipart_formdata(fields, images): |
|---|
| 42 | """ |
|---|
| 43 | fields is a sequence of (name, value) elements for regular form fields. |
|---|
| 44 | files is a sequence of (name, filename, value) elements for data to be uploaded as files |
|---|
| 45 | Return (content_type, body) ready for httplib.HTTP instance |
|---|
| 46 | """ |
|---|
| 47 | BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$' |
|---|
| 48 | CRLF = '\r\n' |
|---|
| 49 | L = [] |
|---|
| 50 | for (key, value) in fields.iteritems(): |
|---|
| 51 | L.append('--' + BOUNDARY) |
|---|
| 52 | L.append('Content-Disposition: form-data; name="%s"' % key) |
|---|
| 53 | L.append('') |
|---|
| 54 | L.append(str(value)) |
|---|
| 55 | for image in images: |
|---|
| 56 | filename = image.get_filename() |
|---|
| 57 | content_type = image.get_content_type() |
|---|
| 58 | L.append('--' + BOUNDARY) |
|---|
| 59 | L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (filename, filename)) |
|---|
| 60 | L.append('Content-Type: %s' % content_type) |
|---|
| 61 | L.append('') |
|---|
| 62 | L.append(image.get_payload(decode=1)) |
|---|
| 63 | L.append('--' + BOUNDARY + '--') |
|---|
| 64 | L.append('') |
|---|
| 65 | body = CRLF.join(L) |
|---|
| 66 | content_type = 'multipart/form-data; boundary=%s' % BOUNDARY |
|---|
| 67 | return content_type, body |
|---|
| 68 | |
|---|
| 69 | def get_content_type(filename): |
|---|
| 70 | return mimetypes.guess_type(filename)[0] or 'application/octet-stream' |
|---|
| 71 | |
|---|
| 72 | def add_moblog_entry(msgbody, host, blog, auth, password): |
|---|
| 73 | """ |
|---|
| 74 | Add entry from mail |
|---|
| 75 | |
|---|
| 76 | msgbody : This argument will be mime message of one email. |
|---|
| 77 | site : |
|---|
| 78 | blog : |
|---|
| 79 | auth : |
|---|
| 80 | password : |
|---|
| 81 | blogbase : This argument will be url for blog. |
|---|
| 82 | Authentication informations(user/pass) must be included. |
|---|
| 83 | """ |
|---|
| 84 | |
|---|
| 85 | body, subj, images = get_parts(msgbody) |
|---|
| 86 | |
|---|
| 87 | if not body: |
|---|
| 88 | # no body was found |
|---|
| 89 | return |
|---|
| 90 | |
|---|
| 91 | body_str = body.get_payload(decode=1) |
|---|
| 92 | if body_str.find(password) != 0: |
|---|
| 93 | return |
|---|
| 94 | else: |
|---|
| 95 | body_str = body_str.replace(password,'') |
|---|
| 96 | while body_str[0] == '\n': |
|---|
| 97 | body_str = body_str[1:] |
|---|
| 98 | body_str = body_str.split('---')[0] |
|---|
| 99 | body_str = unicode(body_str,'iso-2022-jp').encode('utf-8') |
|---|
| 100 | |
|---|
| 101 | fields = {'body': body_str, 'title': subj} |
|---|
| 102 | content_type, body = encode_multipart_formdata(fields, images) |
|---|
| 103 | |
|---|
| 104 | h = httplib.HTTP(host) |
|---|
| 105 | h.putrequest('POST', blog + "/add_moblog_entry") |
|---|
| 106 | h.putheader('content-type', content_type) |
|---|
| 107 | h.putheader('content-length', str(len(body))) |
|---|
| 108 | authstr = 'Basic ' + base64.encodestring(auth).strip() |
|---|
| 109 | h.putheader('Authorization', authstr) |
|---|
| 110 | h.endheaders() |
|---|
| 111 | h.send(body) |
|---|
| 112 | errcode, errmsg, headers = h.getreply() |
|---|
| 113 | |
|---|
| 114 | def main(): |
|---|
| 115 | |
|---|
| 116 | # |
|---|
| 117 | # Please set some information,(host of mail server,etc) to use |
|---|
| 118 | # |
|---|
| 119 | |
|---|
| 120 | import poplib |
|---|
| 121 | s = poplib.POP3('mailhost') |
|---|
| 122 | s.user('pop3user') |
|---|
| 123 | s.pass_('pop3pass') |
|---|
| 124 | l = s.list() |
|---|
| 125 | if len(l) and l[1]: |
|---|
| 126 | m = s.retr(1) |
|---|
| 127 | |
|---|
| 128 | host = "example.com" |
|---|
| 129 | blog = "/coreblog" |
|---|
| 130 | auth = "user:pass" |
|---|
| 131 | password = "moblog_password" |
|---|
| 132 | msgbody = '\n'.join(m[1]) |
|---|
| 133 | add_moblog_entry(msgbody, host, blog, auth, password) |
|---|
| 134 | |
|---|
| 135 | s.dele(1) |
|---|
| 136 | |
|---|
| 137 | s.quit() |
|---|
| 138 | |
|---|
| 139 | return |
|---|
| 140 | |
|---|
| 141 | if __name__ == '__main__': |
|---|
| 142 | main() |
|---|