Created
October 25, 2010 15:41
-
-
Save alx/645153 to your computer and use it in GitHub Desktop.
link parser for tetalab barbabot
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'jabber/bot' | |
require 'rdelicious' | |
require 'bson' | |
require 'mongo' | |
require 'mongoid' | |
Mongoid.configure do |config| | |
name = "tatibotto" | |
host = "localhost" | |
config.master = Mongo::Connection.new.db(name) | |
config.slaves = [ | |
Mongo::Connection.new(host, 27017, :slave_ok => true).db(name) | |
] | |
config.persist_in_safe_mode = false | |
end | |
class Link | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
field :url, :type => String | |
field :description, :type => String | |
field :author, :type => String | |
field :tags, :type => String | |
field :score, :type => Integer, :default => 1 | |
def post_to_delicious | |
delicious = Rdelicious.new("tetalab", "xxxxxx2323") | |
posted_tags = tags | |
posted_tags << author unless author == "fabrice.fourc" | |
delicious.add(url, description, nil, posted_tags) unless delicious.url_exists?(url) | |
end | |
end | |
# Create a new bot | |
bot = Jabber::Bot.new({ | |
:name => 'PublicBot', | |
:jabber_id => 'tatibotto@gmail.com', | |
:password => 'xxxxxx2323', | |
:master => ['barbabot@appspot.com', "alx.girard@gmail.com"], | |
:is_public => true, | |
:status => 'Hello, I am PublicBot.', | |
:presence => :chat, | |
:priority => 10, | |
:silent => true, | |
:debug => true | |
}) | |
# Give the bot another private command, 'puts!', without a response message | |
bot.add_command( | |
:syntax => 'connect_barbabot', | |
:description => 'connect to barbabot', | |
:regex => /^connect_barbabot$/ | |
) do |sender, message| | |
bot.jabber.deliver("barbabot@appspot.com", "hello") | |
bot.jabber.deliver("barbabot@appspot.com", "/join #tetalab") | |
nil | |
end | |
bot.add_command( | |
:syntax => '#tetalab <user> http://<string>', | |
:description => 'speak on barbabot', | |
:regex => /^#tetalab <.*> http:\/\/.*$/, | |
:full_message => true | |
) do |sender, message| | |
regexp, user, text = message.match(/#tetalab <(.*)> (http:\/\/.*)$/).to_a | |
p "regexp1: #{regexp.inspect}" | |
if text && url = URI.extract(text) | |
link = Link.new do |link| | |
# Only get first url (URI.extract can fetch many) | |
link.url = url.first | |
# Extract tags | |
if tags = text.gsub!(link.url, "").scan(/(^|\s)#(\w+)/).flatten | |
tags += text.scan(/(^|\s)@(\w+)/).map{|name| name.gsub("@", "for:")}.flatten | |
tags.reject{|tag| tag.strip.empty?} | |
end | |
link.tags = tags.join(" ").gsub("#", "") | |
# Build description | |
link.description = text.strip | |
link.description = url if link.description.empty? | |
# Connect user to link | |
link.author = user | |
end | |
unless Link.first(:conditions => {:url => link.url}) | |
link.save! | |
link.post_to_delicious unless link.tags.empty? | |
end | |
end | |
nil | |
end | |
bot.add_command( | |
:syntax => '#tetalab <user> +1', | |
:description => '+1 score on last link', | |
:regex => /^#tetalab <.*> \+1$/ | |
) do |sender, message| | |
p "increment score" | |
if link = Link.order_by(:created_at.desc).first | |
p "link found: #{link.inspect}" | |
link.update_attributes!(:score => link.score + 1) | |
end | |
nil | |
end | |
bot.add_command( | |
:syntax => '#tetalab <user> -1', | |
:description => '-1 score on last link', | |
:regex => /#tetalab <.*> -1$/ | |
) do |sender, message| | |
if link = Link.order_by(:created_at.desc).first | |
link.update_attributes!(:score => link.score - 1) | |
end | |
nil | |
end | |
bot.add_command( | |
:syntax => '#tetalab <user> last_links', | |
:description => 'last links', | |
:regex => /#tetalab <.*> last_links/ | |
) do |sender, message| | |
output = "derniers liens:\n" | |
links = Link.order_by(:created_at.desc) | |
links.each do |link| | |
output << " - (#{link.score}): " | |
output << "#{link.description} - " unless link.description == link.url | |
output << "#{link.url} - #{link.author}\n" | |
end | |
output | |
end | |
# Unleash the bot | |
bot.connect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment