I wrote some code tonight in about ten minutes with a "Get'er Done" attitude. We all do that (I hope). It's one-time code that will solve a one-time problem. As such, it isn't always pretty, but it often is interesting.
I've got this silly website called OverheardAtHome that is a collection of silly quotes and stories that my kids (and your kids) have said around the house. It was running on DasBlog (just like this blog) for the last year or more, but the sheer workflow of populating the site was getting tiredsome. DasBlog isn't setup for screening external submissions and promoting them to posts, and I wasn't really interested in extending DasBlog in that way.
Instead, I needed to move OverheardAtHome to a hosted blogging solution, preferably a nice free one as it doesn't make any money. It's just a hobby. I like Tumblr so I figured I put it there. Tumblr has a very basic HTTP API and their native UI supports User Submissions, so it seemed like a win.
DasBlog stores its content not in a database, but rather in an XML file per day. I've got a few hundred XML files that make up the whole of the content on OverheardAtHome and it's very basic stuff.
Here's what I wrote, using the Tumblr API from CodePlex written by (I believe) Jeremy "madkidd" Hodges, who is also a Developer on Graffiti CMS, coincidentally. It's a nice little abstraction on top of HttpWebRequest that uses a little HttpHelper class from "rakker."
I figured I could use PowerShell or something script-like, but this was very fast to write. There's no error handling, but interestingly (or not), there were no errors in hundreds of posts.
using System;using System.Linq;using System.IO;using System.Xml.Linq;using System.Threading;namespace TumblrAPI.ConsoleApp{ class Program { static void Main(string[] args) { Console.WriteLine("Started..."); TumblrAPI.Authentication.Email = "myemail@notyours.com"; // Console.ReadLine(); TumblrAPI.Authentication.Password = "poopypants"; //Console.ReadLine(); Console.WriteLine(TumblrAPI.Authentication.Authenticate().ToString()); if (TumblrAPI.Authentication.Status == TumblrAPI.AuthenticationStatus.Valid) { Console.WriteLine("Now make some posts..."); DirectoryInfo di = new DirectoryInfo(@"C:\overheardathome\xml"); //Get the DasBlog XML files, they are like <entry><title/><content/></entry> and stuff FileSystemInfo[] files = di.GetFileSystemInfos("*.dayentry.xml"); var orderedFiles = files.OrderBy(f => f.Name); XNamespace ns = "urn:newtelligence-com:dasblog:runtime:data"; foreach (FileSystemInfo file in orderedFiles) { XDocument xml = XDocument.Load(file.FullName); var posts = from p in xml.Descendants(ns + "Entry") select p; foreach (var post in posts) { TumblrAPI.Post.Text t = new TumblrAPI.Post.Text(); t.Title = (string)post.Element(ns + "Title"); t.Body = (string)post.Element(ns + "Content"); Thread.Sleep(500); //Tumblr will API limit me if I bash on them. Console.WriteLine(" Response from text post: {0}", t.Publish()); } } } Console.WriteLine("Done, press any key..."); Console.ReadLine(); } }}
Comments? What's a better pattern for left-hand/right-hand bulk crap like this, Dear Reader?
foreach (var file in orderedFiles) { dynamic doc = XDocument.Load(file.FullName).Root.ToElastic(); foreach (var entry in doc.entry_All) { TumblrAPI.Post.Text t = new TumblrAPI.Post.Text(); t.Title = ~entry.Title; t.Body = ~entry.Content; Thread.Sleep(500); //Tumblr will API limit me if I bash on them. Console.WriteLine(" Response from text post: {0}", t.Publish()); } }
var posts = new DirectoryInfo(@"C:\overheardathome\xml") .GetFileSystemInfos("*.dayentry.xml") .OrderBy(file => file.Name) .SelectMany(file => XDocument.Load(file.FullName).Root.Descendants(ns + "Entry")) .Select(entry => new TumblrAPI.Post.Text{ Title = (string)entry.Element(ns + "Title"), Body = (string)entry.Element(ns + "Content") });
var posts = from file in DirectoryInfo(@"C:\overheardathome\xml").GetFileSystemInfos("*.dayentry.xml") orderby file.Name from entry in XDocument.Load(file.FullName).Root.Descendants(ns + "Entry") select new TumblrAPI.Post.Text{ Title = (string)entry.Element(ns + "Title"), Body = (string)entry.Element(ns + "Content") };
var posts = from p in xml.Descendants(ns + "Entry") select p;
var posts = xml.Descendants(ns + "Entry");
Scott Hanselman's Productivity Tips Video
Scott at DevReach in Bulgaria in October
Developer Stand up Comedy - Coding 4 Fun
TechDays/DevDays Netherlands and Belgium:
Posts by Category Posts by Month
Greatest Hits Dev Tools List