forked from LindseyB/XOmBot
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request LindseyB#6 from wilkie/feature/ruby
Feature/ruby
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |