Jump to content

Archived

This topic is now archived and is closed to further replies.

chain

Wunderground weather for eggdrop

Recommended Posts

  • Administrators

Gathers results from wunderground.com and returns them to irc.
Triggers:
 

  •  
  • Default trigger is "!" (This can be changed in the setting at the top)
  •  
  • Both !weather and !forecast will trigger the script.
  •  
  • The results and number of lines for each trigger can be defined at the top of the script.

Syntax:
 

  •  
  • !weather 54011 (returns weather for 45011)
  •  
  • !forecast cincinnati, ohio (returns weather forecast for cincinnati, ohio)
  •  
  • !weather cvg (returns weather for the airport matching cvg
  •  
  • Save this code to a file called wunderground.tcl
  •  
  • Place the file in your eggdrop scripts directory
  •  
  • Add this line to eggdrop.conf -->> source scripts/wunderground.tcl
  •  
  • Rehash or restart your eggdrop
  •  
  • (In the console) type .chanset #channelname +weather "where #channelname is the channel you want the script on in"

See the setup at the top of the script.

##############################################################################################
##  ##  wunderground.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help    ##  ##
##############################################################################################
## To use this script you must set channel flag +weather (ie .chanset #chan +weather)       ##
##############################################################################################
##############################################################################################
##  ##                             Start Setup.                                         ##  ##
##############################################################################################
namespace eval wunderground {
## Change forc to force temp results of F or C.                                         ##  ##
  variable forc "F"
## Edit logo to change the logo displayed at the start of the line                      ##  ##
  variable logo "\017\00304\002W\00304u\00307n\00308d\00311e\00312r\00304g\00307r\00308o\00311u\00312n\00304d\017"
## Edit textf to change the color/state of the text shown                               ##  ##
  variable textf "\017\00304"
## Edit tagf to change the color/state of the Tags:                                     ##  ##
  variable tagf "\017\002"
## Edit weatherline, line1, line2, line3, line4 to change what is displayed             ##  ##
## weatherline is for the !weather trigger and line1-4 are for !forecast                ##  ##
## Valid items are: location weatherstation conditions temperature feelslike            ##  ##
## windspeed winddirection sunset sunrise moon                                          ##  ##
## day1 day2 day3 day4 day5 day6 day7 day8 day9 day10                                   ##  ##
## Do not remove any variables here! Just change them to "" to suppress display         ##  ##
  variable line1 "location weatherstation conditions temperature feelslike windspeed winddirection sunset sunrise moon"
  variable line2 "day1 day2 day3 day4 day5"
  variable line3 "day6 day7 day8 day9 day10"
  variable line4 ""
  variable weatherline "location weatherstation conditions temperature feelslike windspeed winddirection sunset sunrise moon"
## Edit cmdchar to change the !trigger used to for this script                          ##  ##
  variable cmdchar "!"
##############################################################################################
##  ##                           End Setup.                                              ## ##
##############################################################################################
  setudef flag weather
  bind pub -|- [string trimleft $wunderground::cmdchar]weather wunderground::main
  bind pub -|- [string trimleft $wunderground::cmdchar]forecast wunderground::main
}
proc wunderground::main {nick host hand chan text} {
  if {[lsearch -exact [channel info $chan] +weather] != -1} {
    set search [strip $text]; set command [string trimleft $::lastbind $wunderground::cmdchar]
	set div ""; set moon ""; set sunset ""; set sunrise ""; set windspeed ""
    set winddirection ""; set location ""; set weatherstation ""; set temperature ""
	set conditions ""; set feelslike ""; set fconditions ""; set ftemp ""
	set details ""; set forc ""; set count 1; set fconditionsfound ""
	set day1 ""; set day2 ""; set day3 ""; set day4 ""; set day5 ""
	set day6 ""; set day7 ""; set day8 ""; set day9 ""; set day10 ""
    set wundergroundurl "/cgi-bin/findweather/hdfForecast?query=[urlencode $search]"
    set wundergroundsite "www.wunderground.com"
    if {[catch {set wundergroundsock [socket -async $wundergroundsite 80]} sockerr]} {
      return 0
    } else {
      puts $wundergroundsock "GET $wundergroundurl HTTP/1.0"
      puts $wundergroundsock "Host: $wundergroundsite"
      puts $wundergroundsock "User-Agent: Opera 9.6"
      puts $wundergroundsock ""
      flush $wundergroundsock
      while {![eof $wundergroundsock]} {
        set wundergroundvar " [gets $wundergroundsock] "
		regexp -nocase {<div id="(.*?)"\s?>} $wundergroundvar match div
        if {[regexp -nocase {<title>Weather\sForecast\s(.*?)\s\|} $wundergroundvar match location]} {
		  set location "${wunderground::tagf}Location: ${wunderground::textf}${location}"
        } elseif {[regexp -nocase {<div\sclass="b"><a href="\/weatherstation.*">(.*?)<\/a><\/div>} $wundergroundvar match weatherstation]} {
		  set weatherstation "${wunderground::tagf}Weather Station: ${wunderground::textf}${weatherstation}"
        } elseif {[regexp -nocase {<div\sid="curCond">(.+?)<\/div>} $wundergroundvar match conditions]} {
		  set conditions "${wunderground::tagf}Conditions: ${wunderground::textf}${conditions}"
        } elseif {$div == "tempActual" && [regexp -nocase {<span\sclass="b">(.+?)<\/span>} $wundergroundvar match temperature]} {
          regexp -nocase { °(.*?)<} $wundergroundvar match forc
		  if {![string equal -nocase $wunderground::forc $forc] && $forc != ""} {
		    set temperature [forc $temperature $forc]
			set forc $wunderground::forc 
		  }
		  set temperature "${wunderground::tagf}Temperature: ${wunderground::textf}${temperature}º ${forc}"
        } elseif {$div == "tempFeel" && [regexp -nocase {<span\sclass="b">(.+?)<\/span>} $wundergroundvar match feelslike]} {
          regexp -nocase { °(.*?)<} $wundergroundvar match forc
		  if {![string equal -nocase $wunderground::forc $forc] && $forc != ""} {
		    set feelslike [forc $feelslike $forc]
			set forc $wunderground::forc 
		  }
		  set feelslike "${wunderground::tagf}Feels Like: ${wunderground::textf}${feelslike}º ${forc}"
        } elseif {$div == "windCompassContainer" && [regexp -nocase {windCompassSpeed.*>(.+)<\/span>} $wundergroundvar match windspeed]} {
		  set windspeed "${wunderground::tagf}Wind speed: ${wunderground::textf}${windspeed}"
        } elseif {$div == "windCompassContainer" && [regexp -nocase {windCompass.*>(.+)<\/span>} $wundergroundvar match winddirection]} {
		  set winddirection "${wunderground::tagf}Wind speed: ${wunderground::textf}${winddirection}"
        } elseif {$div == "sRise" && [regexp -nocase {class="b">(.+?)<\/span>} $wundergroundvar match sunrise]} {
		  regexp -nocase {span>(.*?)<\/div>} $wundergroundvar match ampm
		  set sunrise "${wunderground::tagf}Sunrise: ${wunderground::textf}${sunrise}${ampm}"
        } elseif {$div == "sSet" && [regexp -nocase {class="b">(.+?)<\/span>} $wundergroundvar match sunset]} {
		  regexp -nocase {span>(.*?)<\/div>} $wundergroundvar match ampm
		  set sunset "${wunderground::tagf}Sunset: ${wunderground::textf}${sunset}${ampm}"
        } elseif {[regexp -nocase {<div\sid="mPhase".*">(.+?)<\/div>} $wundergroundvar match moon]} {
		  set moon "${wunderground::tagf}Moon: ${wunderground::textf}${moon}"
        } elseif {$div == "hourZZ" && $command == "weather"} {
		  msg $chan $wunderground::logo ${wunderground::textf} [subst [regsub -all -nocase {(\S+)} $wunderground::weatherline {$\1}]]
		  close $wundergroundsock
		  return 0
		} elseif {[regexp -nocase {<div\sclass="fctDayDate">(.+)\,} $wundergroundvar match day]} {
		  set day "${wunderground::tagf}${day}"
		} elseif {[string match "*fct_day_*" $div] && [regexp -nocase {"b">(.+?)\s°} $wundergroundvar match ftemp]} {
		  set ftemp [striphtml $ftemp]
		  regexp -nocase {°(.+?)} $wundergroundvar match forc
		  set ftemp "[string map {"| " ""} $ftemp]"
		  foreach {hi lo} $ftemp break
		  if {![string equal -nocase $wunderground::forc $forc] && $forc != ""} {
		    set hi [forc $hi $forc]
			set lo [forc $lo $forc]
			set forc $wunderground::forc
		  }
		  set ftemp "${wunderground::tagf}Temp: ${wunderground::textf}${hi}º|${lo}º ${forc}"
		} elseif {[string match "*<div class=\"fctDayConditions\">*" $wundergroundvar]} {
		  set fconditionsfound "on"
		} elseif {$fconditionsfound == "on"} {
		  regexp {(.*?)\s?$} $wundergroundvar match fconditions
		  set fconditions "${wunderground::tagf}Cond:${wunderground::textf}${fconditions}."
		  set fconditionsfound ""
		} elseif {[string match "*fct_day_*" $div] && [regexp -nocase {popText">(.+?)<\/div} $wundergroundvar match details]} {
		  set details ${wunderground::textf}[string map {"<br />" " "} $details]
		} elseif {[regexp -nocase {popValue">(.+?)<\/div>} $wundergroundvar match chance]} {
		  set details "$details $chance"
		  set day${count} "${day} $ftemp ${fconditions}"
		  incr count
		} elseif {$div == "fct_details"} {
		  if {$wunderground::line1 != ""} {
            msg $chan $wunderground::logo $wunderground::textf [subst [regsub -all -nocase {(\S+)} $wunderground::line1 {$\1}]]
          }
          if {$wunderground::line2 != ""} {
            msg $chan $wunderground::logo $wunderground::textf [subst [regsub -all -nocase {(\S+)} $wunderground::line2 {$\1}]]
          }
          if {$wunderground::line3 != ""} {
            msg $chan $wunderground::logo $wunderground::textf [subst [regsub -all -nocase {(\S+)} $wunderground::line3 {$\1}]]
          }
          if {$wunderground::line4 != ""} {
            msg $chan $wunderground::logo $wunderground::textf [subst [regsub -all -nocase {(\S+)} $wunderground::line4 {$\1}]]
          }
        }		
      }
    }
  }
}
proc wunderground::forc {value fc} {
  if {[string equal -nocase "f" $fc]} {
    if {[expr {(($value - 32) * 5)} == 0]} { return 0 }
    return [format "%.1f" [expr {(($value - 32) * 5) / 9}]]
  } elseif {[string equal -nocase "c" $fc]} {
    if {$value == 0} { return 32 }
    return [format "%.1f" [expr {(($value * 9) / 5) + 32}]]
  }
}
proc wunderground::striphtml {string} {
  return [string map {" \" < < &rt; >} [regsub -all {(<[^<^>]*>)} $string ""]]
}
proc wunderground::urlencode {string} {
  regsub -all {^\{|\}$} $string "" string
  return [subst [regsub -nocase -all {([^a-z0-9\+])} $string {%[format %x [scan "\\&" %c]]}]]
}
proc wunderground::strip {text} {
  regsub -all {\002|\031|\015|\037|\017|\003(\d{1,2})?(,\d{1,2})?} $text "" text
    return $text
}
proc wunderground::msg {chan logo textf text} {
  set text [textsplit $text 50]
  set counter 0
  while {$counter <= [llength $text]} {
    if {[lindex $text $counter] != ""} {
      putserv "PRIVMSG $chan :${logo} ${textf}[string map {\\\" \"} [lindex $text $counter]]"
    }
    incr counter
  }
}
proc wunderground::textsplit {text limit} {
  set text [split $text " "]
  set tokens [llength $text]
  set start 0
  set return ""
  while {[llength [lrange $text $start $tokens]] > $limit} {
    incr tokens -1
    if {[llength [lrange $text $start $tokens]] <= $limit} {
      lappend return [join [lrange $text $start $tokens]]
      set start [expr $tokens + 1]
      set tokens [llength $text]
    }
  }
  lappend return [join [lrange $text $start $tokens]]
  return $return
}
putlog "\002*Loaded* \00304\002W\00304u\00307n\00308d\00311e\00312r\00304g\00307r\00308o\00311u\00312n\00304d\017 \002by \
Ford_Lawnmower irc.GeekShed.net #Script-Help"
Link to comment
Share on other sites



×
×
  • Create New...