Posted on 2008-05-12 22:04
ZelluX 阅读(1272)
评论(0) 编辑 收藏 所属分类:
Scripting
VIM Calender是个很好用的写日记的插件(
http://www.vim.org/scripts/script.php?script_id=52)
水木上的rmrf写了一个同步VIM Calender和Google Calender的脚本(
http://code.google.com/p/diaryvgc/downloads/list)
想到blogger.com支持通过发送邮件发布日志,于是我也写了个把VIM Calender中的日记发布到blogger.com的脚本。
这个脚本把发布情况记录在diary/poster.log中,以后每次执行只会发布最新的日志,同时考虑到当天的日记可能会被修改(blogger.com似乎不支持通过email修改日志),所以当天的日记不会被发布。
使用的时候修改开头几行的配置信息即可
#!/usr/bin/python
# A script for posting diaries created by VIM Calender to blogger.com
# Author: Wang Yuanxuan <zellux@gmail.com>
import smtplib, os, re, datetime
from email.mime.text import MIMEText
fromaddr = xxxxx@fudan.edu.cn'
toaddr = xxxx.xxxx@blogger.com'
smtpserver = 'mail.fudan.edu.cn'
diarydir = '/home/user_name/diary'
username = 'xxxxxx'
password = 'xxxxxx'
logpath = diarydir + '/poster.log'
def PostMail(title, content):
msg = MIMEText(content + '\r\n#end\r\n')
msg['Subject'] = title
msg['From'] = fromaddr
msg['To'] = toaddr
server = smtplib.SMTP(smtpserver)
server.login(username, password)
# server.set_debuglevel(1)
server.sendmail(fromaddr, [toaddr], msg.as_string())
server.quit()
# Load log file. Create a new one if not exist.
posted = []
if os.path.isfile(logpath):
temp = open(logpath, 'r')
posted = [line[:-1] for line in temp.readlines()]
log = open(logpath, 'a')
else:
print "A new poster log has been created at " + logpath
log = open(logpath, 'w')
pattern = r'(\d{4})/(\d{1,2})/(\d{1,2}).cal$'
scanner = re.compile(pattern)
for (top, dirname, filenames) in os.walk(diarydir):
for filename in filenames:
fullpath = os.path.join(top, filename)
if scanner.search(fullpath):
(year, month, day) = scanner.search(fullpath).groups()
filedate = datetime.date(int(year), int(month), int(day))
title = filedate.isoformat()
if filedate == datetime.date.today():
continue
if fullpath not in posted:
log.write(fullpath + '\n')
text = open(fullpath).read()
PostMail(title, text)
print 'The diary ' + title + ' has been posted'
log.close()