Marc Cramdal's blog

Monday, July 10, 2006

Kopete countdown script

Today is my first holiday day. So I wanted to have a countdown for my away message showing the time left before my holidays and then a nice "Vacances !!!!" (it's holiday in french) message.

To achieve this, I just wrote a little Ruby script this time. It's the first time I write such a big (!) Ruby script and it's very easy with some help.

So this is the result :
Image Hosted by ImageShack.us

And below you can find the script.


#!/usr/bin/ruby

require "time"

###### CHANGE SETTINGS HERE ######
# Can be any valid date time of the type : Wed Apr 09 08:56:04
time0 = String "15:00"

# Message before event occurs, use %% to put the number of hours left
message_before = String "%% heures avant (ip)"

# Message after event occurs. Same use of %% here. It will be a positive number of hours.
message_after = String "Vacances !!!!"

# Update the message every sleep_time seconds
sleep_time = Integer 60
#######

target = Time.parse(time0)

while true
current = Time.new
difference = target - current
hours = (difference.abs / 3600).floor;
minutes = ((difference.abs - hours*3600)/60).floor

# Format the number of hours left
time_left = String(hours) + ":" + sprintf("%02d",minutes)

if difference > 0 # If the event isn't past
`dcop kopete KopeteIface setAway "#{message_before.sub("%%", time_left)}" false`
else
`dcop kopete KopeteIface setAway "#{message_after.sub("%%", time_left)}" false`
break
end

sleep sleep_time;

end