Blogger - 自分のブログの全ての記事の取得 2008/04/05

ある程度、いろいろ記事がたまってきたので、自分のブログ記事から、いろいろ解析できないなかぁともくろみ中です。

これから3つステップに分けてとりくもうかなと。

  1. データの収集
  2. データの解析
  3. 可視化

Javaを使ってます。

Developer's Guide: Java - Blogger Data API - Google Code

自分のブログの全ての記事の取得を行います。
以前、ブログからブログへの記事の移行を行ったことがあるので、さっくと。

で、結果の並びかえしたかったのですが、どうもドキュメントみてるとこれは、APIを使う側で処理をするというのが方針のようですね。

Result ordering is up to the implementation.
Google Data APIs Protocol Reference - Google Data APIs - Google Code


ちなみにグーグルってデータベースのようなサービスって公開していましたっけ?
スプレッドシートとかはありますけど、なんかこの勢いだとデータベースのサービスがあってもいい感じなんですが、あるのでしょうか。

それで、できたらSQLではない言語で、問い合わせできて、えーと、あっ、使ったことないけど、アマゾンのSimpleDBみたいなものでもいいかも。


package mygg;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.google.gdata.client.GoogleService;
import com.google.gdata.client.Query;
import com.google.gdata.data.Category;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class Blogger {

public static GoogleService createGoogleService(String user, String password)
throws AuthenticationException {
GoogleService service = new GoogleService("blogger",
"deiji-blogger-app");
service.setUserCredentials(user, password);
return service;
}

public static List<Entry> allEntry(GoogleService service,String blogId)
throws IOException, ServiceException {

URL feedUrl = new URL(
"http://www.blogger.com/feeds/"+ blogId + "/posts/full");
Query query = new Query(feedUrl);

DateTime min = DateTime.parseDateTime("1999-12-31T23:59:59");
DateTime max = DateTime.now();
List<Entry> returnList = new ArrayList<Entry>();
int index = 1;// !!
do {
query.setUpdatedMin(min);
query.setUpdatedMax(max);
query.setStartIndex(index);
Feed resultFeed = service.query(query, Feed.class);
List<Entry> entries = resultFeed.getEntries();
if(!(0 < entries.size())) break;
index += entries.size();
returnList.addAll(entries);

} while (true);
return returnList;
}

public static void printAllPosts(GoogleService myService, String blogId)
throws ServiceException, IOException {

List<Entry> list = allEntry(myService, blogId);
// order older
Collections.reverse(list);
final String TAB = "\t";
for (Entry entry : list) {
System.out
.print(entry.getPublished() + TAB + entry.getId() + TAB + entry.getTitle().getPlainText());
Set<Category> set = entry.getCategories();
for (Category category : set) {
System.out.print(TAB+ category.getTerm());
}
System.out.println();

}

System.out.println();
}


}

: