旧ブログからの引っ越し その2 blogger(GoogleAPI) 2006/11/26
2008/04/05

旧ブログからデータを移行させる試みです。

旧ブログからgoogleApiを使用して、一気にデータを取得する方法をみつけきれなかったので、ちょっと工夫して取得してみました。
短いコードですが、ここまでたどりつくのに、時間がかかってしまいまいた。

実行すると簡単にデータを移行できました。

工夫した点:apiを利用してデータを取得する場合、取得する上限あるようにみえるの(25?)なので、時間指定(min,max)を使用して細かくデータを取得する方法をとった。

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

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

public class TestBlogger5 {

public static void main(String[] args) throws IOException, ServiceException {
newBlog(getAllEntry());
}

public static List<entry> getAllEntry() throws IOException,
ServiceException {

URL feedUrl = new URL("http://www.blogger.com/feeds/old-blog-id/posts/full");
Query query = new Query(feedUrl);
GoogleService service = new GoogleService("blogger", "getAllEntry.app");
service.setUserCredentials("blogger id", "password");

DateTime min = DateTime.parseDateTime("2005-12-31T23:59:59");
// DateTime max = DateTime.now();
DateTime max = DateTime.parseDateTime("2006-10-24T23:59:59");
List<entry> returnList = new ArrayList<entry>();
do {
query.setUpdatedMin(min);
query.setUpdatedMax(max);
Feed resultFeed = service.query(query, Feed.class);

if (!(0 <> list = resultFeed.getEntries();
for (Entry entry : list) {
// thid code need?
max = new DateTime(entry.getPublished().getValue());
System.out.println(entry.getId());
System.out.println(max);
}
returnList.addAll(list);

} while (true);
return returnList;
}

public static void newBlog(List<entry> list) throws IOException,
ServiceException {
URL postUrl = new URL("http://www.blogger.com/feeds/new-blog-id/posts/default");

GoogleService myService = new GoogleService("blogger",
"add.new.blog.app");
myService.setUserCredentials("google-id", "password");
Person author = new Person("your name", null, "google-id");

int cnt = 0;
for (Entry entry : list) {
entry.getAuthors().add(author);
Entry insertedEntry = myService.insert(postUrl, entry);
cnt += 1;
System.out.println("o_o>>" + cnt);
}
}
}


いまさらながら、apiが公開されているのは便利ですね。

: