Skip to content

Instantly share code, notes, and snippets.

@JohnMertz

JohnMertz/traffic.pl

Created Apr 10, 2020
Embed
What would you like to do?
Ottawa traffic news and alerts
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dump qw | dump |;
use LWP::UserAgent;
use JSON::XS;
my $ua = LWP::UserAgent->new();
my $feed = $ua->get('http://ottawa.netalerts.ca/ottawa1/feed.atom')->content();
my @alerts;
my @lines = split '\n', $feed;
my @summaries = grep(/^\s*<summary/, @lines);
foreach my $item (@summaries) {
$item =~ s/^\s*<summary type="html">(.*)\&lt;br\&gt;\&lt;br&gt;-- Next Info Block --.*<\/summary>$/${1}/;
$item =~ s/\&lt;br\&gt;/|/g;
my @details = split '\|', $item;
my %alert;
foreach (@details) {
my $id = my $content = $_;
$id =~ s/^([^:]*):.*/${1}/;
$content =~ s/^([^:]*): (.*)/${2}/;
$alert{$id} = $content;
push @alerts, \%alert;
}
}
print JSON::XS::encode_json(\@alerts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.