How to post to blogger through gdata API (example in Scala Language)

In some earlier posts, I was ranting about the inadequacies of the blogger wysiwyg interface.
Actually, I have ranted a lot about this system in the last couple of days.
So, the next logical step was to create a simple blog client that could connect with the blogger/gdata API.
It actually wasn't that complex. Envision an API for posting a blog entry to blogger, and
the operations shall include: Authenticating to the service, provide a post URL to connect to,
provide the actual content and title to post. That is basically all that is required to use
the API. The documentation does a much better job of explaining the overall functionality
available through the service. And it is freely available and fairly easy to use. I created
the source below in a couple of hours. When using the API, just make sure to secure
your google username and password as it is probably use to access all of the google services.


Most of the scala source can be found below.



class ContentReader(filename: String) {
import java.io._
def readFile(): (String, String) = {
val file = Source.fromFile(filename)
var counted = file.getLines.counted
val fileData = new StringBuilder()
var title = ""
counted.foreach { (line: String) =>
if (counted.count == 0) {
title = line.substring(6).trim()
} else {
fileData.append(line)
}
}
(title, fileData.toString())
}
} // End of Class //




def createPost(service: GoogleService, blogid: String, title: String, content: String,
authorName: String, userName: String): Unit = {

// Create the entry to insert
val myEntry = new Entry()
myEntry.setTitle(new PlainTextConstruct(title))
myEntry.setContent(new PlainTextConstruct(content))
val author = new Person(authorName, null, userName)
myEntry.getAuthors().add(author);
// Ask the service to insert the new entry
val postUrl = new URL("http://www.blogger.com/feeds/" + blogid + "/posts/default")
val res = service.insert(postUrl, myEntry)
Console.println("INFO: create post");
}
def publishEntry(googleUsername: String, googlePwd: String): GoogleService = {
val myService = new GoogleService("blogger", applicationClientId)
myService.setUserCredentials(googleUsername, googlePwd)
return myService
}

Comments

Popular posts from this blog

On Unit Testing, Java TDD for developers to write

Is Java the new COBOL? Yes. What does that mean, exactly? (Part 1)

JVM Notebook: Basic Clojure, Java and JVM Language performance