KBD

Keith Devens .com

Sunday, October 12, 2008 Flag waving
Your best? Losers always whine about their best. Winners go home and fuck the prom queen. – John Mason (The Rock)
← Red Tape RecordsThe simplest blog software? →

Daily link icon Monday, May 3, 2004

Python code to post to a Movable Type weblog by e-mail

My friend is going to be living in Thailand for the next year. The original purpose for creating his blog was so that he could have a place to keep a travel-log (with pictures) while he was in Thailand. Even though he's bringing a computer with him and will probably be able to get decent Internet access, we wanted to set him up with a way to blog from his phone. How cool would it be to be able to take a picture on his phone (OR on his camera which he can move to his phone since they use the same memory stick or whatever), e-mail it to his web site from his phone and have it show up on his blog in a few minutes? So, here's some Python code I wrote for him that enables just that:

Ok, that blew out my layout, so I've saved it to: http://keithdevens.com/software/misc/mail2blog.py

I took out some debugging code still in there currently, and obviously changed some things like his password and such. Note that there's one bit which I would have done differently if I had had Python 2.3 available (the splitting of slug and title). The code could be more robust in some obvious ways, but it works well enough for us for now, and I can tweak it as the need arises. If an e-mail is lost, it'll still be in his sent mail on his phone, so it's not a big deal.

The only problem we ran into was that, while it worked great for e-mails coming from his phone or from my e-mail program (Thunderbird), Mail.app on his Mac set the content-type on the attached images to application/applefile (WTF?) instead of image/jpeg (insert swearing here), and instead of sending normal text e-mails, it sent a multipart/alternative main part that had one part plain text and one part text/enriched. Anger!

But yeah, Python rocks. The built-in easy to use POP3 library, built-in easy to use e-mail/MIME parsing library, and built-in phenomenal XML-RPC client made this code easy. Oh, and this code is public domain, feel free to use/modify/etc. for your own (non-nefarious, I hope) purposes.

Update: It appears I'm participating in a meme. Here's an article about photoblogging and moblogging. Also, I just got a very pleasant and polite e-mail from August Trometer who's about to launch a MoBlogging service at http://BlogSnaps.com. Looks neat!

Update: My friend was able to force Mail.app to generate "normal" e-mails by changing some preferences. After that, e-mails he sent worked with the code I wrote.

← Red Tape RecordsThe simplest blog software? →

Comments XML gif

Ran (http://blogish.org) wrote:

Hey,

Cool script. I will probebly use it.
There is a much cleaner way to format the date. Try this:

import rfc822

date = "Fri, 07 May 2004 16:01:54 +0200" # this is what you get

newdate = rfc822.parsedate_tz(date)
newdate = int(time.mktime(newdate[0:9]))

# newdate is now a unix timetamp
print date
print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(date))

∴ Ran | 11-May-2004 3:54pm est | http://blogish.org | #4543

Keith (http://keithdevens.com/) wrote:

I dunno. It seems like it's essentially this:

date = rfc822.parsedate_tz(message['date'])
time.mktime(newdate[0:9])

vs this:

date = email.Utils.parsedate(message['date'])
date = list(date); date[8] = -1; date = tuple(d)
time.mktime(date)

I'm pretty sure you didn't need to use the int cast, but are you sure you needed the slice? Most importantly, are you sure that handles time zones correctly (actually, the problem I had was not with time zones as such, but with daylight savings time)? If I hadn't had to fix that daylight savings time problem my code would not have needed the middle line above. In any case, I'm not going to change it now since what I have now is tested.

Keith | 11-May-2004 4:37pm est | http://keithdevens.com/ | #4544

Ran wrote:

What you have now is greatSmiley

It's just that my host doesn't have

email.Utils
∴ Ran | 12-May-2004 6:29am est | #4552

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 7 posts

Recent comments XML

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.218s.

(Used 8 db queries)

mobile phone