GunRunMaps/send_aac_maps.pl

223 lines
8.8 KiB
Perl
Raw Permalink Normal View History

2018-08-23 08:33:18 +00:00
#!/usr/bin/perl
2018-08-23 08:53:02 +00:00
# Debug level 0: Send email
# Debug level 1: Send email to myself
# Debug level 2: Print names and email addresses
2018-08-23 08:33:18 +00:00
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;
2018-10-11 12:02:09 +00:00
use Term::ReadKey;
2018-08-23 08:33:18 +00:00
use File::Basename;
use Try::Tiny;
use Data::Dumper;
2023-09-15 08:02:53 +00:00
my $map_dir = '/home/tim/Documents/Projects/2023/Gun Run/Maps/';
2022-09-16 01:55:30 +00:00
my $email_csv = "$map_dir/marshals_by_position.csv";
my $pdf_dir = "$map_dir/GIS Base Map/Marshals/";
2023-09-15 08:02:53 +00:00
my $debug = 2;
2018-08-23 08:33:18 +00:00
my @emails;
2018-10-13 09:29:34 +00:00
if ($debug == 2) {
print "WARNING: Printing names. Not sending emails!\n";
} elsif ($debug == 1) {
print "WARNING: Sending emails to a TEST address.\n";
} elsif ($debug == 0) {
print "Sending emails to...\n";
} else {
print "ERROR: Invalid debug value\n";
}
2018-10-11 12:02:09 +00:00
my $password;
if (-f "$map_dir/password_store") {
open(my $fh, '<:encoding(UTF-8)', "$map_dir/password_store") or die "Could not open password store: $!";
while (my $row = <$fh>) {
chomp($password = $row);
}
close($fh)
} else {
print "Please type your email password: ";
#chomp($password = <STDIN>);
ReadMode('noecho');
chomp($password = ReadLine(0));
print "\n";
ReadMode('normal');
}
2018-08-23 08:33:18 +00:00
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 = {
2022-09-16 01:55:30 +00:00
"day" => $row->[0],
"position" => $row->[1],
"club" => $row->[4],
2018-08-23 08:53:02 +00:00
"email" => $row->[6],
2018-08-23 08:33:18 +00:00
};
2022-09-16 01:55:30 +00:00
#$person->{"name"} =~ s/ +$//g if $person->{"name"};
#$person->{"name"} =~ s/ +/ /g if $person->{"name"};
2018-08-23 08:33:18 +00:00
$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;
}
2018-08-23 08:53:02 +00:00
#print Dumper(@emails);
2018-08-23 08:33:18 +00:00
foreach my $person (@emails) {
2022-09-16 01:55:30 +00:00
next unless $$person{"email"};
2023-09-15 08:02:53 +00:00
next unless $$person{"club"} =~ /^(AAC|Atlantic Athletic.*?)$/;
next if $$person{"position"} !~ /\d\d\w/;
2022-09-16 01:55:30 +00:00
#my $name = $$person{"name"};
2018-08-23 08:33:18 +00:00
my $email = $$person{"email"};
2018-10-11 08:28:05 +00:00
my $club = $$person{"club"};
2018-08-23 08:33:18 +00:00
next unless $email =~ /@/;
2022-09-16 01:55:30 +00:00
my $day = $$person{"day"};
2018-08-23 08:33:18 +00:00
my $position = $$person{"position"};
2018-10-11 12:02:09 +00:00
my $position_time;
2023-09-15 08:02:53 +00:00
if ($day eq '1' and $position =~ /^10\w$/) {
# Start, Saturday
$position_time = "5:00AM";
} elsif ($day eq '2' and $position =~ /^20\w$/) {
# Start, Sunday
2022-09-16 01:55:30 +00:00
$position_time = "4:30AM";
2023-09-15 08:02:53 +00:00
} elsif ($day eq '1' and $position =~ /^00\w$/) {
# Ask Me, Saturday
$position_time = "6:00AM";
} elsif ($day eq '2' and $position =~ /^60\w$/) {
# Ask Me, Sunday
$position_time = "5:30AM";
} elsif ($position =~ /^0[1-7]\w$/) {
# Trail, Saturday
$position_time = "6:30AM";
} elsif ($day eq '1') {
# Saturday
$position_time = "6:00AM";
2018-10-11 12:02:09 +00:00
} else {
2023-09-15 08:02:53 +00:00
# Sunday
2022-09-16 01:55:30 +00:00
$position_time = "5:30AM";
2018-10-11 12:02:09 +00:00
}
2018-08-23 08:33:18 +00:00
2018-10-11 08:28:05 +00:00
# if ($position =~ /^(26|27)$/i) {
# warn "NOTE: Skipped position $position";
# next;
# }
2018-08-23 08:33:18 +00:00
foreach my $m (split(/[, ]+/, $email)) {
2022-09-16 01:55:30 +00:00
#$m = "$name <$m>";
printf( "%-4s %-15s %-15s\n", $$person{"position"}, $club, $m ) if ($debug >= 0);
2018-08-23 08:53:02 +00:00
$m = 'Timothy Allen <tim@allen.org.za>' if ($debug == 1);
2022-09-16 01:55:30 +00:00
my $dayname = ( $day eq '1' ? 'Saturday' : 'Sunday' );
my $pdf = $pdf_dir . $club . '/' . $day . '_' . $club . '_marshal_map_' . $position . '.pdf';
my $jpg = $pdf_dir . $club . '/' . $day . '_' . $club . '_marshal_map_' . $position . '.jpg';
2018-08-23 08:53:02 +00:00
my $pdf_filename = basename($pdf);
my $jpg_filename = basename($jpg);
2023-09-15 08:02:53 +00:00
die "No PDF at $pdf_filename $pdf" unless -e $pdf;
2018-10-11 08:28:05 +00:00
die "No JPG at $jpg_filename" unless -e $jpg;
2018-08-23 08:53:02 +00:00
2018-08-23 08:33:18 +00:00
my $html = qq(
<html><head>
</head>
<body>
<p>
2022-09-16 01:55:30 +00:00
Please find attached to this email 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>$position_time</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.
2018-08-23 08:33:18 +00:00
</p>
<p>
2023-09-15 08:02:53 +00:00
If you have any trouble opening the attachment, the map is available online as a <a href="https://node.org.za/GunRun2023/$club/$pdf_filename">PDF document</a> and an <a href="https://node.org.za/GunRun2023/$club/$jpg_filename">image</a>.
2018-08-23 08:33:18 +00:00
</p>
<p>
2023-09-15 08:02:53 +00:00
In addition, there is a complete <a href="https://node.org.za/GunRun2023/Gun%20Run%202023%20-%20Routes.pdf">route map</a> and a map of <a href="https://node.org.za/GunRun2023/Gun%20Run%202023%20-%20Marshal%20plan.pdf">all marshal positions</a>. If you need to lay out cones, you may find the <a href="https://node.org.za/GunRun2023/Gun%20Run%202023%20-%20Fencing%20-%20Cones.pdf">fencing map</a> helpful.
2018-08-23 08:33:18 +00:00
</p>
<p>
You'll notice the map has several phone numbers on it&mdash;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 or myself.
2018-08-23 08:53:02 +00:00
</p>
<p>
2022-09-16 01:55:30 +00:00
If you have not yet picked up your bib and flag, Roger will be at the finish fields, accessible from Vlei Road, on Friday the 16th.
2018-08-23 08:33:18 +00:00
</p>
<p>
2022-09-16 01:55:30 +00:00
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!
2018-08-23 08:33:18 +00:00
</p>
<p>
2022-09-16 01:55:30 +00:00
Thank you for helping to make this year's Gun Run a success!
2018-08-23 08:33:18 +00:00
</p>
</body>
</html>
);
2018-08-23 08:53:02 +00:00
2022-09-16 01:55:30 +00:00
my $plain_text = qq(Please find attached to this email a map that gives your marshalling position. You are in position $position. It's important to be in place by $position_time 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.
2018-08-23 08:33:18 +00:00
2023-09-15 08:02:53 +00:00
If you have any trouble opening the attachment, the map is available online as a PDF document at <https://node.org.za/GunRun2023/$club/$pdf_filename> and as an image at <https://node.org.za/GunRun2023/$club/$jpg_filename>.
2018-08-23 08:33:18 +00:00
2023-09-15 08:02:53 +00:00
In addition, there is a complete route map at <https://node.org.za/GunRun2023/Gun%20Run%202023%20-%20Routes.pdf> and a map of all marshal positions at <https://node.org.za/GunRun2023/Gun%20Run%202023%20-%20Marshal%20plan.pdf>. If you need to lay out cones, you may find the fencing map helpful: <https://node.org.za/GunRun2023/Gun%20Run%202023%20-%20Fencing%20rev%202.1%20-%20Cones.pdf>.
2018-08-23 08:53:02 +00:00
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 or myself.
2018-08-23 08:33:18 +00:00
2022-09-16 01:55:30 +00:00
If you have not yet picked up your bib and flag, Roger will be at the finish fields, accessible from Vlei Road, on Friday the 16th.
2018-08-23 08:33:18 +00:00
2022-09-16 01:55:30 +00:00
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!
2018-08-23 08:33:18 +00:00
2022-09-16 01:55:30 +00:00
Thank you for helping to make this year's Gun Run a success!
2018-08-23 08:33:18 +00:00
);
2018-08-23 08:53:02 +00:00
$pdf_filename =~ s/^.*?_//;
2019-10-25 07:40:29 +00:00
my %objects = (
"$pdf_filename" => $pdf,
);
2018-08-23 08:53:02 +00:00
2018-08-23 08:33:18 +00:00
my $mail = Email::MIME->create_html(
header => [
From => 'Timothy Allen <tim@allen.org.za>',
2023-09-15 08:02:53 +00:00
Subject => 'Your Gun Run 2023 marshalling position',
2018-08-23 08:33:18 +00:00
To => $m,
],
body => $html,
text_body => $plain_text,
objects => \%objects,
);
my $g_transport = Email::Sender::Transport::SMTP::TLS->new(
host => 'smtp.gmail.com',
username => 'trallen@gmail.com',
2018-10-11 08:28:05 +00:00
password => $password,
2018-08-23 08:33:18 +00:00
);
2022-09-16 01:55:30 +00:00
my $m_transport = Email::Sender::Transport::SMTP::TLS->new(
host => 'in-v3.mailjet.com',
username => 'abb8a948af228c40809c109e9f1c1f59',
password => $password,
);
2018-08-23 08:33:18 +00:00
my $t_transport = Email::Sender::Transport::SMTP::TLS->new(
host => 'mail.treehouse.org.za',
username => 'tim',
2018-10-11 08:28:05 +00:00
password => $password,
2018-08-23 08:33:18 +00:00
);
try {
2022-09-16 01:55:30 +00:00
# sendmail($mail, { transport => $g_transport }) if ($debug <= 1); # Google
sendmail($mail, { transport => $m_transport }) if ($debug <= 1); # Mailjet
# sendmail($mail, { transport => $t_transport }) if ($debug <= 1); # treehouse.org.za
2018-08-23 08:33:18 +00:00
} catch {
warn "\n\nError sending email for position $position\n\n";
warn $_;
};
}
2018-10-11 08:28:05 +00:00
sleep 2 if ($debug < 1);
2018-08-23 08:53:02 +00:00
last if ($debug == 1);
2018-08-23 08:33:18 +00:00
}
2018-10-11 08:28:05 +00:00
# vim: set expandtab shiftwidth=4 softtabstop=4 :