Ruby Script to count a twitter hashtag and save the result and last_tweet_id to hashtag_counter_result.txt and last_tweet_id.txt.

Gist: https://gist.github.com/kuntoaji/589329


#!/usr/bin/env ruby

# Twitter Hashtag Counter
#
# Requirements: twitter gem.
# To install twitter gem: [sudo] gem install twitter
require 'rubygems'
require 'twitter'
require 'optparse'

options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby hashtag_counter.rb [options] [arguments]\n"
opts.banner += "Example: ruby hastag_counter.rb -s mytag -d 2010-09-21"

opts.on("-s", "--search [TAG]", "search tag. ie. mytag, will generate #mytag automatically") do |tag|
options[:search] = tag
end

opts.on("-d", "--since-date [YYYY-MM-DD]", "since date. ie. 2010-09-21") do |date|
options[:date] = date
end
end.parse!


tag = options[:search]
date = options[:date]
last_tweet_id = nil
result = 0
puts "Count hashtag: ##{tag}"
twitter_search = Twitter::Search.new.clear.hashed(tag).since_date(date).per_page(50)

# search since last_tweet_id from last_tweet_id.txt
# last_tweet_id.txt only contains one line with one tweet id
filename = 'last_tweet_id.txt'

if File.exist?(filename)
file = File.open(filename, 'r')
last_tweet_id = file.read
file.close

twitter_search.since("#{last_tweet_id}") if !last_tweet_id.nil? or !last_tweet_id.empty?

# get the last result from hashtag_counter_result.txt
filename = 'hashtag_counter_result.txt'

if File.exist?(filename)
file = File.open(filename, 'r')
result = file.read.to_i
file.close
end
end

begin
# get the last tweet id
# if last_tweet_id == nil, then everything is up-to-date
last_tweet_id = twitter_search.fetch.results.first ? twitter_search.fetch.results.first.id : nil

if last_tweet_id
page = 1
# count each result page
puts "Fetch page #{page}"
result += twitter_search.fetch.count
while twitter_search.next_page?
page += 1
puts "Fetch page #{page}"
twitter_search = twitter_search.fetch_next_page
result += twitter_search.count
end

file = File.open('hashtag_counter_result.txt', 'w')
file.write(result)
file.close

file = File.open('last_tweet_id.txt', 'w')
file.write(last_tweet_id)
file.close

# Print the results
puts "Done\n\n"
puts "Total tweets: #{result}"
puts "Last tweet id: #{last_tweet_id}"
else
puts "Everything is up-to-date"
end
rescue
puts "Oops! Something went wrong. Try again.."
end