每周源代码53 – “全神贯注”版本 – 左手的XML变成右手的HTTP POSTs

        [原文发表地址] The Weekly Source Code 53 - "Get'er Done" Edition - XML in the left hand becomes HTTP POSTs in the right hand

        [原文发表时间] 2010-06-26 12:01 AM

        今晚我全神贯注地花了十分钟的时间写了一段代码。我们都能这样做(我希望)。这是一次性的代码解决了一个过去的问题。因此,它并不总是完美,但它往往是有趣的。

        我找到了这个无聊的网站OverheardAtHome,它收集了我的孩子(和你们的孩子)经常在家周围讲的许多无聊的报导或故事。在过去一年或更久时间里网站运行在DasBlog上(就像这个博客),但该网站的纯粹的填充工作流程越来越烦人了。DasBlog并不是为了筛选外部的意见和促进他们的文章设置的,并且我对用那样方式来扩展DasBlog不感兴趣。

        相反,我需要把OverhearAtHome转移到托管博客解决方案,最好是一个不错的免费的,因为它不赚钱。它仅仅是一个兴趣爱好。我喜欢Tumblr所以我想把它放在那里。Tumblr有一个非常基本的HTTP API,并且其原生的用户界面支持用户提交,所以它看起来会是一个双赢。

        DasBlog 并不将它的内容存储在数据库中,而是在每日的一个XML文件里。我有几百个XML文件,构成了整个OverheardAtHome内容,这是非常基本的东西。

       下面是我写的,运用Jeremy“madkidd”Hodges写的Tumblr API from CodePlex,无独有偶,他也是Graffiti CMS 的开发人员。这是一个在HttpWebRequest的顶部的不错的小抽象化,它从“rakker”中运用了一些HttpHelper的类

       我想我可以使用PowerShell或是类似于脚本的工具来写,但这写起来很快。没有错误处理,但有趣的是,在数百篇文章中都没有错误。

 

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();

 

        }

 

    }

 

}

         评论?亲爱的读者,处理像这样的左手/右手大批量的数据的更好模式是什么呢?