Created
January 16, 2009 20:57
-
-
Save relrod/48119 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/ruby | |
require "socket" | |
# Here we require things for use later on... | |
require "net/http" # For parsing things... | |
require "rexml/document" # For XML parsing. | |
nick = "CB|RubyBot" | |
server = "irc.freenode.net" | |
port = 6667 | |
user = "CodeBlock" | |
realname = "CodeBlock's Ruby Bot" | |
# Make Socket | |
@socket = TCPSocket::new(server,port) | |
def raw(text) | |
@socket.send("#{text}\n",0) | |
puts "--> #{text}" | |
end | |
def say(text) | |
text = text.gsub("/\n/","") | |
text = text.gsub("/\n/","") | |
raw "PRIVMSG #{@channel} :#{text}" | |
end | |
raw "USER #{user} #{user} #{user} #{user} :#{realname}\n" | |
raw "NICK #{nick}\n" | |
raw "JOIN #botters-ruby\n" | |
while true | |
data = @socket.recv(1024) | |
dataregex = data.match(/:(.*)!(.*)@(.*) (.*) (.*) :(.*)/i) | |
data_array = dataregex.to_a | |
said = data_array[6].to_s.gsub("\r","").gsub("\n","") | |
nick = data_array[1].to_s | |
@channel = data_array[5].to_s | |
#p said | |
if said == "hi" | |
raw "PRIVMSG #botters-ruby :Hello, #{nick}." | |
end | |
if said =~ /^!wxalerts (.*)/i | |
inputzip = $1 | |
zipcode = inputzip.gsub("/\s/","%20") | |
alertsurl = "http://api.wunderground.com/auto/wui/geo/AlertsXML/index.xml?query=#{zipcode}" | |
alertsxml = Net::HTTP.get_response(URI.parse(alertsurl)).body | |
alerts = REXML::Document.new(alertsxml) | |
alertcount = [] | |
alerttype = [] | |
alertstart = [] | |
alertstop = [] | |
alerts.elements.each('alerts/alert') do |ele| | |
alertcount << ele.attributes["count"] | |
end | |
alerts.elements.each('alerts/alert/AlertItem/description') do |ele| | |
alerttype << ele.text | |
end | |
alertcount.each_with_index do |alertcount,idx| | |
alertcount = alertcount.to_s.gsub("/\n/","") | |
say "There are #{alertcount} alerts for #{zipcode}: #{alerttype.join(', ')}" | |
end | |
end | |
if said =~ /^!yahoo (.*)/i | |
searchterms = $1.gsub("\s","%20") | |
searchurl = "http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=#{searchterms}&results=1" | |
searchxml = Net::HTTP.get_response(URI.parse(searchurl)).body | |
results = REXML::Document.new(searchxml) | |
title = [] | |
link = [] | |
summary = [] | |
results.elements.each('ResultSet/Result/Title') do |ele| | |
title << ele.text | |
end | |
results.elements.each('ResultSet/Result/Summary') do |ele| | |
summary << ele.text | |
end | |
results.elements.each('ResultSet/Result/Url') do |ele| | |
link << ele.text | |
end | |
title.each_with_index do |title,idx| | |
say "#{title} - #{summary} - #{link}" | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment