rafaelss (owner)

Revisions

gist: 32599 Download_button fork
public
Public Clone URL: git://gist.github.com/32599.git
Embed All Files: show embed
twibot.rb #
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'rubygems'
require 'twitter'
require 'htmlentities'
require 'eventmachine'
require 'activerecord'
require 'activesupport'
require 'xmpp4r-simple'
require 'yaml'
 
class Twibot
 
  def initialize
    @logger = Logger.new("twibot.log")
    @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
 
    @config = YAML::load(File.open('twibot.yml'))
    @email = @config["receiver"]
  end
 
  def connect
    @logger.info("connecting to jabber...")
    @im = Jabber::Simple.new(@config["sender"]["username"], @config["sender"]["password"])
    @logger.info("connected")
  end
 
  def timeline
    @logger.info("connecting to twitter...")
    twitter = Twitter::Base.new(@config["twitter"]["username"], @config["twitter"]["password"])
    @logger.info("connected")
 
    coder = HTMLEntities.new
    messages = ""
    twitter.timeline(:friends).reverse.each do |s|
      status = Status.find_by_status_id(s.id)
      if status.nil?
        name = coder.decode(s.user.name)
        screen_name = coder.decode(s.user.screen_name)
        text = coder.decode(s.text)
 
        replies = text.scan(/@\w+/)
        if replies.length > 0
          text += ' in reply to ' + replies.collect {|user| 'http://twitter.com/' + user[1..-1] }.join(', ')
        end
 
        messages << "#{name} - #{screen_name} =======\n#{text}\n\n"
 
        Status.create(:status_id => s.id)
      end
    end
 
    unless messages.blank?
      @logger.info("delivering to #{@email}")
      @im.deliver(@email, messages)
    end
  end
 
  def update
    @im.received_messages do |message|
      @logger.info("updating twitter with message: #{message}")
 
      twitter = Twitter::Base.new(@config["twitter"]["username"], @config["twitter"]["password"])
      twitter.update(message.body)
    end
  end
 
  def error(exception)
    @logger.error(exception)
 
    unless @im.nil?
      @im.deliver(@email, exception.to_s)
    end
  end
 
  def shutdown
    @logger.info("shutting down")
    @logger.close
 
    unless @im.nil?
      @im.deliver(@email, 'shutting down')
      @im.disconnect
    end
  end
end
 
class Status < ActiveRecord::Base
end
 
ActiveRecord::Base.logger = Logger.new("database.log")
ActiveRecord::Base.colorize_logging = false
 
ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :dbfile => './twibot.db'
)
 
EventMachine.run do
  @twibot = Twibot.new
  at_exit { @twibot.shutdown }
 
  begin
    @twibot.connect
    @twibot.timeline
 
    EventMachine::PeriodicTimer.new(10.minutes) do
      @twibot.timeline
    end
 
    EventMachine::PeriodicTimer.new(1) do
      @twibot.update
    end
  rescue => ex
    @twibot.error(ex)
    retry
  end
end
twibot.yml #
1
2
3
4
5
6
7
8
9
receiver: PUT YOUR JABBER/GTALK ACCOUNT HERE
 
twitter:
  username: YOUR USERNAME
  password: YOUR PASSWORD
 
sender:
  username: ANOTHER JABBER ACCOUNT TO SEND TWEETS TO YOU (someuser@somejabberserver.org)
  password: PASSWORD FOR ACCOUNT
Bash #
1
sqlite3 twibot.db 'CREATE TABLE statuses (id INTEGER PRIMARY KEY, status_id VARCHAR NOT NULL)'