Conky buses!
25/7/2009 - 00:23:24 - 0 comments - By D47
So to show off my new code tags I'm going to show you a script I wrote to get the upcoming bus times displayed right there on your desktop!

There are three bits of code required here, first there is the code to fetch the timetable data for the current day from your local bus service. You're unlucky if they don't provide the data in html form, although if the timetable doesn't change often you can simply create the timetable file yourself.

This is my code:
getbusses.sh:

#!/bin/bash
curl "http://jp.translink.com.au/TransLinkPublicTimetable.asp?RouteCode=301&TimetableKey=1849&Direction=inbound&TimetableVehicle=BTBus" 2>/dev/null | \
awk '/.*Doomben Station.*/{ getline; print; getline; print; getline; print; getline; print; getline; print; getline; print;}' | \
sed "s:.*<td class=\"TimetableDetails\">[ ^I]*\([0-9]*\:[0-9]*.*\)</td>.*:\1:p" -n \
    > ~/scripts/buses


That one is quite simple, it shouldn't be hard to make one up for your timetable provider.

The next piece of code is the workhorse. This one is executed every minute by conky.
nextbus.pl:

#!/usr/bin/perl
use DateTime;

my ($sec,$min,$hour,$day,$month,$yr,@rest) =   localtime(time);
my $line;
my $outputCount = 3;                        #Number of times to output (from current time)
my $previousLine;                        #Used to detect duplicates (this is commonplace practice at translink...)

open(BUSES, "<", "/home/d47/scripts/buses");

while ($line = <BUSES>) {
    if($line == $previousLine) {                #Detect duplicates
        next;
    }

    $previousLine = $line;

    if($outputCount == 0) {
        exit;
    }

    chop $line;

    my @checkTime = split(":", $line);

    my $cHour = @checkTime[0]+0;                #Hour

    if($cHour == 12) {
        $cHour = 0;
    }

    if(substr(@checkTime[1], 2, 2) eq "pm") {        #Adjust to 24Hr time
        $cHour += 12;
    }

    my $cMin = substr(@checkTime[1], 0, 2)+0;        #minute


    if($cHour < $hour) {
        next;
    }

    if($cHour == $hour && $cMin < $min) {
        next;
    }

    my $timeUntil = ($cHour - $hour) * 60 - ($min - $cMin);

    print $line, " (", $timeUntil, " mins)\n";

    $outputCount--;
}


As you can see it is rather strait forward, it cycles through each line on the "buses" file and determines if that bus is yet to come, if it is then it is printed. The program exits when $outputCount amount of buses are printed.

The third and last piece of code is the easiest. Its the code to put into your .conkyrc file, here is my whole file to show context.
~/.conkyrc:

use_xft yes
xftfont Bitstream Vera Sans Mono:style=Bold:size=8

update_interval 1
total_run_times 0
double_buffer yes
text_buffer_size 2048

own_window yes
own_window_type normal
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager

background yes

minimum_size 250 0
maximum_width 250

default_color cyan
draw_shades no

color0 cyan
color1 red
color2 white

border_margin 5
border_width 2

alignment top_right
gap_x 25
gap_y 35

no_buffers no
net_avg_samples 2

override_utf8_locale yes

no_buffers yes

TEXT
DATE ${hr 2}

${color0}${font StyleBats:size=16}4${font}   EST${color}${alignc 45}${color2}${font Arial Black:size=30}${time %H:%M}${font}${color}
${alignc}${time %A %d %Y}
${alignc}${time %s}

${color0}${font StyleBats:size=16}8${font}   UTC${color}${alignc 45}${color2}${font Arial Black:size=30}${utime %H:%M}${font}${color}
${alignc}${utime %A %d %Y}
${alignc}${utime %s}

SYSTEM ${hr 2}
${color0}${font StyleBats:size=16}A${font}${color}   CPU:      ${color1}${cpu cpu0}%${color} ${alignr}${color2}${cpubar cpu0 8,60}${color}
${color0}${font StyleBats:size=16}y${font}${color}   RAM:      ${color1}${memperc}% ${mem}${color} ${alignr}${color2}${membar 8,60}${color}
${color0}${font StyleBats:size=16}q${font}${color}   Uptime:   ${color1}${uptime}${color}

WEB USAGE ${hr 2}
${color0}${font StyleBats:size=16}x${font}${color}   P:        ${color1}${execi 60 awk '{print $1}' ~/scripts/currentusage} /  60GiB${color}     ${color2}${execibar 60 awk '{print ($1/63)*100}' ~/scripts/currentusage}
${color0}${font StyleBats:size=16}g${font}${color}   OP:       ${color1}${execi 60 awk '{print $2}' ~/scripts/currentusage} / 140GiB${color}     ${color2}${execibar 60 awk '{print ($2/143)*100}' ~/scripts/currentusage}${color}

BUSES ${hr 2}
${color0}${font StyleBats:size=16}B${font}${color}   Next buses into the city:
${color1}${execi 3600 ~/scripts/getbuses.sh}${execi 60 ~/scripts/nextbus\ 2.0.pl}${color}


And that's it! All I need to do now is become social and want to know when I can travel to the city to go clubbing! Oh goodie!
 
 
Post Comment:
Name:
Message:
Captcha: captcha
 
 

By Dylan McGannon. Feel free to copy, use and modify anything you find here.

Valid HTML 4.01 Strict / Valid CSS!