Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: News alerts using the BBC's news ticker data file

by zentara (Archbishop)
on Mar 23, 2003 at 17:02 UTC ( [id://245289]=note: print w/replies, xml ) Need Help??


in reply to News alerts using the BBC's news ticker data file

Well here is a simple way to put it into a Tk marquee.

UPDATESet timer to update every 5 minutes.

#!/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('<ButtonRelease>',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_dat +a_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,"<story2.dat") or die "Couldn't open: $!"; #my $ticker_data = do {local $/; <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 ab +ove 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; } ##################################################################

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://245289]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 16:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found