This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyweather.rb
81 lines (72 loc) · 2.36 KB
/
yweather.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#-- vim:sw=2:et
#++
#
# :title: Yahoo! Weather
#
# Author:: Lite
# Copyright:: (C) 2012 Lite
# License:: GPL
# Version:: 2013-01-19
class YWeatherPlugin < Plugin
URL = "http://weather.yahooapis.com/forecastrss"
def help(plugin, topic="")
p = @bot.config['core.address_prefix'].first
case topic.downcase
when 'set'
"Use \"#{p}w set <zipcode|woeid>\" to set " +
"your default zipcode or Yahoo! woeid."
else
"#{p}w <zip|woeid> for local weather -- #{p}fc <zip|woeid> " +
"for two-day forecast. (see also #{p}help #{plugin} set)"
end
end
def weather(m, params)
location = params[:id]
if location.nil? or location.empty?
if @registry.has_key? m.sourcenick.downcase
location = @registry[ m.sourcenick.downcase ]
else
m.reply "Invalid zip/id."
return
end
end
feed = if location.length == 5
@bot.httputil.get URL + "?p=#{location}"
else
@bot.httputil.get URL + "?w=#{location}&u=c"
end
if feed.nil?
m.reply "Yahoo! Weather unavailable."
return
end
xml = REXML::Document.new feed
degrees = xml.elements['//yweather:units'].attributes['temperature']
condition = xml.elements['//yweather:condition'].attributes['text']
city = xml.elements['//yweather:location'].attributes['city']
humidity = xml.elements['//yweather:atmosphere'].attributes['humidity']
region = xml.elements['//yweather:location'].attributes['region']
temp = xml.elements['//yweather:condition'].attributes['temp']
if params[:forecast]
xml.elements.each("//yweather:forecast") do |e|
m.reply "#{Bold}#{e.attributes['day']}:#{Bold} " +
e.attributes['low'] + '/' + e.attributes['high'] + degrees +
', ' + e.attributes['text']
end
else
m.reply "#{city}, #{region} - #{temp}#{degrees}/#{humidity}\% humidity, #{condition}"
end
end
def set_user(m, params)
location = params[:id].to_s
@registry[ m.sourcenick.downcase ] = location
m.okay
end
end
p = YWeatherPlugin.new
[ 'w', 'weather', 'wz' ].each do |w|
p.map "#{w} [:id]", :action => "weather", :defaults => { :forecast => false }
p.map "#{w} set [zip[code]] [:id]", :action => "set_user"
end
[ 'fc', 'forecast'].each do |fc|
p.map "#{fc} [:id]", :action => "weather", :defaults => { :forecast => true }
end