public
Description: Twitter tracking Twitter on Twitter
Homepage: http://twitter.com/metatweet
Clone URL: git://github.com/al3x/metatweet.git
metatweet / metatweet.rb
100755 76 lines (58 sloc) 1.859 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%w( rubygems yaml xmpp4r-simple gosu htmlentities).each { |g| require g }
include Gosu
 
CONFIG = YAML.load_file 'config.yml'
 
class Message
  #%w( body x y z alpha fontsize ).each { |at| attr_accessor at.to_sym }
  attr_accessor :body, :x, :y, :z, :alpha, :fontsize, :created_at
  
  def initialize(body, x=nil, y=nil)
    @body = HTMLEntities.new.decode body.split(':')[1]
    
    @x = x ? x : rand(100)
    @y = y ? y : rand(700)
    @z = 1 + rand(10)
    
    @alpha = 255
    @fontsize = 14 + rand(18)
    
    @created_at = Time.now
  end
  
  def age_in_seconds
    (Time.now - @created_at).floor
  end
  
  def degrade_alpha
    return @alpha if self.age_in_seconds < 45
    return @alpha if @alpha == 1
    @alpha = @alpha - 1
  end
end
 
class JabberStream
  def initialize
    @im = Jabber::Simple.new(CONFIG['jabber']['jid'], CONFIG['jabber']['password'])
    @im.status(:chat, 'tweet at me brah')
    @im.deliver('twitter@twitter.com', 'on')
  end
 
  def messages(&block)
    @im.received_messages { |msg| yield msg if msg.type == :chat }
  end
end
 
class TweetWindow < Gosu::Window
  def initialize
    super(1024, 768, CONFIG['fullscreen'], 20)
    self.caption = 'MetaTweet'
    
    @background_image = Gosu::Image.new(self, "bg.png", false)
    @jabber = JabberStream.new
    @messages = Array.new
  end
  
  def update
    @messages = Array.new if button_down?(Gosu::Button::MsLeft)
          
    @jabber.messages do |msg|
      @messages.shift if @messages.size > 10
      @messages << Message.new(msg.body)
    end
  end
  
  def draw
    @background_image.draw(0, 0, 0, 2, 2);
    
    @messages.each do |msg|
      t = Gosu::Image.from_text(self, msg.body, "Helvetica", msg.fontsize, 10, 800, :left)
      t.draw(msg.x, msg.y, msg.z, 1, 1, Gosu::Color.new(msg.degrade_alpha, 0, 83, 65))
    end
  end
end
 
TweetWindow.new.show