using System;
using System.Xml;
using newtelligence.DasBlog.Runtime;
namespace OpinionatedGeek.Applications.WordPressToDasBlog
{
internal class Program
{
private const string ContentNamespace = "http://purl.org/rss/1.0/modules/content/";
private const string WordpressNamespace = "http://wordpress.org/export/1.0/";
private static int _entryIdCounter = 0;
private static void Main ()
{
IBlogDataService dataService = BlogDataServiceFactory.GetService (AppDomain.CurrentDomain.BaseDirectory + "\\content", null);
XmlDocument exported = new XmlDocument ();
exported.Load (@"..\..\wordpress.xml");
XmlNamespaceManager namespaces = new XmlNamespaceManager (exported.NameTable);
namespaces.AddNamespace ("content", ContentNamespace);
namespaces.AddNamespace ("wp", WordpressNamespace);
XmlNodeList items = exported.SelectNodes ("/rss/channel/item");
foreach (XmlNode item in items)
{
ImportBlogEntry (dataService, namespaces, item);
}
Console.In.ReadLine ();
return;
}
private static void ImportBlogEntry (IBlogDataService dataService, XmlNamespaceManager namespaces, XmlNode item)
{
DateTime postDate = DateTime.Parse (item ["pubDate"].InnerText);
string blogText = item ["content:encoded"].InnerText;
string blogTitle = item ["title"].InnerText;
string guid = item ["guid"].InnerText;
Entry entry = new Entry ();
entry.CreatedLocalTime = postDate;
entry.ModifiedLocalTime = postDate;
entry.Title = blogTitle;
entry.Content = blogText.Replace ("\r\n", "
");
// There seems to be a problem with dasBlog's entry lookup code. It HTML encodes the
// entry ID to do the lookup, but they're stored unencoded (as far as I can tell, which
// isn't very far). So, we need to use an entry ID which is the same when HTML encoded
// and unencoded. This seems to rule out the normal GUIDs that Wrodpress uses (which
// are just entry URLs). Let's keep it simple and use an increasing int counter.
//entry.EntryId = guid;
entry.EntryId = (++_entryIdCounter).ToString ();
string categories = "";
foreach (XmlNode categoryItem in item.SelectNodes ("category"))
{
categories += categoryItem.InnerText + ";";
}
categories = categories.Trim (';');
entry.Categories = categories;
entry.Author = "Paul";
entry.AllowComments = true;
dataService.SaveEntry (entry);
Console.Out.WriteLine ("Title: {0}", blogTitle);
Console.Out.WriteLine ("Date: {0}", postDate);
Console.Out.WriteLine ("Categories: {0}", categories);
Console.Out.WriteLine ("GUID: {0}", guid);
foreach (XmlNode commentNode in item.SelectNodes ("wp:comment", namespaces))
{
ImportComment (entry, dataService, commentNode);
}
return;
}
private static void ImportComment (Entry entry, IBlogDataService dataService, XmlNode commentNode)
{
DateTime commentDate = DateTime.Parse (commentNode ["wp:comment_date"].InnerText);
string commentText = commentNode ["wp:comment_content"].InnerText;
string commentAuthorName = commentNode ["wp:comment_author"].InnerText;
string commentAuthorEmail = commentNode ["wp:comment_author_email"].InnerText;
string commentAuthorHomepage = commentNode ["wp:comment_author_url"].InnerText;
string commentAuthorIPAddress = commentNode ["wp:comment_author_IP"].InnerText;
Comment comment = new Comment ();
comment.CreatedLocalTime = commentDate;
comment.ModifiedLocalTime = commentDate;
comment.TargetEntryId = entry.EntryId;
comment.TargetTitle = entry.Title;
comment.Author = commentAuthorName;
comment.AuthorEmail = commentAuthorEmail;
comment.AuthorHomepage = commentAuthorHomepage;
comment.AuthorIPAddress = commentAuthorIPAddress;
comment.Content = commentText;
Console.Out.WriteLine ("Comment Author: {0} ({1})", commentAuthorName, commentAuthorEmail);
Console.Out.WriteLine ("Comment IP/Home Page: {0} ({1})", commentAuthorIPAddress, commentAuthorHomepage);
Console.Out.WriteLine ("Comment Date: {0}", commentDate);
Console.Out.WriteLine ("Comment: {0}", commentText);
dataService.AddComment (comment);
return;
}
}
}