coolguylentz

Weather Script MCD

Dec 29th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. # weather.tcl -- 2.2.1
  2. #
  3. # Returns the current weather for the city or postcode using the iGoogle
  4. # API for weather.
  5. #
  6. # Copyright (c) 2011 HM2K, the eggtcl project
  7. #
  8. # Name: Weather Lookup
  9. # Author: HM2K <[email protected]>
  10. # License: http://www.opensource.org/licenses/bsd-license.php The BSD 2-Clause License
  11. # Link: http://www.hm2k.com/posts/weather-tcl
  12. # Labels: weather, lookup, google, api
  13. # Updated: 08-Jul-2011
  14. #
  15. ###Usage
  16. # > .wz london
  17. # <Bot> HM2K, * Weather: London, England: Mostly Cloudy, 8ºC Humidity: 87% Wind:
  18. # W at 9 mph
  19. #
  20. ###Revisions
  21. # 2.2.1 - Improved removing of temperature unit flag from argument
  22. # 2.2 - Uses setudef
  23. # - Uses namespace
  24. # - Added init and deinit procs
  25. # - Added optional 'weather.settings.tcl' file for custom settings
  26. # 2.1 - Added -c/-f syntax
  27. # - Fix tabbing
  28. # - Syntax update
  29. # 2.0.6 - added command check for ::htmlparse::mapEscapes proc
  30. # 2.0.5 - added http timeout check; improvements to code syntax
  31. # 2.0.4 - improved package defintion checks and added utf-8 support
  32. # 2.0.3 - better requirement checking and better file header
  33. # 2.0.2 - now returns temperature in C or F, depending on what you define.
  34. # 2.0.1 - fixed weather.tcl $info(problem) -> $data(problem) (tnx Pixelz)
  35. # 2.0 - a complete rewrite using google's api to gather the weather info
  36. # 1.x - based on a script by Ycarus
  37. #
  38. ###Todo
  39. # 3.0 - Switch to the Yahoo Weather API (developer.yahoo.com/weather)
  40. # 3.0 - Add Subset/Sunrise
  41. # 3.0 - Weather forecast (.wzf)
  42. # 3.0 - Set default user location (-d)
  43. #
  44. ###Credits
  45. # Web hosting by Phurix <www.phurix.co.uk>
  46. # Shell hosting by Gallush <www.gallush.com>
  47. #
  48. # Please consider a donation. Thanks! http://tinyurl.com/hm2kpaypal
  49.  
  50. #
  51. # Settings
  52. #
  53.  
  54. # Namespace
  55. namespace eval ::wz {
  56. variable version
  57. set version "2.2.1"; #current version of this script
  58.  
  59. variable wz
  60. # Default settings
  61. set wz(lang) "en"; #language
  62. set wz(cmd) ".wz"; #public command trigger
  63. set wz(dcccmd) "wz"; #dcc command trigger
  64. set wz(prefix) "* Weather:"; #output prefix
  65. set wz(tempu) "F"; # temperature unit [C/F]
  66. set wz(output) "\002%s:\002 %s, %sº%s %s %s"; #format for the output
  67. set wz(problem) "Problem:";
  68. set wz(errormsg) "Error: No information could be found for";
  69. set wz(usage) "Usage: $wz(cmd) -<c/f> <city|postcode,country|country>";
  70. set wz(ua) "MSIE 6.0"; #simulate a browser's user agent
  71. set wz(url) "http://www.google.com/ig/api"; #url
  72. set wz(timeout) 25000; #timeout
  73. set wz(timeoutmsg) "Error: Connection timed out"; #timeout message
  74. # Note: Copy the above "Default settings" section into "weather.settings.tcl"
  75. # to customise your settings. Then they will be used instead.
  76.  
  77. # Settings file
  78. if {[catch {source scripts/weather.settings.tcl} err]} {
  79. #putlog "Warning: 'weather.settings.tcl' was not loaded"
  80. }
  81. }
  82.  
  83. #
  84. # Procedures
  85. #
  86.  
  87. # Initialization
  88. proc ::wz::init {args} {
  89. variable wz
  90. variable version
  91. # Package Definition
  92. package require eggdrop 1.6; #see http://geteggdrop.com/
  93. package require Tcl 8.4; #see http://tinyurl.com/6kvu2n
  94. if {[catch {package require http 2.0} err]} {
  95. putlog "[info script] error: $err"
  96. putlog "http 2.0 package or above is required, see http://wiki.tcl.tk/1475";
  97. }
  98. if {[catch {package require htmlparse} err]} {
  99. putlog "[info script] error: $err"
  100. putlog "Tcllib is required, see http://wiki.tcl.tk/12099";
  101. }
  102. # User defined channel flag
  103. setudef flag weather
  104. # Binds
  105. bind pub - $wz(cmd) [namespace current]::pub
  106. bind dcc -|- $wz(dcccmd) [namespace current]::dcc
  107. bind evnt -|- prerehash [namespace current]::deinit
  108. # Loaded
  109. putlog "weather.tcl $version loaded"
  110. }
  111.  
  112. # Deinitializaion
  113. proc ::wz::deinit {args} {
  114. catch {unbind pub -|- {* *} [namespace current]::pub}
  115. catch {unbind dcc -|- {*} [namespace current]::dcc}
  116. catch {unbind evnt -|- prerehash [namespace current]::deinit}
  117. namespace delete [namespace current]
  118. }
  119.  
  120. # Public command
  121. proc ::wz::pub { nick uhost hand chan arg } {
  122. variable wz
  123. # check channel permission
  124. if {[channel get $chan weather]<1} { return }
  125. set arg [split $arg]
  126. if {[llength $arg]==0} {
  127. putserv "NOTICE $nick :$wz(usage)"
  128. return
  129. }
  130. set result [::wz::get $arg]
  131. putserv "PRIVMSG $chan :$nick, $wz(prefix) $result"
  132. }
  133.  
  134. # DCC command
  135. proc ::wz::dcc {ha idx arg} {
  136. variable wz
  137. set arg [split $arg]
  138. if {[llength $arg]==0} {
  139. putdcc $idx $wz(usage)
  140. return
  141. }
  142. set result [::wz::get $arg]
  143. putdcc $idx $result
  144. }
  145.  
  146. # Get
  147. proc ::wz::get { arg } {
  148. variable wz
  149. set tempu $wz(tempu)
  150. set tempf [lsearch [string tolower $arg] "-f"]
  151. if {$tempf >-1} {
  152. set arg [lreplace $arg $tempf $tempf]
  153. set tempu "F"
  154. }
  155. set tempc [lsearch [string tolower $arg] "-c"]
  156. if {$tempc >-1} {
  157. set arg [lreplace $arg $tempc $tempc]
  158. set tempu "C"
  159. }
  160.  
  161. set query [::http::formatQuery weather $arg hl $wz(lang)]
  162. #set url [format $wz(url) $arg $wz(lang)]
  163.  
  164. set http [::http::config -useragent $wz(ua) -urlencoding "utf-8"]
  165. set http [::http::geturl $wz(url)?$query -timeout $wz(timeout)]
  166. if {[::http::status $http] eq "timeout"} {
  167. ::http::cleanup $http
  168. return $wz(timeoutmsg)
  169. }
  170. set data [::http::data $http]
  171. set data [::wz::mapEscapes $data]
  172. #set data [::wz::parse $data "forecast_information"][::wz::parse $data "current_conditions"]
  173. ::http::cleanup $http
  174.  
  175. set temp [expr {([string tolower $wz(tempu)] == "f")?"temp_f":"temp_c"}]
  176.  
  177. set info(city) [::wz::parsedata $data city]
  178. set info(condition) [::wz::parsedata $data condition]
  179. set info(temp) [::wz::parsedata $data $temp]
  180. set info(humidity) [::wz::parsedata $data humidity]
  181. set info(wind) [::wz::parsedata $data wind_condition]
  182. set info(problem) [::wz::parsedata $data problem_cause]
  183.  
  184. if {([info exists info(problem)]) && ($info(problem) ne "")} {
  185. return "$wz(problem) $info(problem)"
  186. }
  187. if {([info exists info(city)]) && ($info(city) == "")} {
  188. return "$wz(errormsg) $arg"
  189. }
  190.  
  191. return [format $wz(output) $info(city) $info(condition) $info(temp) $tempu $info(humidity) $info(wind)]
  192. }
  193.  
  194. # Parse
  195. proc ::wz::parse { data arg } {
  196. set arg [string tolower $arg]
  197. set matched ""
  198. set result ""
  199. regexp "<$arg>(.+?)</$arg>" $data matched result
  200. return $result
  201. }
  202.  
  203. # Parse data
  204. proc ::wz::parsedata { data arg } {
  205. set arg [string tolower $arg]
  206. set matched ""
  207. set result ""
  208. regexp "<$arg data=\"(\[^\"\]+)\"/>" $data matched result
  209. return $result
  210. }
  211.  
  212. # HTML Escape
  213. proc ::wz::mapEscapes {data} {
  214. if {[info commands ::htmlparse::mapEscapes] == ""} {
  215. putlog "Invalid command name \"::htmlparse::mapEscapes\""
  216. putlog "Tcllib is required, see http://wiki.tcl.tk/12099"
  217. } else {
  218. return [::htmlparse::mapEscapes $data]
  219. }
  220. }
  221.  
  222. ::wz::init
Add Comment
Please, Sign In to add comment