Skip to content

Instantly share code, notes, and snippets.

@JohnMertz
Created April 10, 2020 11:44
Show Gist options
  • Save JohnMertz/b1c4d51a3c5abc43c0f3c3a13613a593 to your computer and use it in GitHub Desktop.
Save JohnMertz/b1c4d51a3c5abc43c0f3c3a13613a593 to your computer and use it in GitHub Desktop.
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