Hi All

I travel along the M62 to get to work and the traffic is a bit of a pain. I wrote this simple LWP script to grab traffic reports from RAC website and pick out those of interest to me.

The code does rely on the structure of the web page not changing which is not the best solution, so if any Monks out there know of a structured source for traffic reports, i'd like to hear from you.

Regards
Orthanc

#!/usr/bin/perl use LWP::Simple; $| = 1; $url = "http://www.rac.co.uk/scripts/traffic.htt?area=5"; $res_okay = 1; $req_motorway = ($ARGV[0]) ? $ARGV[0] : "M62|A58"; $req_motorway =~ s/\,/\|/g; $clear = `clear`; print "$clear"; print "Getting reports for $req_motorway\n\n"; @html=get("$url"); foreach (@html) { $html .= $_; } @htmllines = split(/\n/,$html); %motorways = (); foreach (@htmllines) { if(/bodytextonehead/) { m/bodytextonehead\"\>(.*)\</i; $mway = "$1|$i"; } if(/bodytexttwo/ && /\|/) { m/bodytexttwo\"\>\| (.*) \|\</i; $motorways{$mway}{'time'} = $1; m/ltnbaro_0(\d+)\.gif/i; $motorways{$mway}{'baro'} = $1; } if(/bodytexttwofade/) { m/bodytexttwofade\"\>(.*)\<\/FONT/i; $motorways{$mway}{'report'} = $1; $motorways{$mway}{'report'} =~ s/'/\'/g; $motorways{$mway}{'report'} =~ s/&/\&/g; } $i++; } foreach $key (keys %motorways) { ($mway,$x) = split(/\|/,$key); next if($mway !~ m/$req_motorway/); print "$mway\n"; print "Time: $motorways{$key}{'time'}\n"; print "Baro: $motorways{$key}{'baro'}\n"; print "Report: $motorways{$key}{'report'}\n"; } exit;

Replies are listed 'Best First'.
RE: Road Traffic Check
by ar0n (Priest) on Jun 20, 2000 at 01:00 UTC
    I am by no means an expert, but, as a Good Monk there are just some things
    I need to say.
    • Cool!
    • print `clear`; instead of first setting up a variable is easier
    • stripping every newline character from an array can be done easier by doing a chomp(@html), so your code would look thusly:
      chomp(@html = get($url));
    • Also, I believe
      while(@html) { #parse code here }
      is faster than foreach since it doesn't need to load the whole array, just one line at a time.
    neato application of LWP, though... :-)

    -- ar0n
RE: Road Traffic Check
by Odud (Pilgrim) on Jun 20, 2000 at 23:17 UTC
    I used to live in the Manchester area - you have my sympathy if you travel the motorways every day. Did they ever finish the motorway ring around Manchester? - when I was there it petered out around Stockport.

      I only travel from one junction to the next between Leeds and Halifax. The problem is that it is the busiest part.

      This helps me decide whether to go through Bradford instead.

      To anyone:
      Does anyone have any info about a source for traffic reports?, no one replied.

      Orthanc