Super simple tweet script in Ruby

I was trying to work out how to get my social network coverage of Skylines Australia and I decided I'd write a small script that would tweet any popular topics.

Obtaining the popular topics was a piece of pie, an alter to my topics table to flag a topic as 'already popular' and a query that grabbed any topics with more than X posts with the last post in the last 24hrs;

SELECT tid, title, posts FROM ibf_topics WHERE (last_post BETWEEN unix_timestamp()-14400 AND unix_timestamp()) AND posts > 50 AND sau_is_popular = 0 LIMIT 1

Easy enough.

But, tweeting them was more difficult. There are lots and lots of gems for Twitter but not all of them have been updated to work with Twitters OAuth. When I found one, the documentation was a little lacking and it was not clear how to obtain the credentials for the OAuth.

Thats when this awesome command line script came to my aid.

The end result is a simple little ruby script on a 4 hourly cron that tweets popular topics. Sweet!

require "rubygems"
require "mysql"
require "twitter"
require "cgi"
require "yajl"

dbh = Mysql.real_connect("localhost", "user", "pass", "db")

res = dbh.query("SELECT tid, title, posts FROM ibf_topics WHERE (last_post BETWEEN unix_timestamp()-14400 AND unix_timestamp()) AND posts > 50 AND sau_is_popular = 0 LIMIT 1")


Twitter.configure do |config|
    config.consumer_key = TWITTER_CONSUMER_KEY
    config.consumer_secret = TWITTER_CONSUMER_SECRET
    config.oauth_token = TWITTER_ACCESS_TOKEN
    config.oauth_token_secret = TWITTER_ACCESS_SECRET

client =

while row = res.fetch_hash do
    status = "Skylines Australia trending topic: "
    topic = CGI::unescapeHTML(row["title"])

    # We have 60 chars in our status, so that leaves us with up to 100 for the topic title
    if (topic.length > 100)
        topic = topic[0,97] + "..."

    status = status + topic + "" + row["tid"]

    # Post a status update

    dbh.query("UPDATE ibf_topics SET sau_is_popular = 1 WHERE tid = " + row["tid"])

Hopefully this helps you.

You can see the status updates at

By the way, I wrote a small URL shortener which helps also but you could use the api or something else if you'd like.

  • The nice topic really great information is shared..thanks for this useful post.

  • Social media network provides good plat foam that freely communicate to your friends and other people in all over the world so we not only communicate with other but also shear favorite website and links .

  • This is a great article.The written skill is so good.This article is the second part in a three part series.Thanks to share this blog with us.Keep it up.I will keep share in future.

  • Excellent shared article.I appreciate it a lot.Well Thanks for sharing such smart written article with us.Keep sharing as like this always.Perfect reference if you wanted start web design. Great article!

  • Great article! Thanks for this one. You did a great job detailing it.

blog comments powered by Disqus