Skip to content

Commit

Permalink
Merge pull request LindseyB#6 from wilkie/feature/ruby
Browse files Browse the repository at this point in the history
Feature/ruby
  • Loading branch information
LindseyB committed Jun 21, 2012
2 parents ad68acc + c910b27 commit 5e60cfa
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/xombot/plugins/define.rb
@@ -0,0 +1,32 @@
class Define < XOmBot::Plugin
DICTIONARY_QUERY_URL = "http://dictionary.reference.com/browse/"

match /define (.+)/
help "retrieves the definition for the given english word"
usage "define bier -- prints the definition of the word bier"

def execute(m, word)
@agent = Mechanize.new
page = @agent.get "#{DICTIONARY_QUERY_URL}#{word}"
part_of_speech = page.search('//div[@class="pbk"]/span[@class="pg"]').first.content.strip
definition = []
definitions = page.search('//div[@class="pbk"]/div[@class="luna-Ent"]')
definition << definitions.first.children.inject("") do |result, element|
result + element.content
end
definition[0].gsub! /^\d+\./, ""

if (definitions.count > 1)
definition << definitions[1].children.inject("") do |result, element|
result + element.content
end
definition[1].gsub! /^\d+\./, ""
end

m.reply "#{word}: (#{part_of_speech})"
m.reply "#{" "*(word.length-1)}1. #{definition[0]}"
if definition[1]
m.reply "#{" "*(word.length-1)}2. #{definition[1]}#{definitions.count-3 > 0 ? " (#{definitions.count-3} definitions follow...)" : ""}"
end
end
end
50 changes: 50 additions & 0 deletions lib/xombot/plugins/weather.rb
@@ -0,0 +1,50 @@
class Weather < XOmBot::Plugin
WEATHER_QUERY_URL = "http://weather.com/search/enhancedlocalsearch?where="

match /weather ([^ ]+)(?:\s(.+))?/
help "gives the forecast for the given city"
usage "weather pittsburgh -- gives the weather for pittsburgh"
usage "weather melbourne hourly -- gives a 6 hour forecase for melbourne"

def execute(m, place, option)
@agent = Mechanize.new
page = @agent.get "#{WEATHER_QUERY_URL}#{place}"
current_weather = page.search '//span[@itemprop="temperature-fahrenheit"]'
current_phrase = page.search '//span[@itemprop="weather-phrase"]'

if option == "hourly"
hourly_url = page.search '//a[@from="rightnow_1"]'
if hourly_url.first.nil?
hourly_url = page.search '//a[@from="rightnow_TimeNav_weather_nav"][@title="Hourly"]'
end

if hourly_url.first.nil?
m.reply "No hourly forecast available."
return
end

hourly_url = hourly_url.first.attribute "href"
page = @agent.get "http://weather.com#{hourly_url}"

parts = page.search('//div[@class="wx-timepart"]').to_a
parts.insert(0, page.search('//div[@class="wx-timepart wx-first"]'))

reply_string = parts.inject("") do |a, part|
hour = part.children[1].children[0].content.to_i
meridian = part.children[1].children[1].content.strip
if meridian.upcase == "PM"
hour += 12
end
temp = part.children[3].children[2].content.strip.to_i
phrase = part.children[3].children[4].content
temp_cel = (temp - 32) * 5 / 9
"#{a} #{hour}:00: #{temp}\u00b0F/#{temp_cel}\u00b0C/#{phrase}"
end
m.reply "#{place} by hour:#{reply_string}"
else
temp = current_weather.first.content.to_i
temp_cel = (temp - 32) * 5 / 9
m.reply "Weather in #{place}: #{temp}\u00b0F/#{temp_cel}\u00b0C and #{current_phrase.first.content}"
end
end
end

0 comments on commit 5e60cfa

Please sign in to comment.