kastner / ruby-junk

my random ruby scripts

This URL has Read+Write access

ruby-junk / twitcount.rb
100755 47 lines (39 sloc) 0.999 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env ruby -wKU
 
require 'rubygems'
require 'open-uri'
require 'json'
require 'cgi'
 
# the first part of a term must have no spaces
# the second part is the actual search on search.twitter.com you want counted
TERMS = {
  "etsy" => "etsy"
}
 
class Twitter
  def self.base
    "http://search.twitter.com/search.json?q=%s&rpp=100&page=%s"
  end
  
  def self.mentions(term)
    mentions = 0
    
    1.upto(15) do |page|
      p = JSON.parse(open(base % [CGI.escape(term), page]).read)
      p["results"].each do |tweet|
        t = Time.parse(tweet["created_at"])
        minutes_ago = ((Time.now - t) / 60).to_i
        return mentions if minutes_ago > 5
        mentions += 1
      end
    end
    
    return mentions
  end
end
 
if ARGV[0] == "config"
  puts "graph_title Twitter Mentions"
  puts "graph_vlabel mentions"
  TERMS.each do |term, title|
    puts "#{term}.label #{title}"
  end
  exit
end
 
TERMS.each do |term, title|
  puts "#{term}.value #{Twitter.mentions(title)}"
end