Skip to content

Commit

Permalink
Merge pull request LTe#3 from jasiek/master
Browse files Browse the repository at this point in the history
Search engine
  • Loading branch information
LTe committed Jun 7, 2012
2 parents e3519c9 + 2a86906 commit 722971f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/muzang-plugins.rb
Expand Up @@ -10,3 +10,4 @@
require 'muzang-plugins/muzang-reddit'
require 'muzang-plugins/muzang-rubygems'
require 'muzang-plugins/muzang-soupirc'
require 'muzang-plugins/muzang-grep'
102 changes: 102 additions & 0 deletions lib/muzang-plugins/muzang-grep.rb
@@ -0,0 +1,102 @@
require 'sqlite3'
require 'active_record'
require 'pastie-api'

class Grep
include Muzang::Plugins::Helpers

DATABASE_FILE = 'irclog.sqlite3'
DATABASE_CONFIG = {
:database => 'irclog.sqlite3',
:encoding => 'utf8',
:adapter => 'sqlite3'
}

class Message < ActiveRecord::Base
def self.like(channel, term)
find(:all, :conditions => ['channel LIKE ? AND content MATCH ?' , channel, term], :order => 'created_at DESC', :limit => 20)
end

def to_text
["## #{user} @ #{created_at}:", content, ''].join("\n")
end

class Migration < ActiveRecord::Migration
def self.up
execute(%q{
CREATE VIRTUAL TABLE messages USING fts4(
channel VARCHAR(255) NOT NULL,
user VARCHAR(255) NOT NULL,
content VARCHAR(2048) NOT NULL,
created_at DATETIME NOT NULL);
})
end

def self.down
drop_table :messages
end
end
end

def initialize(bot)
@bot = bot
open_database
end

def call(connection, message)
on_channel(message) do
match(message, /^[^~(Searched)]/) do
persist_message(message)
end
match(message, /^~ (.*)/) do |what|
search_for_term(connection, message, what[1])
end
end
rescue
puts $!
puts $!.backtrace
raise
end

def persist_message(message)
Message.create!(:channel => message.channel,
:user => message.user,
:content => message.message)
end

def search_for_term(connection, message, term)
results = Message.like(message.channel, term)
if results.size > 0
link = save_results(term, results)
connection.msg(message.channel, "Searched for '#{term}', found #{results.size} matches: #{link}")
else
connection.msg(message.channel, "Searched for '#{term}', nothing found")
end
end

private
def open_database
initialize_database unless database_exists?
open_existing_database
end

def database_exists?
File.exists?(DATABASE_FILE)
end

def initialize_database
ActiveRecord::Base.establish_connection(DATABASE_CONFIG)
Message::Migration.up
ActiveRecord::Base.connection.disconnect!
end

def open_existing_database
ActiveRecord::Base.establish_connection(DATABASE_CONFIG)
end

def save_results(term, results)
content = ["Searched for '#{term}', found #{results.size} matches", '']
content += results.map(&:to_text)
Pastie.create(content.join("\n"), false).link
end
end
3 changes: 3 additions & 0 deletions muzang-plugins.gemspec
Expand Up @@ -19,6 +19,9 @@ Gem::Specification.new do |gem|
gem.add_dependency "muzang", "~> 1.0.0"
gem.add_dependency "memetron", "~> 0.1.1"
gem.add_dependency "soup-client"
gem.add_dependency "sqlite3"
gem.add_dependency "activerecord"
gem.add_dependency "pastie-api"

gem.add_development_dependency "em-ventually", "~> 0.1.2"
gem.add_development_dependency "rspec", "~> 2.6.0"
Expand Down

0 comments on commit 722971f

Please sign in to comment.