[Tutor] IMAP2RSS

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Jun 7 14:04:44 EDT 2004



On Mon, 7 Jun 2004, Stella Rockford wrote:

> In constant pursuit of lowering the threshold to web publishing I have
> been experimenting with IMPA2RSS in python
>
> here is the script --->
> http://www.holgerarendt.de/journal/text/imap2rss.jsp
>
> right about line 58 of Holger Arendt's script I get an error
> ---------------------------------------------------------------
>
> pcp08247552pcs:~ stella$ /Users/stella/Desktop/IMAP2RSS
> Traceback (most recent call last):
>    File "/Users/stella/Desktop/IMAP2RSS", line 97, in ?
>      makeFeed()
>    File "/Users/stella/Desktop/IMAP2RSS", line 89, in makeFeed
>      mail = parse(data)
>    File "/Users/stella/Desktop/IMAP2RSS", line 58, in parse
>      os.remove(f);
> TypeError: coercing to Unicode: need string or buffer, file found
>
>
> ---------------------------------------------------------------
>
> any guidance or suggestions would be appreciated


Hi Stella,


The code appears to be buggy.  Its parse() method opens up a file as a
temporary buffer, and when it tries to remove it, the code passes the file
object 'f' to os.remove... but os.remove() takes in a file name, not the
file object:

    http://www.python.org/doc/lib/os-file-dir.html#l2h-1452


This should explain why we're getting the error message:

> TypeError: coercing to Unicode: need string or buffer, file found




Let's look at the function again:

###
def parse(maildata):
    filename="/tmp/imap2rss_fetch_"+str(os.getpid())
    f = open(filename,'w')
    f.write(maildata[0][1])
    f.close()
    f = open(filename,'r')
    m = rfc822.Message(f)
    data= {'From'    : m.getaddr('from'),
           'To'      : m.getaddrlist('to'),
           'Subject' : encode(m.getheader('subject')),
           'Date'    : m.getheader('date')}
    m.rewindbody()
    body=encode("<pre>")
    while 1:
        line = f.readline();
        if not line:
            body=body+encode("</pre>")
            break
        body = body+encode(line)
    data['Body']=body
    f.close()
    os.remove(f);
    return data
###


Ok, I see, so the code uses a temporary file just to feed into
rfc822.Message, since rfc822.Message expects a file object, not a string.
But rather than reinvent the wheel, the code may want to take advantage of
either the 'tempfile' module:

    http://www.python.org/doc/lib/module-tempfile.html

or 'StringIO':

    http://www.python.org/doc/lib/module-StringIO.html


In this case, StringIO looks like a promising module.  The code can be
simplified by using StringIO to put a file-like wrapper around the mail
string.


Here is a revised version of the code, replacing the temporary-file stuff
with StringIO stuff:

###
def parse(maildata):
    f = StringIO.StringIO(maildata[0][1])
    m = rfc822.Message(f)
    data= {'From'    : m.getaddr('from'),
           'To'      : m.getaddrlist('to'),
           'Subject' : encode(m.getheader('subject')),
           'Date'    : m.getheader('date')}
    m.rewindbody()
    body=encode("<pre>")
    while 1:
        line = f.readline();
        if not line:
            body=body+encode("</pre>")
            break
        body = body+encode(line)
    data['Body']=body
    return data
###


Let's try it.


###
>>> text = """From: dyoo at hkn.eecs.berkeley.edu
... To: john at doe.com
... Subject: where have all the cowboys gone?
...
... Hi John,
...
... Do you know where all the cowboys have gone?  I'm looking
... for them.  Thanks.
... """
>>> def encode(x): return x
...
>>> parse([['', text]])
{'Date': None, 'To': [('', 'john at doe.com')], 'Body': "<pre>Hi John,\n\nDo
you know where all the cowboys have gone?  I'm looking\nfor them.
Thanks.\n</pre>", 'From': ('', 'dyoo at hkn.eecs.berkeley.edu'), 'Subject':
'where have all the cowboys gone?'}
###


I just wrote some mock data (and a mock version of encode()) just to see
if parse() is doing something reasonable.  Looks functional!

But I can't seem to find the email address of the original author
of the code.  It would be great if we could send these corrections back to
Holger Arendt.


Good luck to you!




More information about the Tutor mailing list