Wednesday, April 11, 2007

Porting my blog

As you may have noticed, most of the entries from my old blog are in here - and yet, I'm running Blogger. No, I'm far too lazy to type them all in - instead, I used Google's C# API to pull my posts from the database and post them. I ran into a few issues; Google have a rate-limiting feature (to stop spammers, a noble goal) that cut me off a few times mid-post, and I forgot to flag which posts I'd added the first time and ended up having to delete a lot of stuff and repost it.

You can get the API from this site: http://code.google.com/p/google-gdata/

My import code is neither pretty nor very well written, but if it helps anyone here are some snippets: (Note that the URI is the address of the atom feed of your blog)

To Retrieve a Feed (all entries are in Feed.Entries)
private static AtomFeed getFeed(out Service service)
{
FeedQuery query = new FeedQuery();
service = new Service("blogger", "DataMigrate");
NetworkCredential nc = new NetworkCredential("user", "pass");
service.Credentials = nc;
query.Uri = new Uri("URL");
query.NumberToRetrieve = 500;

AtomFeed feed = service.Query(query);
return feed;
}


To delete an entry
service.Delete(e);

To add an entry
static void InsertEntry(string title, string body, DateTime date)
{
FeedQuery query = new FeedQuery();
Service service = new Service("blogger", "MyProgramName");
NetworkCredential nc = new NetworkCredential("user", "pass");
service.Credentials = nc;
query.Uri = new Uri("URL");

AtomEntry entry = new AtomEntry();
entry.Content.Content = body;
entry.Content.Type = "html";
entry.Title.Text = title;
entry.Published = date;
service.Insert(query.Uri, entry);
}


Seems to have worked!

No comments: