vantrash.tcl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #
  2. # 2010-10-03
  3. #
  4. # Notify specific channels when garbage pickup is next day
  5. # Uses next-day API from http://vantrash.ca
  6. #
  7. package require http
  8. namespace eval vantrash {
  9. # corresponds to zone name on vantrash.ca
  10. variable zone "vancouver-north-blue"
  11. variable url "http://vantrash.ca/zones/${zone}/nextpickup.txt"
  12. # where to output
  13. variable channel #tea
  14. # min hr day month year
  15. bind time - {30 19 * * *} vantrash::check
  16. bind time - {30 20 * * *} vantrash::check
  17. bind time - {30 21 * * *} vantrash::check
  18. bind pub -|- "!vantrash" vantrash::handler
  19. variable cached_date []
  20. }
  21. proc vantrash::handler {nick uhost hand chan argv} {
  22. vantrash::check * * * * *
  23. }
  24. proc vantrash::check {min hour day month year} {
  25. # Only fetch new date if we haven't yet found one, or that one is past
  26. if {$vantrash::cached_date == "" || [clock seconds] > $vantrash::cached_date} {
  27. set token [http::geturl $vantrash::url]
  28. set data [http::data $token]
  29. set ncode [http::ncode $token]
  30. http::cleanup $token
  31. if {$ncode != 200} {
  32. putserv "PRIVMSG $vantrash::channel :(vantrash) Error (${ncode}) fetching next pickup date. (Cached date is expired or not present): ${data}"
  33. return
  34. }
  35. set next_date [lindex [split $data] 0]
  36. set vantrash::cached_date [clock scan $next_date]
  37. }
  38. set next_day [string trim [clock format $vantrash::cached_date -format %e]]
  39. set tomorrow_day [string trim [clock format [clock scan tomorrow] -format %e]]
  40. if {$next_day == $tomorrow_day} {
  41. putserv "PRIVMSG $vantrash::channel :Garbage day tomorrow!"
  42. }
  43. }
  44. putlog "vantrash.tcl loaded"