[util] AutoBlog

Executive / ADHD Summary:
Schedule a .Text post with this console application that reads saved blog BJD (BlogJet Draft) format files and posts them to your .Text weblog. Can be scheduled in task scheduler to run at whatever interval you choose. Posts only the oldest file in the directory, so you can write multiple posts and have them go out once per day.

This was posted with a new utility I just wrote, AutoBlog. I got the idea from Raymond Chen's pre-recorded blogs while he was on vacation. Since I've got two kids under the age of 4, I don't always get consistent free time to blog regularly (blame it on the youngsters, they can't defend themselves). I often have to save blog fragments until I get time to flesh them out, which means I then have a pile of posts all at once. I'd rather spread them out a bit, hence this tool.

This is a simple console application, all configuration is in the AutoBlog.exe.config file. You configure your .Text username, password, url, and a directory to post from. The app finds the oldest BJD file, parses it, and posts it using the SimpleBlogService webservice interface to your .Text blog.

I chose to use BlogJet files rather than write my own because (1) it has nice HTML editing features (2) I use it for my "normal" posts already, and (3) it has nice support for categories - it retrieves your category list and saves them out with the post file. AutoBlog supports post categories.

I was thinking about adding an optional footer to the config that would get appended to all autoblogged posts [Autoblogged] or something (I manually typed it at the end of this post). If anyone wants that I'll add it in.

How to use:
1. Download it.
2. Unzip to the directory of your choice.
3. Configure AutoBlog.exe.config.
4. Set up a Scheduled Task that points to AutoBlog.exe. The account will have to run as a user that has write permissions on the posts directory so it can rename the BJD file.
5. Be sure to test it - right click on the scheduled task and click "Run".

Code:

using System;
using
System.Configuration;
using
System.IO;

namespace
AutoBlog
{
   
class
Console
    {
        [STAThread]
       
static void Main(string
[] args)
        {
           
string
username = ConfigurationSettings.AppSettings["Username"];
           
string
password = ConfigurationSettings.AppSettings["Password"];
            System.DateTime fileTimestamp = System.DateTime.MaxValue;
           
string
filename = "";
           
string
[] categoryArray;
           
string
title;
           
string
body;

            DirectoryInfo di =
new
DirectoryInfo(ConfigurationSettings.AppSettings["PostsDirectory"]);
            FileInfo[] files = di.GetFiles("*.bjd");

           
foreach(FileInfo fi in
files)
            {
               
if
(fi.LastWriteTime<fileTimestamp)
                {
                    fileTimestamp = fi.LastWriteTime;
                    filename = fi.FullName;
                }   
            }

           
if
(filename != "")
            {
               
using (StreamReader sr = new
StreamReader(filename))
                {
                    sr.ReadLine();
//**BLOGJET POST**

                    sr.ReadLine();
//Blog name

                    title = sr.ReadLine();
                    categoryArray = sr.ReadLine().Replace("\"","").Split(',');
                    sr.ReadLine();
// -

                    body = sr.ReadToEnd();
                }

                net.asp.weblogs.SBSSimpleBlogService webService =
new
AutoBlog.net.asp.weblogs.SBSSimpleBlogService();
                webService.Url = ConfigurationSettings.AppSettings["ServiceUrl"];

               
//Rename the file first, since this is most likely to fail as a scheduled task
                System.IO.File.Move(filename,filename + ".bak");

                try
                {
                    if (categoryArray.Length > 0)
                        webService.InsertCategoryPost(username,password,System.DateTime.Now,title,body,categoryArray);
                    else
                        webService.InsertPost(username,password,System.DateTime.Now,title,body);
                }
                catch
                {
                    //Post failed - rename back to BJD extension
                    System.IO.File.Move(filename + ".bak",filename);
                }
            }
        }
    }
}

UPDATE:
I've changed the order so the rename is done before the blog is posted. That's for two reasons: (1) it's more likely to fail when run as a scheduled task, and (2) if it fails, the same thing will get posted again and again. I figure it's better to have nothing posted than duplicate posts.

[Autoblogged] 

No Comments