Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################################################################################
- # Name m00nie::weather
- # Description Uses wunderground API to grab some weather...
- # Version 1.6 - Corrects wind conditions when set to Metric, or Combined
- # Adjusted styling for better readability
- # 1.5 - Corrects flag detection for chans and adds option for current conditions to display
- # both F and C temps in the same output. Might add this to forecast in future version
- # 1.4 - Warn on more than 5 possible restults (stops LOTS of spam)
- # 1.3 - Added warning when mutlple/unclear matches
- # 1.2 - Correct RFC compliant output for some IRCDs (thanks to Alan and Robert for highlighting)
- # 1.1 - F/C tempreture option
- # 1.0 - Initial Release
- # Website https://www.m00nie.com
- # Notes Grab your own key @ http://www.wunderground.com/weather/api
- #########################################################################################
- namespace eval m00nie {
- namespace eval weather {
- package require http
- package require tdom
- bind pub - !w m00nie::weather::current_call
- bind pub - !wl m00nie::weather::location
- bind pub - !fc m00nie::weather::forecast_call
- variable version "1.6"
- # Set forc to 0 for tempreture in Celsius, 1 for Farenheit or 2 for both!
- # At the moment forecasts can only use 0 or 1, 2 will default back to Celsius
- variable forc "2"
- setudef flag weather
- variable key "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
- ::http::config -useragent "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
- proc current_call {nick uhost hand chan text} {
- set location [verify $hand $chan $text]
- if {$location != 0} {
- current $location $chan
- }
- }
- proc forecast_call {nick uhost hand chan text} {
- set location [verify $hand $chan $text]
- if {$location != 0} {
- forecast $location $chan
- }
- }
- # Allow a user to save their location so they can search without defining it each time
- proc location {nick uhost hand chan text} {
- if {![validuser $hand]} {
- set mask [maskhost [getchanhost $nick $chan]]
- adduser $nick $mask
- chattr $nick -hp
- putlog "m00nie::weather::location added user $nick with host $uhost"
- }
- setuser $nick XTRA m00nie:weather.location $text
- puthelp "PRIVMSG $chan :Default weather location for $nick set to $text."
- putlog "m00nie::weather::location $nick set their default location to $text."
- }
- # Search for current weather
- proc verify {hand chan text} {
- if {(![channel get $chan weather])} {
- putlog "m00nie::weather::search Trigger seen but channel doesnt have +weather set!"
- return 0
- }
- if {$text != ""} {
- set location $text
- } else {
- set location [getuser $hand XTRA m00nie:weather.location]
- }
- if {[string length $location] == 0 || [regexp {[^0-9a-zA-Z,. ]} $location match] == 1} {
- putlog "m00nie::weather::search location b0rked or no location said/default? Argument: $location"
- puthelp "PRIVMSG $chan :Did you ask to search somewhere? Or use !wl to set a default location"
- return
- } else {
- return $location
- }
- }
- proc current {location chan} {
- putlog "m00nie::weather::current is running against location: $location"
- set rawpage [getinfo $location conditions]
- set doc [dom parse $rawpage]
- set root [$doc documentElement]
- # Check for no results!
- set notfound [$root selectNodes /response/error/description/text()]
- if {[llength $notfound] > 0 } {
- set errormsg [$notfound nodeValue]
- putlog "m00nie::weather::current ran but could not find any info for $location or an API error occured: $errormsg"
- puthelp "PRIVMSG $chan :$errormsg"
- return
- }
- # Check for multiple results
- set multi [$root selectNodes /response/results/result]
- if {[llength $multi]} {
- putlog "m00nie::weather::current multiple results found"
- # Lets check we dont have LOADS of results to spam
- set i 0
- foreach place [$root selectNodes "/response/results/result"] {
- incr i
- if {$i >= 5} {
- puthelp "PRIVMSG $chan :Your search returned more than 5 results. Please try a more specific search."
- return
- }
- }
- puthelp "PRIVMSG $chan :Multiple results found pick one and run again"
- foreach place [$root selectNodes "/response/results/result"] {
- set name [lindex [$place selectNodes "name"] 0]
- set country [lindex [$place selectNodes "country"] 0]
- if {[$country text] eq "US"} {
- set state [lindex [$place selectNodes "state"] 0]
- puthelp "PRIVMSG $chan : - [$name text] [$state text] [$country text]"
- } else {
- puthelp "PRIVMSG $chan : - [$name text] [$country text]"
- }
- }
- return
- }
- set city [[$root selectNodes /response/current_observation/display_location/full/text()] nodeValue]
- if { $m00nie::weather::forc == 0 } {
- foreach var { observation_time weather temp_c wind_dir wind_kph wind_gust_kph feelslike_c precip_today_string } {
- set $var [[$root selectNodes /response/current_observation/$var/text()] nodeValue]
- }
- set spam "Current weather for \002$city\002 ($observation_time) Current conditions: $weather, Temperature: $temp_c C, Wind: From $wind_dir at $wind_kph KPH Gusting to $wind_gust_kph KPH, Rain today: $precip_today_string, Feels like: $feelslike_c C"
- } elseif { $m00nie::weather::forc == 1} {
- foreach var { observation_time weather temp_f wind_string feelslike_f precip_today_string } {
- set $var [[$root selectNodes /response/current_observation/$var/text()] nodeValue]
- }
- set spam "Current weather for \002$city\002 ($observation_time) Current conditions: $weather, Temperature: $temp_f F, Wind: $wind_string, Rain today: $precip_today_string, Feels like: $feelslike_f F"
- } elseif { $m00nie::weather::forc == 2} {
- foreach var { observation_time weather temp_f temp_c wind_dir wind_mph wind_kph wind_gust_mph wind_gust_kph feelslike_string precip_today_string } {
- set $var [[$root selectNodes /response/current_observation/$var/text()] nodeValue]
- }
- set spam "Current weather for \002$city\002 ($observation_time) \002Current conditions:\002 $weather, \002Temperature:\002 $temp_f F ($temp_c C), \002Wind:\002 From the $wind_dir at $wind_mph MPH ($wind_kph KPH) Gusting to $wind_gust_mph MPH ($wind_gust_kph KPH), \002Rain today:\002 $precip_today_string, \002Feels like:\002 $feelslike_string"
- } else {
- putlog "m00nie::weather::current $m00nie::weather::forc is not a valid value for forc..."
- return
- }
- puthelp "PRIVMSG $chan :$spam"
- }
- proc forecast {location chan} {
- putlog "m00nie::weather::forecast is running against location: $location"
- set rawpage [getinfo $location forecast]
- set doc [dom parse $rawpage]
- set root [$doc documentElement]
- set dayList [$root selectNodes /response/forecast/txt_forecast/forecastdays/forecastday/title/text()]
- if { ($m00nie::weather::forc == 0) || ($m00nie::weather::forc == 2) } {
- set foreList [$root selectNodes /response/forecast/txt_forecast/forecastdays/forecastday/fcttext_metric/text()]
- } elseif { $m00nie::weather::forc == 1 } {
- set foreList [$root selectNodes /response/forecast/txt_forecast/forecastdays/forecastday/fcttext/text()]
- } else {
- putlog "m00nie::weather::forecast $m00nie::weather::forc is not a valid value for forc..."
- return
- }
- puthelp "PRIVMSG $chan :Three day forecast for \002$location\002"
- set x 0
- while { $x < 6 } {
- set dayname [[lindex $dayList $x] nodeValue]
- set fore [[lindex $foreList $x] nodeValue]
- puthelp "PRIVMSG $chan :\002$dayname:\002 $fore"
- incr x
- }
- }
- proc getinfo {location type} {
- regsub -all -- { } $location {%20} location
- set url "http://api.wunderground.com/api/$m00nie::weather::key/$type/q/$location.xml"
- putlog "m00nie::weather::getinfo grabbing data from $url"
- for { set i 1 } { $i <= 5 } { incr i } {
- set xmlpage [::http::data [::http::geturl "$url" -timeout 10000]]
- if {[string length xmlpage] > 0} { break }
- }
- putlog "m00nie::weather::getinfo xmlpage length is: [string length $xmlpage]"
- if { [string length $xmlpage] == 0 } {
- error "wunderground returned ZERO no data :( or we couldnt connect properly"
- }
- return $xmlpage
- }
- }
- }
- putlog "m00nie::weather $m00nie::weather::version loaded"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement