root/platform/tdiary/plugin/livedoor_weather.rb

Revision 15391, 5.5 kB (checked in by hsbt, 5 months ago)

oops. fix small bug.

Line 
1# livedoor_weather.rb
2#
3# insert weather information using livedoor weather web service.
4#
5# Copyright (C) 2007 SHIBATA Hiroshi <h-sbt@nifty.com>
6# You can redistribute it and/or modify it under GPL2.
7#
8
9require 'open-uri'
10require 'timeout'
11require 'rexml/document'
12
13@lwws_rest_url = 'http://weather.livedoor.com/forecast/webservice/rest/v1'
14
15def lwws_init
16        @conf['lwws.city_id'] ||= 63
17        @conf['lwws.icon.disp'] ||= ""
18        @conf['lwws.max_temp.disp'] ||= ""
19        @conf['lwws.min_temp.disp'] ||= ""
20        @conf['lwws.cache'] ||= ""
21        @conf['lwws.cache_time'] ||= 6
22end
23
24def convert_date( date_status )
25        case date_status
26        when "today"
27                date = Time.now
28        when "tomorrow"
29                date = Time.now + (60 * 60 * 24)
30        when "dayaftertomorrow"
31                date = Time.now + (60 * 60 * 24 * 2)
32        end
33        return date.strftime("%Y%m%d")
34end
35
36def lwws_request( city_id, date_status )
37        url =  @lwws_rest_url.dup
38        url << "?city=#{city_id}"
39        url << "&day=#{date_status}"
40
41        proxy = @conf['proxy']
42        proxy = 'http://' + proxy if proxy
43        timeout( 10 ) do
44                open( url, :proxy => proxy ) {|f| f.read }
45        end
46end
47
48def lwws_get( date_status , update = false)
49        lwws_init
50
51        city_id = @conf['lwws.city_id']
52        cache_time = @conf['lwws.cache_time'] * 60 * 60  # hour
53
54        cache = "#{@cache_path}/lwws"
55        file_name = "#{cache}/#{convert_date( date_status )}.xml" # file_name is YYYYMMDD.xml
56
57        begin
58                Dir::mkdir( cache ) unless File::directory?( cache )
59                cached_time = nil
60                cached_time = File.mtime( file_name ) if File.exist?( file_name )
61
62                unless cached_time.nil?
63                        if @conf['lwws.cache'] == "t" and Time.now > cached_time + cache_time
64                                update = true
65                        end
66                end
67
68                if cached_time.nil? or update
69                        xml =  lwws_request( city_id, date_status )
70                        File::open( file_name, 'wb' ) {|f| f.write( xml )}
71                end
72        rescue Timeout::Error
73                return
74        rescue NoMethodError
75                return
76        end
77end
78
79def lwws_to_html( date )
80        lwws_init
81
82        cache = "#{@cache_path}/lwws"
83        case date
84        when "today", "tommorow", "dayaftertommorow"
85                file_name = "#{cache}/#{convert_date( date_status)}.xml"
86        else
87                file_name = "#{cache}/#{date}.xml"
88        end
89
90        today = Time.now.strftime("%Y%m%d")
91
92        begin
93                xml = File::read( file_name )
94                doc = REXML::Document::new( xml ).root
95
96                telop = @conf.to_native( doc.elements["telop"].text, 'utf-8' )
97                max_temp = doc.elements["temperature/max/celsius"].text
98                min_temp = doc.elements["temperature/min/celsius"].text
99                detail_url = doc.elements["link"].text
100
101                result = ""
102                result << %Q|<div class=\"lwws\">|
103
104                if @conf['lwws.icon.disp'] != "t" or @conf.mobile_agent? then
105                        result << %Q|<a href="#{h(detail_url)}">#{telop}</a>|
106                else
107                        title = @conf.to_native( doc.elements["image/title"].text, 'utf-8' )
108                        link = doc.elements["image/link"].text
109                        url = doc.elements["image/url"].text
110                        width = doc.elements["image/width"].text
111                        height = doc.elements["image/height"].text
112                        if date == today
113                                result << %Q|<a href="#{link}">|
114                        end
115                        result << %Q|<img src="#{url}" border="0" alt="#{title}" title="#{title}" width=#{width} height="#{height}" /></a>|
116                        if date == today
117                                result << %Q|</a>|
118                        end
119                end
120
121                if @conf['lwws.max_temp.disp'] == "t" and not max_temp.nil? then
122                        result << %Q| #{@lwws_max_temp_label}:#{h(max_temp)}#{@celsius_label}|
123                end
124
125                if @conf['lwws.min_temp.disp'] == "t" and not min_temp.nil? then
126                        result << %Q| #{@lwws_min_temp_label}:#{h(min_temp)}#{@celsius_label}|
127                end
128
129                result << %Q|</div>|
130
131                return result
132        rescue Errno::ENOENT
133                return ''
134        end
135end
136
137def lwws_conf_proc
138        lwws_init
139
140        if @mode == 'saveconf' then
141                @conf['lwws.city_id'] = @cgi.params['lwws.city_id'][0].to_i
142                @conf['lwws.icon.disp'] = @cgi.params['lwws.icon.disp'][0]
143                @conf['lwws.max_temp.disp'] = @cgi.params['lwws.max_temp.disp'][0]
144                @conf['lwws.min_temp.disp'] = @cgi.params['lwws.min_temp.disp'][0]
145                @conf['lwws.cache'] = @cgi.params['lwws.cache'][0]
146                @conf['lwws.cache_time'] = @cgi.params['lwws.cache_time'][0].to_i
147        end
148
149        result = ''
150
151        result << <<-HTML
152        <h3>#{@lwws_label_city_id}</h3>
153        <p>#{@lwws_desc_city_id}</p>
154        <p><input name="lwws.city_id" value="#{h(@conf['lwws.city_id'])}"></p>
155        HTML
156
157        result << %Q|<h3>#{@lwws_icon_label}</h3>|
158        checked = "t" == @conf['lwws.icon.disp'] ? ' checked' : ''
159        result << %Q|<p><label for="lwws.icon.disp"><input id="lwws.icon.disp" name="lwws.icon.disp" type="checkbox" value="t"#{checked} >#{@lwws_icon_desc}</label></p>|
160
161        result << %Q|<h3>#{@lwws_label_disp_item}</h3>|
162        result << %Q|<p>#{@lwws_desc_disp_item}</p>|
163        result << %Q|<ul>|
164        checked = "t" == @conf['lwws.max_temp.disp'] ? ' checked' : ''
165        result << %Q|<li><label for="lwws.max_temp.disp"><input id="lwws.max_temp.disp" name="lwws.max_temp.disp" type="checkbox" value="t"#{checked} >#{@lwws_max_temp_label}</label></li>|
166        checked = "t" == @conf['lwws.min_temp.disp'] ? ' checked' : ''
167        result << %Q|<li><label for="lwws.min_temp.disp"><input id="lwws.min_temp.disp" name="lwws.min_temp.disp" type="checkbox" value="t"#{checked} >#{@lwws_min_temp_label}</label></li>|
168        result << %Q|</ul>|
169
170        result << %Q|<h3>#{@lwws_label_cache}</h3>|
171        checked = "t" == @conf['lwws.cache'] ? ' checked' : ''
172        result << %Q|<p><label for="lwws.cache"><input id="lwws.cache" name="lwws.cache" type="checkbox" value="t"#{checked} >#{@lwws_desc_cache}</label></p>|
173        result << %Q|<p>#{@lwws_desc_cache_time}</p>|
174        result << %Q|<p><input name="lwws.cache_time" value="#{h(@conf['lwws.cache_time'])}"></p>|
175
176        return result
177end
178
179add_body_enter_proc do |date|
180        unless @conf.mobile_agent? or @conf.iphone? or feed? or bot?
181                lwws_to_html( date.strftime("%Y%m%d") )
182        end
183end
184
185add_update_proc do
186        lwws_get( "today" )
187        lwws_get( "tomorrow" )
188        lwws_get( "dayaftertomorrow" )
189end
190
191add_conf_proc( 'lwws', @lwws_plugin_name ) do
192        lwws_conf_proc
193end
Note: See TracBrowser for help on using the browser.