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.

今晚大约十分钟,我以“ Get'er Done”的态度写了一些代码。 我们都这样做(我希望)。 它是一次性代码,可以解决一次性问题。 因此,它并不总是很漂亮,但通常很有趣。

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.

我有一个名为OverheardAtHome的愚蠢网站,该网站收集了我的孩子(和您的孩子们)在房子周围所说的愚蠢的名言和故事。 过去一年或更长时间,它一直在DasBlog上运行(就像该博客一样),但是填充该站点的纯粹工作流程却让人感到厌倦。 DasBlog并未设置用于筛选外部提交并将其提升为帖子的方式,我对以这种方式扩展DasBlog并不真正感兴趣。

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.

相反,我需要将OverheardAtHome转移到托管博客解决方案,最好是一个不错的免费解决方案,因为它不会赚钱。 这只是一种爱好。 我喜欢Tumblr,所以我想我把它放在那里了。 Tumblr有一个非常基本的HTTP API,并且其本机UI支持用户提交,因此看起来很成功。

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.

DasBlog每天不将其内容存储在数据库中,而是存储在XML文件中。 我有几百个XML文件,这些文件构成了OverheardAtHome的全部内容,这是非常基本的东西。

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."

这是我用(我相信) Jeremy“ madkidd” Hodges (我也是Graffiti CMS的开发人员)编写的CodePlexTumblr API编写的。 这是HttpWebRequest之上的一个很好的小抽象,它使用来自“ rakker”的一个HttpHelper类

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.

我认为我可以使用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();
}
}
}

Comments? What's a better pattern for left-hand/right-hand bulk crap like this, Dear Reader?

评论? 亲爱的读者,像这样的左手/右手大杂烩,什么是更好的模式?

翻译自: https://www.hanselman.com/blog/the-weekly-source-code-53-geter-done-edition-xml-in-the-left-hand-becomes-http-posts-in-the-right-hand

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐