Skip to content

Instantly share code, notes, and snippets.

@oogali
Created August 18, 2011 18:48
Show Gist options
  • Save oogali/1154821 to your computer and use it in GitHub Desktop.
Save oogali/1154821 to your computer and use it in GitHub Desktop.
Woot! IRC bot
#!/usr/bin/env ruby
require 'rubygems'
require 'isaac'
require 'net/http'
require 'nokogiri'
require 'open-uri'
CACHE_EXPIRATION = 3600
cache = {}
configure do |c|
c.nick = 'woot'
c.server = 'irc.yourserver.com'
c.port = 6667
c.realname = 'woot!'
c.verbose = true
end
helpers do
def get_doc(category)
subdomain = nil
case category
when 'regular'
subdomain = 'www'
else
subdomain = category.split('.').first
end
raise 'Invalid category specified' if !subdomain
url = "http://api.woot.com/1/sales/current.rss/#{subdomain}.woot.com"
doc = Nokogiri::XML open(url, 'r', :read_timeout => 5)
end
def get(cache, category = nil)
category ||= 'regular'
return cache[category] if cache[category] and cache[category][:ts] > Time.now.to_i
begin
doc = get_doc category
cache[category] = {
:ts => Time.now.to_i + CACHE_EXPIRATION,
:price => doc.xpath('//channel/item/woot:price', { 'woot' => 'http://www.woot.com/' }).text,
:product => doc.xpath('//channel/item/woot:products/woot:product', { 'woot' => 'http://www.woot.com/' }).map { |x| x.text }.join(', '),
:shipping => doc.xpath('//channel/item/woot:shipping', { 'woot' => 'http://www.woot.com/' }).text,
:condition => doc.xpath('//channel/item/woot:condition', { 'woot' => 'http://www.woot.com/' }).text,
:url => shorten(doc.xpath('//channel/item/woot:discussionurl', { 'woot' => 'http://www.woot.com/' }).text)
}
rescue => err
puts "Could not get document: #{err.to_s}"
end
end
def expire(cache, category = nil)
category ||= 'regular'
cache.delete category if cache[category]
end
def shorten(url)
short = open('http://be.gs/shorten?url=' + URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")), 'r', :read_timeout => 1).read rescue nil
short = open('http://is.gd/create.php?format=simple&url=' + URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")), 'r', :read_timeout => 1).read rescue nil
short = url if !short
short
end
end
on :connect do
join '#botdev'
end
on :channel, /^!woot(.*)/ do
c = get cache, (match.first =~ /^\s+/ ? match.first.gsub(/^\s+/, '') : nil)
msg channel, "#{c[:price]} + #{c[:shipping]} // #{c[:condition].upcase}: #{c[:product]} // #{c[:url]}" if c
end
on :channel, /^!refurb/ do
msg channel, "r0x0rs!"
end
on :private, /^!join\s+(.*)/ do
join match.first
end
on :private, /^!expire(.*)/ do
category = match.first =~ /^\s+/ ? match.first.gsub(/^\s+/, '') : 'regular'
expire cache, category
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment