#!/usr/bin/perl #click on marquee to remove use strict; use LWP::Simple; use Tk; my $text; my $mw = tkinit; $mw->geometry('+20+20'); $mw->overrideredirect(1); my $label = $mw->Label( -textvariable=>\$text, -font=>'courier', -bg=>'green', -bd=>4, -relief=>'ridge' )->pack(-fill=>'both'); getnews(); $mw->repeat(300000,\&getnews); $label->bind('',sub{$mw->destroy}); $mw->repeat(160,[sub{$text=~s/(.)(.*)/$2$1/;}]); MainLoop; ##################################################### sub getnews{ my %ticker = parse_ticker_data( 'http://tickers.bbc.co.uk/tickerdata/story2.dat' ); my @stories; foreach my $story (@{ $ticker{'WORLD'} }) { push @stories, $story->{headline} } $text = join ' - ', @stories; $label->update; return; } ######################################################################### sub parse_ticker_data { my $ticker_data_url = $_[0]; my (%ticker, $current_category, @stories_in_this_cat, $last_category, $last_token, $headline, $url); die "No ticker URL supplied to parse_ticker_data()" if (!$ticker_data_url); # Download the ticker data file from the BBC my $ticker_data = get $ticker_data_url; die "Couldn't retrive ticker data" if (!$ticker_data); #for offline testing #open (FILE,"}; #close FILE; # Examine each line in the ticker data file foreach (split /\n/, $ticker_data) { if (/Last update at (\d\d:\d\d)/) { # Extract last updated time $ticker{update_time} = $1 . ' GMT'; $headline = undef; next; } # Set the current category if (/HEADLINE\s (SPORTS|BUSINESS|WORLD|UK|SCI-TECH|TRAVEL|WEATHER|FINANCE)/x) { $current_category = $1; $last_token = 'new'; $headline = undef; next; } my ($token, @data) = split /\s/, $_; my $data = join ' ', @data; # Have we changed categories? If so, then we need to store all the # stories in the old category in the data structure if ($current_category ne $last_category) { if (@stories_in_this_cat) { my @narrow_scoped = @stories_in_this_cat; $ticker{$last_category} = \@narrow_scoped; @stories_in_this_cat = (); } } if ( ($token eq 'HEADLINE') and ($last_token eq 'STORY') ) { # Starting to parse a new headline. Need to put the old one onto # the array though. if ($headline) { # don't want headlines like "UK News", see above push @stories_in_this_cat, { headline => $headline, url => $url }; } $headline = $data; } # Set the URL if ($token eq 'URL') { $url = $data; } # Update for next iteration $last_token = $token; $last_category = $current_category; } # The last category won't be added in the loop, so need to do it # manually. This is nasty, so any improvements would be welcome :-) $ticker{$current_category} = \@stories_in_this_cat; return %ticker; } ##################################################################