comparison weather.tcl @ 161:2c9db9a29429

weather: More work, bugfixes.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Jun 2014 15:53:53 +0300
parents c8fa73ba67da
children bee5cd89d41c
comparison
equal deleted inserted replaced
160:e3e156911ab4 161:2c9db9a29429
10 # 10 #
11 ########################################################################## 11 ##########################################################################
12 12
13 set weather_default_locations "raahe" 13 set weather_default_locations "raahe"
14 14
15 set weather_datafile "/home/niinuska/bot/data.weather" 15 set weather_datafile "/home/niinuska/bot/weather.data"
16 set weather_check_period 5 16 set weather_aliasfile "/home/niinuska/bot/weather.alias"
17 set weather_check_period 2
17 18
18 set weather_max_results 3 19 set weather_max_results 3
19 20
20 set weather_logmsg 1 21 set weather_logmsg 1
21 set weather_preferredmsg "PRIVMSG" 22 set weather_preferredmsg "PRIVMSG"
72 proc weather_ctime {utime} { 73 proc weather_ctime {utime} {
73 return [clock format $utime -format "%H:%M"] 74 return [clock format $utime -format "%H:%M"]
74 } 75 }
75 76
76 77
78 proc weather_valid_user {uhand} {
79 if {$uhand != "" && $uhand != "{}" && $uhand != "*"} {
80 return 1
81 }
82 return 0
83 }
84
85
86 proc weather_check_user {uhand} {
87 if {![weather_valid_user $uhand]} {
88 weather_msg $upublic $unick $uchan $weather_msg_user_not_known
89 return 0
90 }
91 return 1
92 }
93
94
95 #-------------------------------------------------------------------------
96 proc weather_get_data {ukey udata} {
97 set str "\002[lindex $udata 0]\002, mitattu klo [weather_ctime [lindex $udata 1]]: \002[lindex $udata 2]°C\002"
98 if {[llength $udata] > 3} {
99 append str ", tien pinta [lindex $udata 3]°C"
100 }
101 if {[llength $udata] > 4} {
102 append str ", [lindex $udata 4]"
103 }
104 return "${str}."
105 }
106
107
108 proc weather_get_key {ukey} {
109 global weather_data
110 return [weather_get_data $ukey $weather_data($ukey)]
111 }
112
113
77 #------------------------------------------------------------------------- 114 #-------------------------------------------------------------------------
78 proc weather_update {} { 115 proc weather_update {} {
79 global weather_datafile weather_data 116 global weather_datafile weather_data
80 117
81 # Create dict 118 # Create dict
119 array unset weather_data
82 array set weather_data {} 120 array set weather_data {}
83 set wtemp_min_val 500000 121 set wtemp_min_val 500000
84 set wtemp_max_val -500000 122 set wtemp_max_val -500000
85 set wtemp_min_key "" 123 set wtemp_min_key ""
86 set wtemp_max_key "" 124 set wtemp_max_key ""
94 set utemp [lindex $udata 2] 132 set utemp [lindex $udata 2]
95 set ukey [lindex $udata 0] 133 set ukey [lindex $udata 0]
96 134
97 set weather_data($ukey) $udata 135 set weather_data($ukey) $udata
98 136
99 if {$utemp < $wtemp_min_val} { 137 if {[string is double -strict $utemp]} {
100 set wtemp_min_key $ukey 138 if {$utemp < $wtemp_min_val} {
101 } 139 set wtemp_min_key $ukey
102 if {$utemp > $wtemp_max_val} { 140 set wtemp_min_val $utemp
103 set wtemp_max_key $ukey 141 }
142 if {$utemp > $wtemp_max_val} {
143 set wtemp_max_key $ukey
144 set wtemp_max_val $utemp
145 }
104 } 146 }
105 } 147 }
106 } 148 }
107 close $ufile 149 close $ufile
108 150
145 } else { 187 } else {
146 weather_update 188 weather_update
147 } 189 }
148 190
149 #------------------------------------------------------------------------- 191 #-------------------------------------------------------------------------
150 proc weather_get_data {ukey udata} {
151 return "\002[lindex $udata 0]\002, mitattu klo [weather_ctime [lindex $udata 1]]: \002[lindex $udata 2]°C\002."
152 }
153
154
155 proc weather_get_key {ukey} {
156 global weather_data
157 return [weather_get_data $ukey $weather_data($ukey)]
158 }
159
160
161 #-------------------------------------------------------------------------
162 proc weather_cmd {unick uhand uchan uargs upublic} { 192 proc weather_cmd {unick uhand uchan uargs upublic} {
163 global weather_default_locations weather_data weather_max_results 193 global weather_default_locations weather_data weather_max_results
164 global weather_msg_usage_1 weather_msg_usage_2 weather_msg_user_not_known 194 global weather_msg_usage_1 weather_msg_usage_2 weather_msg_user_not_known
165 global weather_msg_no_results weather_msg_no_data_for_location 195 global weather_msg_no_results weather_msg_no_data_for_location
166 196
174 return 0 204 return 0
175 } 205 }
176 206
177 # Setting the location 207 # Setting the location
178 if {$rarg == "aseta" || $rarg == "set"} { 208 if {$rarg == "aseta" || $rarg == "set"} {
179 if {$uhand == "" || $uhand == "{}"} { 209 if {![weather_check_user $uhand]} {
180 weather_msg $upublic $unick $uchan $weather_msg_user_not_known
181 return 0 210 return 0
182 } 211 }
183 212
184 # Split the list of desired locations 213 # Split the list of desired locations
185 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}] 214 set qlist [::textutil::split::splitx [join [lrange $rarglist 1 end] " "] {\s*\;\s*}]
193 weather_msg $upublic $unick $uchan $weather_msg_usage_2 222 weather_msg $upublic $unick $uchan $weather_msg_usage_2
194 } 223 }
195 return 0 224 return 0
196 } 225 }
197 226
227 if {$rarg == "default" || $rarg == "vakio"} {
228 if {![weather_check_user $uhand]} {
229 return 0
230 }
231
232 set lista [getuser $uhand XTRA "weather_locations"]
233 if {$lista == "" || $lista == "{}"} {
234 set lista "Ei asetettu"
235 }
236 weather_msg $upublic $unick $uchan "Vakiopaikat ${uhand}: \002${lista}\002."
237 return 0
238 }
239
198 # Get args or default location(s) 240 # Get args or default location(s)
199 set rargs [join $rarglist " "] 241 set rargs [join $rarglist " "]
200 if {$rargs == "" && ($uhand == "" || $uhand == "{}")} { 242 if {$rargs == "" && $uhand != "" && $uhand != "{}" && $uhand != "*"} {
201 set rargs [getuser $uhand XTRA "weather_locations"] 243 set rargs [getuser $uhand XTRA "weather_locations"]
202 } 244 }
203 if {$rargs == "" || $rargs == "{}"} { 245 if {$rargs == ""} {
204 set rargs $weather_default_locations 246 set rargs $weather_default_locations
205 } 247 }
206 248
207 # Handle argument list 249 # Handle argument list
208 set nresults 0 250 set nresults 0
235 } else { 277 } else {
236 weather_msg $upublic $unick $uchan "\002${ukey}\002: $weather_msg_no_results" 278 weather_msg $upublic $unick $uchan "\002${ukey}\002: $weather_msg_no_results"
237 } 279 }
238 incr ufound 280 incr ufound
239 } 281 }
282
283 if {$nresults >= $weather_max_results} {
284 return 0
285 }
240 } 286 }
241 287
242 if {$ufound == 0} { 288 if {$ufound == 0} {
243 weather_msg $upublic $unick $uchan "\002${rarg}\002: $weather_msg_no_data_for_location" 289 weather_msg $upublic $unick $uchan "\002${rarg}\002: $weather_msg_no_data_for_location"
244 } 290 }