Restoring/Importing From BlogML

Topics: Developer Forum
Aug 3, 2007 at 10:51 AM
I used the Atom and GData facility in this program to import a BlogML file I had exported from MSN Spaces.
Grab the BlogML.dll and add it as a reference from the current release (http://www.codeplex.com/BlogML)

Put this code under a button or something.
It shows a filedialog, loads the file using BlogMLs library
gets the service feed URL (copy pasted from other functions)
fills an AtomEntry object
calls _service.Insert
then loops 50 times

I made an attempt to update the My.Settings.LastRestoredPostCount counter although without taking a closer look at the design of you app the function is poorly integrated with the rest of the app. I simply used it to import data I had. Though I'd post the function thinking it might help out you to add BlogML support the application.

Me.OpenFileDialog1.InitialDirectory = Me.BackupPathFolderPicker.SelectedPath

If Me.OpenFileDialog1.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
Dim x As New StreamReader(Me.OpenFileDialog1.FileName)
'handle loading BlogML file for restore
Dim Blog As New BlogML.Xml.BlogMLBlog
'attempt deserialization of BlogML file
Try
Blog = BlogML.Xml.BlogMLSerializer.Deserialize(New StringReader(x.ReadToEnd()))
Catch ex As Exception
Throw New Exception("Couldn't load your BlogML file. Maybe it is wellformedness")
End Try

'loop here to add in entries up to 50 i guess
Dim RestoreCount As Integer = My.Settings.LastRestoredPostCount

'get service feed url for blog we are posting to
Dim url As String = ""
Dim entry As BlogComboBoxItem = CType(Me.BlogsComboBox.SelectedItem, BlogComboBoxItem)
For Each link As AtomLink In entry.BlogAtomEntry.Links
If link.Rel = BaseNameTable.ServiceFeed Then
url = link.HRef.ToString()
Exit For
End If
Next
'loop through each post in the BlogML file and try to post skip ones we posted before

'My.Settings.LastRestoredPostCount
Do Until RestoreCount = 50
Dim post As BlogML.Xml.BlogMLPost = Blog.Posts(RestoreCount)
'For Each post As BlogML.Xml.BlogMLPost In Blog.Posts
Dim RestorePost As New AtomEntry
'fill in AtomEntry
RestorePost.Title.Text = post.Title
RestorePost.Content = New AtomContent()
RestorePost.Content.Content = post.Content.UncodedText
RestorePost.Content.Type = "html"
RestorePost.Updated = post.DateModified
RestorePost.Published = post.DateCreated

For Each author As BlogML.Xml.BlogMLAuthorReference In post.Authors
RestorePost.Authors.Add(New AtomPerson(AtomPersonType.Author, author.Ref))
Next

For Each category As BlogML.Xml.BlogMLCategoryReference In post.Categories
RestorePost.Categories.Add(New Google.GData.Client.AtomCategory(category.Ref))
Next

StatusUpdate("Posting to Blogger")
Try
_service.Insert(New Uri(url), RestorePost)

Catch ex As Exception
If MsgBox(ex.Message, MsgBoxStyle.OkCancel, "Error Posting to Blogger") = MsgBoxResult.Cancel Then
StatusUpdate("Error : " & ex.Message)
Exit Do
End If

End Try
RestoreCount += 1
Loop
TotalDownloaded(RestoreCount)
My.Settings.LastRestoredPostCount = RestoreCount
SaveSetting("BloggerBackup", "LastRestoredPostCount", url, My.Settings.LastRestoredPostCount)
StatusUpdate("Done")
End If







If someones wondering how i exported a BlogML file from windows livespaces. I used LiveSpace.BlogML (also part of the release mentioned above) heres the code i used, referencing BlogML, LiveSpace.BlogML, CookComputing.XMLRPCV2

LiveSpaceBlogMLWriter ml = new LiveSpaceBlogMLWriter(txtBlogName.Text, txtSecretWord.Text);
ml.PostCount = 100; //set this high which caused alot of exceptions, worked anyway but it wrote alot of blank posts
XmlWriter xw = XmlWriter.Create("test.xml"); //file to save entries too
ml.Write(xw);
xw.Close();


Anyone wishing to contact me do so at colindm"standardemailaddressAtsign"hotmail.com
Coordinator
Aug 3, 2007 at 1:58 PM
Edited Aug 3, 2007 at 1:58 PM
Thanks.

Yeah, I'm not happy with how I implemented the LastRestoredPostCount. I was trying something out and it just didn't work as I wanted it too. So while it works, it's cheesy and smells pretty bad...

I'm using it as a sort of fake global variable... The actual restored count for a blog is stored in the registry. When the user selects which of their blogs they want to use, I then update My.Settings.LastRestoredPostCount with the value in the registry for that specific blog.

This was an experiment and one I don't think went all that well. I'm going to re-implement/fix it in a future version. All the settings need to be integrated into a single scheme. Right now I'm using a couple (registry for blog specific settings, My.Settings for other settings, etc)

Anyway, thanks for posting this. It's very cool to see that I could easily add restoring from BlogML files to the Restore Posts feature.

Thanks again,
Greg