dp.cx blog

Posted on

Filed under programming, jekyll, and ruby

As a similar plugin to PubSubHubbub, I wanted to be able to have Jekyll automatically tweet whenever a new post occurs. So I wrote the code below:

 
module Jekyll
    class TweetGenerator < Generator
        require "rubygems"
        require "twitter"
 
        Twitter.configure do |config|
            config.consumer_key        = ''
            config.consumer_secret     = ''
            config.oauth_token        = ''
            config.oauth_token_secret = ''
        end
 
        def generate(site)
            post = site.posts.reverse[0]
 
            if ! post.data.has_key?('tweet_id')
                post_tweet(post)
            else
                puts "#{post.data['title']} has already been tweeted"
            end
        end
 
        def post_tweet(post)
            tags = post.data['tags']
            tags.collect! {|x| "#" + x }
 
            tweet = "#{post.data['title']} #{tags.join(" ")} http://www.dp.cx/blog/#%7Bpost.url%7D"%3C/span%3E
            puts "Tweeting #{tweet}"
 
            tweet_results = Twitter.update(tweet)
            puts "Please update the YAML front-matter for #{post.data['title']} to include 'tweet_id: #{tweet_results['id_str']}"  
        end
    end
end
 

If you want to use it, you'll need to create an app within Twitter, and get oauth keys. That step is left as an exercise to the reader.

The only current downside to this is that you'll have to manually update the YAML front-matter with the new tweet_id so that the plugin will know not to tweet about this again.