Created
December 6, 2008 02:56
-
-
Save rafaelss/32599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sqlite3 twibot.db 'CREATE TABLE statuses (id INTEGER PRIMARY KEY, status_id VARCHAR NOT NULL)' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
receiver: PUT YOUR JABBER/GTALK ACCOUNT HERE | |
twitter: | |
username: YOUR USERNAME | |
password: YOUR PASSWORD | |
sender: | |
username: ANOTHER JABBER ACCOUNT TO SEND TWEETS TO YOU ([email protected]) | |
password: PASSWORD FOR ACCOUNT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment