Get it Now at the Eggdrop Tcl Archive: https://tclarchive.org/view.php?id=1793
Please test the new script and see what we think:)
You can also get SayWeather version 6.01 here.
Code: Select all
###############################################################
# Important Notice about SayWeather ver. 6.01 #
# #
# This is the next great version of: #
# The Script Formerly Known as WunderWeather #
# #
# www.wunderground.com is now the Weather Corporation and #
# is no longer providing free weather data to anyone!! #
# This version changes our weather provider to www.apixu.com #
###############################################################
###############################################################
## SayWeather version 6.01 25/08/2018 ##
## Original Script by (lily@disorg.net) ##
## Version 5.0+ updates by SpiKe^^ www.mytclscripts.com ##
## ##
## -> NEW IN VERSION 6.01 <- ##
## - Changed the weather source page to www.apixu.com ##
## - Changed script name from WunderWeather to SayWeather ##
## - Script now requires a Free API Key from www.apixu.com ##
## - Temporarily removed the forecast part of the reply. ##
## Recoded forecast will be available in next version ##
## ##
################## - Version 5.0+ History - ###################
## VERSION 5.03 - 04/12/2017 ##
## - Users can now set/save their default weather location. ##
## example: !w set miami, fl ##
## To use that saved weather location, just type: !w ##
## Does Not use the Eggdrop user file (saves to a file) ##
## - Added metric conversion of most forecast strings. ##
## - Script now allows multiple public weather triggers. ##
## VERSION 5.01 - 28/10/2017 ##
## - All processes and globals are now in a namespace ##
## - Replaced weather forecast (removed by Kiely Allen) ##
## - Replaced metric setting (removed by Kiely &/or David) ##
## - Added 3 new settings for removing colors and/or bolds ##
## - Added Throttle Users - Thanks to user & speechles ##
###############################################################
# International Weather script for Eggdrop.
# Displays in both F/C and MPH/KPH
# Will return weather from www.apixu.com
# Requires TCL8.5 or greater
# Has been tested on Eggdrop 1.8.3
# You must ".chanset #channel +weather" for each chan
# you wish to use this in.
# Usage: !w :or: !w <input>
# Input can be <zip> <city, st> <city, country> <airport code>
###############################################################
package require http
namespace eval saywz { variable sayw
#######################################################
## - Begin Script Settings - Begin Script Settings - ##
#######################################################
# Set your Api Key from www.apixu.com (REQUIRED) #
set sayw(api_key) ""
# Command Char(s) to use for public binds
# Examples: "!" or "."
# Or a Space Separated List: "! . ?"
set sayw(cmdchars) "! ."
# Set public Weather Command(s) #
set sayw(cmds_wz) "w weather"
# Set route/filename for the users default weather locations #
set sayw(usrfile) "scripts/sayweather.users"
# Use Metric as the Primary in channel output?? #
# 1 = Yes, use metric as primary output. (0=no) #
set sayw(metric) 0
# Use Color Changing Temperatures?? (1=yes 0=no) #
set sayw(colortemp) 1
# Use SayWeather Logo Color?? (1=yes 0=no)#
set sayw(colorlogo) 1
# Show Weather Item Names in Bold?? (1=yes 0=no) #
set sayw(boldnames) 1
# Amount user can issue before Throttle
set sayw(throttle_max) 2
# Throttle time (seconds)
set sayw(throttle_time) 30
# Set udef flag used to enable this script by channel
# Use: .chanset #channel +weather
set sayw(udef_flag) "weather"
#######################################################
#### - End Script Settings - End Script Settings - ####
#######################################################
setudef flag $sayw(udef_flag)
set sayw(cmdchars) [split [string trim $sayw(cmdchars)]]
set sayw(cmds_wz) [split [string trim $sayw(cmds_wz)]]
variable nsc [namespace current]
variable name "SayWeather"
variable ver "6.01"
variable auth "SpiKe^^"
proc say_w {nk uh hn ch input} { variable sayw
if {![channel get $ch $sayw(udef_flag)]} { return 0 }
set input [join [lassign [split [string trim $input]] cmd]]
set do ""
if {[string index $cmd 0] in $sayw(cmdchars)} {
set c [string range $cmd 1 end]
if {[lsearch -exact -nocase $sayw(cmds_wz) $c] > -1} { set do wz }
}
if {$do eq ""} { return 0 }
if {[throttle $uh $nk $ch]} { return 0 }
set input [split [regsub -all -- {\s{2,}} [string trim $input] { }]]
lassign [split [string trim $uh ~] @] user host
set id [string tolower "$nk,$user,$ch"]
variable wusers
if {[info exists wusers($id)]} { set wusers(updated) 1
set wusers($id) [lreplace $wusers($id) 0 0 [unixtime]]
set udata $wusers($id)
} else { set udata "" }
if {![llength $input] && $udata eq ""} {
puthelp "PRIVMSG $ch :You must provide a city or zip code or set a default location"
return 0
}
set opt [string tolower [lindex $input 0]] ; set isset 0
if {$opt in {set -set}} { set isset 1
set input [lrange $input 1 end]
if {![llength $input]} {
if {$udata eq ""} {
puthelp "PRIVMSG $ch :You must provide a city or zip code to set as default location"
} else { unset wusers($id)
puthelp "PRIVMSG $ch :Removed all custom settings for $nk"
}
return 0
}
} elseif {![llength $input]} { set input [lindex $udata 1] }
set input [join $input]
set query [::http::formatQuery q $input]
set addr "http://api.apixu.com/v1/current.xml?key=$sayw(api_key)&$query"
set token [::http::geturl $addr]
set html [::http::data $token] ; ::http::cleanup $token
#set open [open scripts/saywz.xml.txt w]; puts $open $html; close $open
######### Get Error Data #########
regexp {<error>(.*?)</error>} $html - err
if {[info exists err]} {
regexp {<code>(.*?)</code><message>(.*?)</message>} $err - code msg
if {$code==1006} { puthelp "PRIVMSG $ch :$input Not Found"
} else { puthelp "PRIVMSG $ch :\00313Error:\003 $code - $msg" }
return 0
}
######## Get Location Data ########
regexp {<location>(.*?)</location>} $html - loc
if {![info exists loc]} {
puthelp "PRIVMSG $ch :\00313Error:\003 No Error and No Location Data??"
return 0
}
foreach term {name region country lat lon localtime} {
regexp "<$term>(.*?)</$term>" $loc - $term
if {![info exists $term]} { set $term "" }
}
unset loc
set where "$name, "
if {$region ne ""} { append where "$region, " }
if {[string match -nocase "*united states*" $country]} {
append where "United States"
} else { append where "$country" }
if {$isset==1} { set wusers(updated) 1
puthelp "PRIVMSG $ch :Set default weather location to: $where"
set wusers($id) [list [unixtime] [split $where]]
return 0
}
######## Get Current Data ########
regexp {<current>(.*?)</current>} $html - curr
if {[info exists curr]} {
set terms [list last_updated temp_f temp_c is_day text wind_mph wind_kph]
lappend terms wind_dir pressure_in pressure_mb precip_in precip_mm
lappend terms humidity cloud feelslike_f feelslike_c vis_miles vis_km
foreach term $terms {
regexp "<$term>(.*?)</$term>" $curr - $term
if {![info exists $term]} { set $term "" }
}
unset curr
}
# // Build current weather string.
if {$sayw(boldnames)==0} { set b "" } else { set b "\002" }
if {$sayw(colorlogo)==0} { set logo "" ; set endl ""
} else { set logo "\00313" ; set endl "\003" }
set puttx ""
append puttx "${b}Temperature:$b[color $temp_f $temp_c]"
if {$sayw(metric)==1} { append puttx " (${temp_f}F) - "
} else { append puttx " (${temp_c}C) - " }
## if {($tempf > $windcf) && (abs($tempf - $windcf) > 1.0)} ##
append puttx "${b}Feels like:$b[color $feelslike_f $feelslike_c]"
if {$sayw(metric)==1} { append puttx " (${feelslike_f}F) - "
} else { append puttx " (${feelslike_c}C) - " }
append puttx "${b}Humidity:$b ${humidity}% - ${b}Wind:$b "
# wind_dir wind_mph wind_kph #
if {$wind_mph < 1} { append puttx "Calm"
} else { append puttx "$wind_dir @ "
if {$sayw(metric)==1} { append puttx "${wind_kph}KPH (${wind_mph}MPH)"
} else { append puttx "${wind_mph}MPH (${wind_kph}KPH)" }
}
# pressure_in pressure_mb #
append puttx " - ${b}Pressure:$b "
if {$sayw(metric)==1} { append puttx "${pressure_mb}mb (${pressure_in}in)"
} else { append puttx "${pressure_in}in (${pressure_mb}mb)" }
append puttx " - ${b}Conditions:$b $text - ${b}Updated:$b $last_updated"
puthelp "PRIVMSG $ch :${logo}$where:$endl $puttx"
} ;# End proc say_w #
#########################################################################
# // Primary Temperature Color Changing.
# // You can change colors based on fahrenheit temperatures.
# // IRC allows only 16 colors. See mIRC color chart.
proc color {temp tempc} { variable sayw
set x "${temp}F"
if {$sayw(metric)==1} { set x "${tempc}C" }
if {$sayw(colortemp)==0} { return " $x" }
if {$temp < 20} { return "\00312 $x\003" }
if {$temp < 45} { return "\00311 $x\003" }
if {$temp < 70} { return "\00309 $x\003" }
if {$temp < 85} { return "\00307 $x\003" }
if {$temp < 100} { return "\00304 $x\003" }
return "\00313 $x\003"
}
#########################################################################
# // Throttle Proc (slightly altered, super action missles) - Thanks user & speechles
# // See this post: http://forum.egghelp.org/viewtopic.php?t=9009&start=3
proc throttle {id nk ch} { variable throttle ; variable sayw
set now [clock seconds]
if {[info exists throttle($id)] && [lindex $throttle($id) 0] > $now} {
lassign $throttle($id) ut cnt
if {$cnt == $sayw(throttle_max)} {
puthelp "PRIVMSG $ch :$nk, you have been Throttled (wait [expr {$ut-$now}] secs)"
}
set throttle($id) [list $ut [incr cnt]]
if {$cnt > $sayw(throttle_max)} { set id 1 } { set id 0 }
} else {
set throttle($id) [list [expr {$now+$sayw(throttle_time)}] 1]
set id 0
}
}
# // sub - clean throttled users
proc throttleclean {} { variable throttle
set now [clock seconds]
foreach {id time} [array get throttle] {
if {[lindex $time 0]<=$now} { unset throttle($id) }
}
}
#########################################################################
proc savewusers {} { variable wusers ; variable sayw
if {![info exists wusers(updated)]} { return }
unset wusers(updated)
if {![array size wusers]} {
file delete $sayw(usrfile)
return
}
set open [open $sayw(usrfile) w]
puts $open "array set wusers [list [array get wusers]]"
close $open
}
#########################################################################
proc wusersclean {} { variable wusers
if {![array size wusers]} { return }
set old [expr {[unixtime] - (86400 * 45)}] ;# 45 days #
foreach {key val} [array get wusers] {
if {$key eq "updated"} { continue }
if {[lindex $val 0] <= $old} {
unset wusers($key)
set wusers(updated) 1
}
}
}
#########################################################################
proc bindtime {mn hr args} { variable nsc
if {$mn eq "02" && $hr in {00 12}} { ${nsc}::wusersclean }
${nsc}::savewusers
if {[string match "*1" $mn]} { ${nsc}::throttleclean }
}
#########################################################################
# // Script reset (unbind/rebind all triggers).
proc w_reset {} { variable sayw ; variable nsc ; set dead "wunder"
foreach x {w say} { ;# remove all version 5+ binds #
if {$x eq "w"} { set wproc "::${dead}::*"
} else { set wproc "${nsc}::say_w" }
foreach wbind [binds $wproc] {
lassign $wbind t f n h p
unbind $t $f $n $p
}
}
foreach char $sayw(cmdchars) {
bind pubm - "?%~\\${char}*" $wproc
}
bind time - * ${nsc}::bindtime
variable wusers
if {[file exists "scripts/${dead}weather.users"]} {
if {![file exists $sayw(usrfile)]} { set newufile 1
if {[array exists wusers]} { unset wusers }
file rename "scripts/${dead}weather.users" $sayw(usrfile)
}
}
if {![array exists wusers] || [info exists newufile]} {
array set wusers ""
if {[file exists $sayw(usrfile)]} { source $sayw(usrfile) }
}
}
w_reset
putlog "\002S\002ay\002W\002eather v$ver Loaded."
} ;## - End namespace eval - End namespace eval - ##