X-Git-Url: https://git.sommitrealweird.co.uk/rss2maildir.git/blobdiff_plain/d3fb153e25a909bee388d63448063fc0f7650732..e4e1e5427c6afcedb1876b15694e5f473272e164:/rss2maildir.py?ds=sidebyside diff --git a/rss2maildir.py b/rss2maildir.py index a735d3a..968c647 100755 --- a/rss2maildir.py +++ b/rss2maildir.py @@ -11,10 +11,16 @@ import feedparser import email import datetime +import random +import string + +import socket from optparse import OptionParser from ConfigParser import SafeConfigParser +from base64 import b64encode + def parse_and_deliver(maildir, url, statedir): md = mailbox.Maildir(maildir) @@ -22,21 +28,27 @@ def parse_and_deliver(maildir, url, statedir): for item in fp["items"]: # things that we need in the message msg = email.message_from_string("") - msg.add_header("Subject", item["title"]) - msg.set_unixfrom("Brett Parker ") + msg.set_unixfrom("\"%s\" " %(url)) + msg.add_header("From", "\"%s\" " %(item["author"])) + msg.add_header("To", "\"%s\" " %(url)) msg.add_header("Date", datetime.datetime(*item["created_parsed"][0:6]).strftime("%a, %e %b %Y %T -0000")) - msg.add_header("To", url) - msg.set_payload(item["content"][0]["value"]) + msg.add_header("Subject", item["title"]) msg.set_charset("utf8") - msg.set_default_type("text/plain") + msg.set_default_type(item["content"][0]["type"]) + msg.set_type(item["content"][0]["type"]) + msg.set_payload(b64encode(item["content"][0]["value"])) + + msg.add_header("Message-ID", "<" + datetime.datetime.now().strftime("%Y%m%d%H%M") + "." + "".join([random.choice(string.ascii_letters + string.digits) for a in range(0,6)]) + "@" + socket.gethostname() + ">") - # open a temporary file in the maildir - fn = os.tempnam(os.path.join(maildir, "tmp")) + # start by working out the filename we should be writting to, we do + # this following the normal maildir style rules + fname = str(os.getpid()) + "." + socket.gethostname() + "." + "".join([random.choice(string.ascii_letters + string.digits) for a in range(0,10)]) + "." + datetime.datetime.now().strftime('%s') + fn = os.path.join(maildir, "tmp", fname) fh = open(fn, "w") fh.write(msg.as_string()) fh.close() # now move it in to the new directory - newfn = os.tempnam(os.path.join(maildir, "new")) + newfn = os.path.join(maildir, "new", fname) os.link(fn, newfn) os.unlink(fn)