初衷
因为是技术博客,所以经常有代码。每次发篇有代码的博客,都要先将代码用Pygments转成HTML,然后将HTML拷到博客的HTML中,再继续编辑博客。这样 麻烦死了。再者,在网上编辑格式还是不如Python的RST直接写出来的方便,所以就直接用Python的RST来写博客文章,然后通过Google Blogger API发到 网上,这样方便多了。
这篇文章就是第一次采用blogspot这个程序来完成编辑、渲染和上传。
代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# utils.py
# author: rshen
# date: 2009-02-27
from gdata import service
import gdata
import atom
def post_blogspot(title, html_content, username, password):
blogger_service = service.GDataService(username, password)
blogger_service.source = 'blogup-app-1.0'
blogger_service.service = 'blogger'
blogger_service.account_type = 'GOOGLE'
blogger_service.server = 'www.blogger.com'
blogger_service.ProgrammaticLogin()
entry = gdata.GDataEntry()
entry.title = atom.Title("xhtml", title)
entry.content = atom.Content(content_type='html',
text=html_content.decode("utf-8"))
query = service.Query()
query.feed = '/feeds/default/blogs'
feed = blogger_service.Get(query.ToUri())
blog_id = feed.entry[0].GetSelfLink().href.split("/")[-1]
return blogger_service.Post(entry, '/feeds/%s/posts/default' % blog_id)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# blogup.py
# author: rshen
# date: 2009-02-27
from __future__ import with_statement
from docutils import core
from optparse import OptionParser
from rstdirective import *
from utils import *
import tempfile
import BeautifulSoup as bs
import sys
def to_html(rst_file, options):
f = open(rst_file)
contents = f.read()
parts = core.publish_parts(writer_name='html', source=contents)
html_content = unicode(parts['whole']).encode('utf-8')
tmp_file, tmp_path = tempfile.mkstemp()
with open(tmp_path, 'w') as tmp:
tmp.write(html_content)
print "HTML Contents written to %s" % tmp_path
return bs.BeautifulSoup(html_content)
def gen_blog(html_tree, options):
content = html_tree.find("div")
title = content.find("h1")
title.extract()
content.find("table", { "class" : "docinfo" }).extract()
sections = content.findAll("div", { "class" : "section" })
sections_html = ''.join([''.join([str(c) for c in sec.contents])\
for sec in sections])
return (''.join(title.contents), sections_html)
def upload_blog(title, html_content, options):
post_blogspot(title, html_content, options.username, options.password)
No comments:
Post a Comment