#!/usr/bin/python
# encoding: utf-8
from __future__ import with_statement
"""
post2blogger.py
Created by Jan Austin on 2009-03-02.
"""
import sys
import os
import getopt
import gdata
import atom
import subprocess
from BeautifulSoup import BeautifulSoup
from gdata import service
acc = 'xxx@gmail.com'
password = 'xxxxxx'
name = 'your name'
help_message = '''
post2blogger auto generate html from rest text file and post to blogger.
post2blogger -ftest.txt
'''
class BloggerOperation(object):
""" Provide blogger functions"""
def __init__(self,email,pw):
"""Creates a GDataService and provides ClientLogin auth details to it.
The email and password are required arguments for ClientLogin. The
'source' defined below is an arbitrary string, but should be used to
reference your name or the name of your organization, the app name and
version, with '-' between each of the three values."""
# Authenticate using ClientLogin.
self.service = service.GDataService(email, pw)
self.service.source = 'Austin-post2blogger-1.0'
self.service.service = 'blogger'
self.service.account_type = 'GOOGLE'
self.service.server = 'www.blogger.com'
self.service.ProgrammaticLogin()
# Get the blog ID for the first blog.
feed = self.service.Get('/feeds/default/blogs')
self_link = feed.entry[0].GetSelfLink()
if self_link:
self.blog_id = self_link.href.split('/')[-1]
def CreatePost(self, title, content, author_name, is_draft):
"""This method creates a new post on a blog. The new post can be stored as
a draft or published based on the value of the is_draft parameter. The
method creates an GDataEntry for the new post using the title, content,
author_name and is_draft parameters. With is_draft, True saves the post as
a draft, while False publishes the post. Then it uses the given
GDataService to insert the new post. If the insertion is successful, the
added post (GDataEntry) will be returned.
"""
# Create the entry to insert.
entry = gdata.GDataEntry()
entry.author.append(atom.Author(atom.Name(text=author_name)))
entry.title = atom.Title(title_type='xhtml', text=title)
entry.content = atom.Content(content_type='html', text=content)
if is_draft:
control = atom.Control()
control.draft = atom.Draft(text='yes')
entry.control = control
# Ask the service to insert the new entry.
return self.service.Post(entry,
'/feeds/' + self.blog_id + '/posts/default')
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def get_blogger_html(htmlfile):
"""docstring for get_blogger_html"""
with open(htmlfile) as f:
html = f.read()
soup = BeautifulSoup(html)
if soup.title:
title = soup.title.string
else:
title = soup.h1.string
print title
return title, unicode(soup.div)
def run(inputfile):
"""docstring for run"""
pwd = os.getcwd()
inputfile = os.path.join(pwd,inputfile)
print 'rest 2 html.....'
cmd = 'rst2htmlc.py %s test.html'%inputfile
subprocess.call([cmd],shell=True)
print 'rest 2 html done.'
print 'Get html snippet...'
title,content = get_blogger_html('test.html')
print 'Get html snippet done.'
print 'Connect blogger server.....'
blogger = BloggerOperation(acc,password)
print 'Connected blogger server.'
author_name = name
print 'Send content to blogger server...'
blogger.CreatePost(title,content.encode('utf-8'),author_name,True)
print 'All Done.'
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "hf:v", ["help", "file="])
except getopt.error, msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option == "-v":
verbose = True
if option in ("-h", "--help"):
raise Usage(help_message)
#if option in ("-f", "--file"):
# inputfile = value
if len(args) == 1:
print args
inputfile = args[0]
run(inputfile)
else:
Usage(help_message)
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\t for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())
post2blogger.py
標籤:
python
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言