#!/bin/python
from gdata import service
import gdata
import atom
import sys
import popen2
import string
import re
post_id = re.compile('.*post-([0-9]+)')
class Blogger :
def __init__(self, username, password, blog_id=None) :
self.service = service.GDataService(username, password)
self.service.source = 'Plan9 BloggerFS'
self.service.service = 'blogger'
self.service.account_type = 'GOOGLE'
self.service.server = 'www.blogger.com'
self.service.ProgrammaticLogin()
self.blogs = {}
self.posts = {}
self.title = ''
self.blog_id = blog_id
def bloglist(self) :
if len(self.blogs) > 0 :
return self.blogs
query = service.Query()
query.feed = '/feeds/default/blogs'
feed = self.service.Get(query.ToUri())
self.blogs = {}
self.title = feed.title.text
for entry in feed.entry:
self.blogs[entry.GetSelfLink().href.split("/")[-1]] = entry.title.text
return self.blogs
def postlist(self) :
feed = self.service.GetFeed('/feeds/' + self.blog_id + '/posts/default')
self.posts = {}
for entry in feed.entry:
m = post_id.match(entry.id.text)
if(m) :
self.posts[m.group(1)] = entry.title.text
return self.posts
def post(self, entry) :
self.service.Post(entry, '/feeds/%s/posts/default' % self.blog_id)
def put(self, entry) :
self.service.Put(entry, entry.GetEditLink().href)
def entry(self, id) :
query = service.Query()
query.feed = '/feeds/%s/posts/default/%s' % (self.blog_id, id)
return self.service.Get(query.ToUri())
class Post :
def __init__(self, fd=None, entry=None) :
if fd :
self.read(fd)
elif entry :
self.entry = entry
self.title = entry.title.text
self.tags = [e.term for e in entry.category]
if self.entry.control :
self.draft = True
else :
self.draft = False
self.content = entry.content.text
else :
self.title = ''
self.tags = ()
self.draft = True
self.content = ''
def fill_entry(self) :
self.entry.title = atom.Title('xhtml', self.title)
self.entry.content = atom.Content(content_type='html', text=self.content)
self.entry.category = []
for tag in self.tags : # from http://code.activestate.com/recipes/576441/
category = atom.Category(term=tag, scheme="http://www.blogger.com/atom/ns#")
self.entry.category.append(category)
if self.draft :
control = atom.Control()
control.draft = atom.Draft(text='yes')
self.entry.control = control
def newentry(self) :
self.entry = gdata.GDataEntry()
self.fill_entry()
return self.entry
def read(self, fd) :
content = fd.readlines()
hdrs = 1
self.draft = False
self.tags = ()
while hdrs :
hdrs = 0
if len(content[0]) > 5 and content[0][0:6].lower() == 'title:' :
self.title = content[0][6:].strip()
content = content[1:]
hdrs = 1
if len(content[0]) > 5 and content[0][0:6].lower() == 'draft:' :
# if draft: is present treat it as a draft unless is says draft: no
self.draft = content[0][6:].strip().lower() != 'no'
content = content[1:]
hdrs = 1
if len(content[0]) > 4 and content[0][0:5].lower() == 'tags:' :
self.tags = [t.strip() for t in content[0][6:].strip().lower().split(',')]
content = content[1:]
hdrs = 1
self.content = string.join(content, "").strip()
def update(self, fd) :
self.read(fd)
self.fill_entry()
return self.entry
def __str__(self) :
if self.draft :
d = 'Draft: yes\n'
else :
d = ''
return "Title: %s\n%sTags: %s\n%s" % (self.title, d, string.join(self.tags, ", "), self.content)
def getpw(u) :
(io, err) = popen2.popen4(('getuserpasswd', 'proto=pass server=www.google.com user=%s' % u))
return io.read()
if len(sys.argv) > 3 :
blogger = Blogger(sys.argv[1], getpw(sys.argv[1]), sys.argv[2])
if sys.argv[3] == "new" :
post = Post(fd=sys.stdin)
blogger.post(post.newentry())
elif sys.argv[3] == "posts" :
for p in blogger.postlist() :
print "%s\t%s" % (p, blogger.posts[p])
elif sys.argv[3] == "update" :
post = Post(entry=blogger.entry(sys.argv[4]))
blogger.put(post.update(sys.stdin))
elif sys.argv[3] == "read" :
if len(sys.argv) > 3 :
post = Post(entry=blogger.entry(sys.argv[4]))
print post
else :
blogger = Blogger(sys.argv[1], getpw(sys.argv[1]))
if sys.argv[2] == "bloglist" :
for b in blogger.bloglist() :
print "%s\t%s" % (b, blogger.blogs[b])
|