Przeglądaj źródła

(vantrash) cache the date so we don't fetch so much

horgh 15 lat temu
rodzic
commit
f38a631dd5
1 zmienionych plików z 19 dodań i 10 usunięć
  1. 19 10
      vantrash.tcl

+ 19 - 10
vantrash.tcl

@@ -9,26 +9,35 @@ package require http
 
 namespace eval vantrash {
 	# corresponds to zone name on vantrash.ca
-	set zone "vancouver-north-blue"
-	set url "http://vantrash.ca/zones/${zone}/nextpickup.txt"
+	variable zone "vancouver-north-blue"
+	variable url "http://vantrash.ca/zones/${zone}/nextpickup.txt"
 
 	# where to output
-	set channel #newhell
+	variable channel #newhell
 
 	# min hr day month year
 	bind time - {30 21 * * *} vantrash::check
+
+	variable cached_date []
 }
 
 proc vantrash::check {min hour day month year} {
-	set token [http::geturl $vantrash::url]
-	set data [http::data $token]
-	http::cleanup $token
-
-	set next_date [lindex [split $data] 0]
-	set next_date [clock scan $next_date]
-	set next_day [string trim [clock format $next_date -format %e]]
+	# Only fetch new date if we haven't yet found one, or that one is past
+	if {$vantrash::cached_date == "" || [clock seconds] > $vantrash::cached_date} {
+		set token [http::geturl $vantrash::url]
+		set data [http::data $token]
+		http::cleanup $token
+
+		set next_date [lindex [split $data] 0]
+		set vantrash::cached_date [clock scan $next_date]
+		putserv "PRIVMSG $vantrash::channel :(vantrash) Got new date"
+	} else {
+		putserv "PRIVMSG $vantrash::channel :(vantrash) Date is cached"
+	}
 
+	set next_day [string trim [clock format $vantrash::cached_date -format %e]]
 	set tomorrow_day [string trim [clock format [clock scan tomorrow] -format %e]]
+
 	if {$next_day == $tomorrow_day} {
 		putserv "PRIVMSG $vantrash::channel :Garbage day tomorrow!"
 	} else {