summaryrefslogtreecommitdiff
path: root/scripts/emailposter.py
blob: d380ffda4ab73f2c86879df9afedf657cde6f7b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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