Module: Twtail

Defined in:
lib/twtail.rb

Class Method Summary collapse

Class Method Details

.colorize(color, text, regex = nil) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/twtail.rb', line 73

def colorize(color, text, regex = nil)
  if regex.nil?
    "\033[#{color}m#{text}\033[0m"
  else
    text.gsub(regex, "\033[#{color}m\\1\033[0m")
  end
end

.execute(params = nil) ⇒ Object



15
16
17
18
# File 'lib/twtail.rb', line 15

def execute(params=nil)
  trap("INT") { exit }
  params ? self.search(params) : self.help
end

.from_parser(from) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/twtail.rb', line 65

def from_parser(from)
  from.sub!(/(\w+).+\n.+/,'\1')
  from.gsub!(/.*(\(.*)/, "\\1")
  from.gsub!(/\)http/, ") - http")
  from = colorize(32, "#{from}: ")
  colorize(4, from, /(https?:\/\/[\S]+)/i)
end

.helpObject



20
21
22
# File 'lib/twtail.rb', line 20

def help
  return("Usage: twtail [xbox+live | from:caffo | '#barcamp' | '-spammed_keyword']")
end

.search(params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/twtail.rb', line 24

def search(params)
   begin
    pointer   = Time.now-86400
    parameter = CGI::escape(params)
    url       = "http://search.twitter.com/search.atom?q=#{parameter}"
    feed      = SimpleRSS.parse open(url)
    coder     = HTMLEntities.new

    puts "\033[37m==\033[0m \033[1;32m#{feed.channel.title}\033[0m \033[37m==\033[0m\n\n" unless Module.constants.include?("DEBUG")
    if feed.items[0].published < pointer
      puts "No items found since yesterday"
      exit
    end

    while 1==1 do
      new_items = false
      begin
        feed  = SimpleRSS.parse open(url) rescue nil
        feed.items.each do |item|
          next if item.published < pointer
          unless Module.constants.include?("DEBUG")
            msg = colorize(37, coder.decode(item.title))
            msg = colorize(31, msg, /(#[[\d]]?(\S+))/i)
            msg = colorize(33, msg, /(@[[\d]]?(\S+))/i)
            msg = colorize(34, msg, /(https?:\/\/[\S]+)/i)
            puts "#{coder.decode(from_parser(item.author))} #{msg}"
            new_items = true
          end
        end
      rescue
      end
      puts "\n\n" if new_items == true
      pointer = feed.items[0].published + 1
      break if Module.constants.include?("DEBUG")
      sleep(60)
    end
  rescue
     "No data was found for this criteria. please try with other keywords."
  end
end