Created
October 8, 2010 18:41
-
-
Save moretea/617294 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'socket' | |
require 'octopi' | |
include Octopi | |
COMPETITORS = { | |
'refinery' => "resolve/refinerycms", | |
'radiant' => "radiant/radiant", | |
'zena' => "zena/zena", | |
'adva' => "svenfuchs/adva_cms", | |
'djangocms' => "divio/django-cms", | |
'nesta' => "gma/nesta", | |
'browsercms' => "browsermedia/browsercms", | |
'pyro' => "pyrocms/pyrocms" | |
} | |
def project_info(path) | |
repo = Repository.find(:user => path.split("/").first, :repo => path.split("/").last) | |
score = repo.forks * 4 + repo.watchers | |
{:name => repo.name, :forks => repo.forks, :watchers => repo.watchers, :score => score.to_f } | |
end | |
def compare_projects(project1, project2) | |
project1_info = project_info(project1) | |
project2_info = project_info(project2) | |
if (proportion = project1_info[:score] / project2_info[:score]) > 1 | |
proportion **= -1 | |
project1, project2 = project2, project1 | |
end | |
{ :project1 => project1_info, :project2 => project2_info, :score => ((proportion * 10000).round / 100.0).to_s + "%" } | |
end | |
# Reusable IRC bot-framework | |
class SimpleIrcBot | |
def initialize(server, port, channel, nick, password) | |
@channel = channel | |
@nick = nick | |
@socket = TCPSocket.open(server, port) | |
say "NICK #{@nick}" | |
say "USER #{@nick} 0 * #{@nick}" | |
say_to "NickServ", "identify #{password}" | |
say "JOIN ##{@channel}" | |
say_to_chan "#{1.chr}ACTION is here to help#{1.chr}" | |
end | |
def say_to_chan(msg) | |
say_to "##{@channel}", msg | |
end | |
def say_to(to, msg) | |
say "PRIVMSG #{to} :#{msg}" | |
end | |
def notice_to(to, msg) | |
say "NOTICE #{to} :#{msg}" | |
end | |
def say(msg) | |
puts msg | |
@socket.puts msg | |
end | |
def run | |
until @socket.eof? do | |
begin | |
msg = @socket.gets | |
puts msg | |
# Keep alive | |
if msg.match(/^PING :(.*)$/) | |
say "PONG #{$~[1]}" | |
next | |
end | |
if msg.match(/:(.*)!(.*) PRIVMSG ##{@channel} :(.*)$/) | |
channel_message $3, $1, $2 | |
elsif msg.match(/:(.*)!(.*) PRIVMSG #{@nick} :(.*)$/) | |
private_message $3, $1, $2 | |
elsif msg.match(/:(.*)!(.*) JOIN :(.*)$/) | |
join $3, $1, $2 | |
elsif msg.match(/:(.*)!(.*) NICK :(.*)$/) | |
nick $3.chomp.strip, $1, $2 | |
end | |
rescue Exception => e | |
p [:exception, e] | |
end | |
end | |
end | |
def quit | |
say "PART ##{@channel} : It's time to go. Bye!" | |
say 'QUIT' | |
end | |
end | |
class RefineryBot < SimpleIrcBot | |
def channel_message msg, who, full_name | |
p [:channel, msg] | |
if msg =~ /(.*): (pastie please|post a pastie| can you post a pastie)/ | |
notice_to $1, "#{who} requested that you'd post a pastie. Please go to http://pastie.org/pastes/new" | |
end | |
if msg =~ /^compare (.*) vs (.*)/ | |
p1 = $1.chomp.strip | |
p2 = $2.chomp.strip | |
if COMPETITORS.has_key? p1 | |
if COMPETITORS.has_key? p2 | |
compare_frameworks p1, p2, @channel | |
end | |
end | |
end | |
end | |
def private_message msg, who, full_name | |
if msg =~/^help/ | |
notice_to who, "Supported commands:" | |
notice_to who, " compare list Shows a list of comparable cms's" | |
notice_to who, " compare <CMS1> vs <CMS2> Compare the 'popularity' of two CMS's" | |
notice_to who, " <USER>: pastie please \\" | |
notice_to who, " <USER>: post a pastie |=> Send pastie request" | |
notice_to who, " <USER>: can you post a pastie /" | |
elsif msg =~ /^compare list/ | |
notice_to who, COMPETITORS.keys.sort.join(", ") | |
else | |
notice_to who, "Try /msg #{@nick} help." | |
end | |
end | |
def join channel, who, fullname | |
notice_to who, "Welcome to refinerycms. Don't hesitate to ask your questions!" | |
end | |
def nick new_nick, old_nick, fullname | |
p [:nick, new_nick, old_nick, fullname] | |
say_to_chan "Attention! parndt is present!" if new_nick == "parndt" and old_nick =~ /parndt_/ | |
end | |
def compare_frameworks a,b, who | |
result = compare_projects COMPETITORS[a], COMPETITORS[b] | |
p result | |
say_to_chan "Comparing #{result[:project1][:name]} with #{result[:project2][:name]}" | |
say_to_chan " %-20s: forks: %04d watchers: %04d" % [result[:project1][:name], result[:project1][:forks], result[:project1][:watchers]] | |
say_to_chan " %-20s: forks: %04d watchers: %04d" % [result[:project2][:name], result[:project2][:forks], result[:project2][:watchers]] | |
say_to_chan " Score: " + result[:score] | |
end | |
end | |
bot = RefineryBot.new("irc.freenode.net", 6667, 'moreteatest', "RefBot", "secret password") | |
trap("INT"){ bot.quit } | |
bot.run |
Just a irc bot that compares the "popularity" of a few different frameworks ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What are you up to? I see pyro being mentioned :)