GunRunMaps/GIS base files and routes/send_aac_maps.pl

161 lines
5.6 KiB
Perl
Raw Normal View History

2018-08-23 08:33:18 +00:00
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
use Email::MIME::CreateHTML;
use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP;
use Email::Sender::Transport::SMTPS;
use Email::Sender::Transport::SMTP::TLS;
use File::Basename;
use Try::Tiny;
use Data::Dumper;
my $email_csv = '/home/tim/Documents/Projects/2016/Gun Run/maps/routes/marshals_by_position.csv';
my $debug = 0;
my @emails;
my $csv = Text::CSV->new ( { binary => 1 } ) or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $ifh, "<", $email_csv or die "Could not read $email_csv: $!";
$csv->column_names($csv->getline( $ifh ));
while ( my $row = $csv->getline( $ifh ) ) {
my $person = {
"position" => $row->[0],
"name" => $row->[1],
"club" => $row->[2],
"email" => $row->[4],
};
$person->{"name"} =~ s/ +$//g if $person->{"name"};
$person->{"name"} =~ s/ +/ /g if $person->{"name"};
$person->{"email"} =~ s/,//g if $person->{"email"};
push @emails, $person;
}
$csv->eof or $csv->error_diag();
close $ifh;
#@emails = sort { $a->{"position"} cmp $b->{"position"} } @emails;
if ($ARGV[0]) {
my $pos = $ARGV[0];
@emails = grep { $_->{"position"} =~ /^$pos$/ } @emails;
}
foreach my $person (@emails) {
next unless $$person{"name"} and $$person{"email"};
next unless $$person{"club"} =~ /^AAC$/;
my $salutation = $$person{"name"};
my $email = $$person{"email"};
next unless $email =~ /@/;
my $position = $$person{"position"};
#next if ($position =~ /^(68|306|302|74)$/i);
foreach my $m (split(/[, ]+/, $email)) {
printf "%-4s %-25s <%s>\n", $$person{"position"}, $salutation, $m if $debug <= 2;
$m = 'tim@allen.org.za' if $debug == 1;
my $html = qq(
<html><head>
</head>
<body>
<p>
Dear $salutation
</p>
<p>
Thank you for helping out at this year's Gun Run!
</p>
<p>
Attached to this email is a map that gives your marshalling position. <b>You are in position $position on the map.</b> It's important to be in place by <b>6AM</b> on the day, as the police and traffic officials will be conducting spot checks at that point, and if any marshal is not in position, they may call off the race or delay it.
</p>
<p>
You'll notice the map has several phone numbers on it -- if in doubt, please call the Venue Operations Centre (VOC) in case of any emergency, as you'll get a faster response than by calling your captain, Roger, or myself.
</p>
<p>
It can get quite cold in the mornings at this time of year; please be sure to bring something warm (wear it <i>under</i> your bib, please!), as well as sunscreen and a hat. You may also wish to bring water or coffee and something to eat, and even something to sit on. Please do encourage the runners as they run past you!
</p>
<p>
Roger and I will be holding briefings on Monday, Tuesday, Wednesday and Thursday this week at 6PM at Hamilton's. Please ensure you come to at least one to pick up your flag, bib and t-shirt. If you have any questions, please ask us at the briefing, or email us.
</p>
<p>
Regards,
</p>
<p>
Tim
</p>
</body>
</html>
);
my $plain_text = qq(Dear $salutation
Thank you for helping out at this year's Gun Run!
Attached to this email is a map that gives your marshalling position. You are in position $position. It's important to be in place by 6AM on the day, as the police and traffic officials will be conducting spot checks at that point, and if any marshal is not in position, they may call off the race or delay it.
You'll notice the map has several phone numbers on it -- if in doubt, please call the Venue Operations Centre (VOC) in case of any emergency, as you'll get a faster response than by calling your captain, Roger, or myself.
It can get quite cold in the mornings at this time of year; please be sure to bring something warm (wear it under your bib, please!), as well as sunscreen and a hat. You may also wish to bring water or coffee and something to eat, and even something to sit on. Please do encourage the runners as they run past you!
Roger and I will be holding briefings on Monday, Tuesday, Wednesday and Thursday this week at 6PM at Hamilton's. Please ensure you come to at least one to pick up your flag, bib and t-shirt. If you have any questions, please ask us at the briefing, or email us.
Regards,
Tim
);
my $pdf = '/home/tim/Documents/Projects/2016/Gun Run/maps/routes/marshals rev 1.4/'.$$person{"club"}.'_marshal_map_'.$position.'.pdf';
my $filename = basename($pdf);
$filename =~ s/^.*?_//;
die "No PDF at $filename" unless -e $pdf;
my %objects = ( "$filename" => $pdf );
my $mail = Email::MIME->create_html(
header => [
From => 'Timothy Allen <tim@allen.org.za>',
Subject => 'Gun Run marshalling position',
To => $m,
],
body => $html,
text_body => $plain_text,
objects => \%objects,
);
# my $g_transport = Email::Sender::Transport::SMTPS->new(
# host => 'smtp.gmail.com',
# ssl => 'ssl',
# sasl_username => 'trallen@gmail.com',
# debug => 1, # or 1
# );
# my $t_transport = Email::Sender::Transport::SMTPS->new(
# host => 'mail.treehouse.org.za',
# sasl_username => 'tim',
# debug => 1, # or 1
# );
my $g_transport = Email::Sender::Transport::SMTP::TLS->new(
host => 'smtp.gmail.com',
username => 'trallen@gmail.com',
password => '',
);
my $t_transport = Email::Sender::Transport::SMTP::TLS->new(
host => 'mail.treehouse.org.za',
username => 'tim',
password => '',
);
try {
sendmail($mail, { transport => $g_transport }) if $debug <= 1;
# sendmail($mail, { transport => $t_transport }) if $debug <= 1;
} catch {
warn "\n\nError sending email for position $position\n\n";
warn $_;
};
}
# sleep 5;
last if $debug == 1;
}
# vim: paste