Scheduling posts on your Ghost Blog

Datememe is a new 100% free online dating site. Just because it's free doesn't mean you can't enjoy premium features found on other paid websites. No fake users, no paid features, just free online chat to help you meet that someone special. What are you waiting for?



 

In a previous post we did a developer install of Ghost from git onto an Ubuntu VPS. Now let's get Ghost working in a more automated fashion and schedule future posts.

Ghost really needs post scheduling and they know it. It's not on the Planned Feature list yet but there is a trello card.

I don't think scheduling posts needs to be all that complicated and I can solve it for my needs by simplifying the problem.

The problem

Most blog editors follow a schedule like posting 1 or more articles per day at specific times so lets just build a quick script to solve that problem. I just want Ghost to automatically publish a post 2 times a day at 9, and 5 pm.

The solution

To accomplish this i'm going to update my content that's ready to be published with the tag "scheduled". I will then write a small javascript script that will run constantly on the server and publish these articles 3 times a day on schedule.

Publish Post Script

Here is a quick javascript snippet using the ghost api to query for draft posts with the tag scheduled and publish the post at the current time.

api.posts.browse({  
      filter: 'tags:scheduled',
      status: 'draft',
      include: 'tags',
      limit: '1',
      context: {
        internal: true
      }
    }).then(function(result) {
        //remove scheduled tag
        var tags = _.remove(result.posts[0].tags, function(tag) {
          return (tag.name !== 'scheduled');
        });
        //edit post params
        var post = {
          posts: [{
            published_at: new Date(),
            published_by: result.posts[0].author,
            status: "published",
            tags: tags
          }];
        }

        // publish post
        api.posts.edit(post, {
          id: result.posts[0].id,
          context: {
            user: result.posts[0].author
          }
        }).then(function(result) {
          console.log("published", config.getBaseUrl() + result.posts[0].url);
        });
      } else {
        console.log("no drafts found to post");
      }
    });

Based on our schedule we will call this function to publish the first draft found.

Build your schedule

My schedule is pretty simple I just need to publish 2 times a day using the servers timezone. My schedule snippet looks like this:

["at 9:00am","at 5:00pm"].forEach(function(item) {
  later.setInterval(publishPost, later.parse.text(item));
});

publish a post at 9am, 5pm every day.

If your having trouble defining your schedule let me know in the comments and I'll try to help.

Put it all together

Download jobs.js and modify it with your schedule and then put it in your ghost directory for ex:

/var/www/dm_ghost

Now you just need an upstart script to start your job server automatically when the server starts up:

description "ghost blog job server"  
start on started dm-ghost-blog  
stop on shutdown  
expect fork

setuid deploy  
env HOME="/var/www/dm_ghost"  
env NODE_ENV="production"  
env SPIN_SLEEP_TIME="60000"

chdir /var/www/dm_ghost

script exec   /var/www/dm_ghost/node_modules/forever/bin/forever start --spinSleepTime 30000 --pidFile /var/run/ghost-blog-job-server.pid -l /var/log/ghost-blog-job-server.log -a -d /var/www/dm_ghost/jobs.js  
end script  

place this file in:

/etc/init/dm_ghost.conf

Now you can manually start the job server:

sudo start dm_ghost_jobs

Automate all the things

Now you've got a javascript based job server with full access to the ghost api to allow you to get some automation going on your blog.

Here are a few ideas of things you might want to automate if you have a large blog:

  • automatic posting to social networks or pinging
  • notifications if no posts are scheduled
  • backup your ghost blog
  • run a grunt task to optimize images
  • run a link checker or html validator

I hope my guide helped you to get scheduled posts going on your blog. Please leave a comment and if you are having trouble automating some aspect of your blog and i'll try to help.

Datememe is a new 100% free online dating site. Just because it's free doesn't mean you can't enjoy premium features found on other paid websites. No fake users, no paid features, just free online chat to help you meet that someone special. What are you waiting for?