/usr/share/nadoka/plugins/tenkibot.nb is in nadoka 0.7.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | # -*-ruby-*-
#
# Copyright (c) 2004-2006 SASADA Koichi <ko1 at atdot.net>
#
# This program is free software with ABSOLUTELY NO WARRANTY.
# You can re-distribute and/or modify this program under
# the same terms of the Ruby's license.
#
=begin
== Abstract
Answer weather information using "Livedoor Weather Web Service / LWWS".
LWWS: http://weather.livedoor.com/weather_hacks/webservice.html
== Usage
tenki> [CITY]
tenki:[today|tomorrow|dayaftertomorrow]> [CITY]
[CITY] should be city name in Kanji listed on following table.
http://weather.livedoor.com/forecast/rss/forecastmap.xml
If timing is not specified, show today's information.
== Configuration
BotConfig = [
{
:name => :TenkiBot,
:ch => /nadoka/, # default: /.*/
}
]
=end
require 'open-uri'
require 'pp'
require 'kconv'
require 'rexml/document'
require 'date'
class TenkiBot < Nadoka::NDK_Bot
CityIDs = {}
def bot_initialize
@available_channel = @bot_config[:ch] || /.*/
open('http://weather.livedoor.com/forecast/rss/forecastmap.xml'){|f|
f.each{|line|
if /city title="(.+?)" id="(\d+)"/ =~ line
CityIDs[$1.toeuc] = $2.to_i
end
}
}
end
def tenki city, timing
doc = open(
"http://weather.livedoor.com/forecast/webservice/rest/v1?" \
"city=#{CityIDs.fetch(city)}&day=#{timing}"){|f|
REXML::Document.new f.read
}
title = doc.elements['/lwws/title/'].text.toeuc
telop = doc.elements['/lwws/telop/'].text.toeuc
link = doc.elements['/lwws/link/'].text.toeuc
desc = doc.elements['/lwws/description/'].text.toeuc
max = doc.elements['/lwws/temperature/max/celsius/'].text
min = doc.elements['/lwws/temperature/min/celsius/'].text
date = Date.parse(doc.elements['/lwws/forecastdate/'].text)
datestr = date.strftime('%m/%d')
celsius = []
celsius << "max: #{max}" if max
celsius << "min: #{min}" if min
unless celsius.empty?
celsius = "(#{celsius.join(', ')}) "
end
"#{title} (#{datestr}): #{telop} - #{celsius}#{desc} - #{link}".tojis
end
def on_privmsg prefix, ch, msg
if @available_channel === ch
if /\Atenki(|:(today|tomorrow|dayaftertomorrow))>(.+)/ =~ msg
city = $3.strip.toeuc
timing = ($2 || 'today').strip
begin
result = tenki(city, timing).gsub(/\n/, ' ')
rescue IndexError
result = "Unknown city. Check city title on http://weather.livedoor.com/forecast/rss/forecastmap.xml"
rescue => e
result = "#{e}"
end
send_notice ch, "tenki bot: #{result}"
end
end
end
end
|