#!/usr/bin/python # # Copyright (C) 2007 Robert Love # # py2blogger.py - posts pyblosxom files to blogger via gdata api # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. try: from xml.etree import ElementTree # for Python 2.5 users except: from elementtree import ElementTree from gdata import service import gdata import atom import time import os import sys import string # Your Google Account's Email Address EMAIL = 'foo@bar.com' # Your Google Account's Password PASSWD = 'mypassword' # Your Pyblosxom Files' Timezone, in the format [-]HH:MM TZ = '-04:00' # Publish all entries as drafts? DRAFT = 0 class BloggerPost: def __init__(self): self.service = service.GDataService(EMAIL, PASSWD) self.service.account_type = 'GOOGLE' self.service.source = 'rlove-py2blogger-1.0' self.service.service = 'blogger' self.service.server = 'www.blogger.com' self.service.ProgrammaticLogin() feed = self.service.Get('/feeds/default/blogs'); self.blog_id = feed.entry[0].GetSelfLink().href.split("/")[-1] def CreatePost(self, title, content, tm): entry = gdata.GDataEntry() entry.title = atom.Title('xhtml', title) entry.content = atom.Content('html', '', content) if (DRAFT == 1): entry.control = atom.Control() entry.control.draft = atom.Draft(text='yes') entry.published = \ atom.Published(time.strftime("%Y-%m-%dT%H:%M:%S.000", tm) + TZ) return self.service.Post(entry, '/feeds/' + self.blog_id + '/posts/default') def main(): if (len(sys.argv) < 2): print "%s " % sys.argv[0] sys.exit(0) client = BloggerPost() for file in sys.argv[1:]: f = open(file, 'r') title = string.strip(f.readline()) tm = time.localtime(os.fstat(f.fileno()).st_mtime) content = string.strip(f.read()) f.close() client.CreatePost(title, content, tm) print 'Successfully posted ' + str(len(sys.argv[1:])) + ' entries' if __name__ == '__main__': main()