public class BloggerAutoPost {
static final String EMAILACCOUNT = "googleaccount_email";
static final String PASSWORD = "password";
GoogleService service;
String blogId;
Collection<Post> posts;
public static void main(String[] args) throws Exception {
BloggerAutoPost bloggerAutoPost = new BloggerAutoPost();
bloggerAutoPost.load();
bloggerAutoPost.login();
bloggerAutoPost.getBlog();
bloggerAutoPost.metaPost();
}
void load(){
//initialize collection of posts
}
void metaPost(){
for (Post post : posts){
post(post);
}
}
private void login() throws AuthenticationException {
service = new GoogleService("blogger", "test");
service.setUserCredentials(EMAILACCOUNT, PASSWORD);
}
private void post(Post post) throws Exception {
Entry myEntry = new Entry();
myEntry.setTitle(new PlainTextConstruct(post.title));
myEntry.setContent(new PlainTextConstruct(post.body));
Set categories = myEntry.getCategories();
Category category = new Category();
category.setTerm(post.category);
category.setLabel(null);
category.setScheme("http://www.blogger.com/atom/ns#");
category.setLabelLang(null);
categories.add(category);
// Ask the service to insert the new entry
URL postUrl = new URL("http://www.blogger.com/feeds/" + blogId + "/posts/default");
service.insert(postUrl, myEntry);
}
private void getBlog() throws MalformedURLException, IOException, ServiceException {
// Request the feed
final URL feedUrl = new URL("http://www.blogger.com/feeds/default/blogs");
Feed resultFeed = service.getFeed(feedUrl, Feed.class);
// Print the results
System.out.println(resultFeed.getTitle().getPlainText());
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
Entry entry = resultFeed.getEntries().get(i);
blogId = entry.getId().substring(entry.getId().indexOf("-", 30) + 1);
System.out.println("\t" + entry.getTitle().getPlainText());
System.out.println(blogId);
}
}
}
class Post {
String title;
String body;
String category;
public Post(String title, String body, String category) {
this.title = title;
this.body = body;
this.category = category;
}
}