Add daily digest script
authorChip Black <bytex64@bytex64.net>
Mon, 2 Mar 2015 08:29:06 +0000 (02:29 -0600)
committerChip Black <bytex64@bytex64.net>
Mon, 2 Mar 2015 08:29:06 +0000 (02:29 -0600)
Makefile
aux/bin/daily-digest [new file with mode: 0755]

index caf187f..947c935 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -102,7 +102,7 @@ install-environment: all docs
                $(ENV_DIR)/www/doc/perl \
                $(ENV_DIR)/www/images
        install --mode 755 blerg.cgi aux/cgi/*.cgi $(ENV_DIR)/lib/cgi-bin/
-       install --mode 755 blergtool blerglatest $(ENV_DIR)/bin/
+       install --mode 755 blergtool blerglatest aux/bin/* $(ENV_DIR)/bin/
        install --mode 644 www/favicon.gif www/favicon.ico www/index.html \
                www/welcome.html \
                $(ENV_DIR)/www/
diff --git a/aux/bin/daily-digest b/aux/bin/daily-digest
new file mode 100755 (executable)
index 0000000..18ae52b
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+use Blerg::Database;
+use Mail::Message;
+use POSIX qw/strftime/;
+use strict;
+use v5.10;
+
+my $t1 = time;
+my $t0 = $t1 - 86400;
+
+sub send_email {
+    my ($username) = @_;
+
+    if (!Blerg::Database::exists($username)) {
+        return;
+    }
+
+    my $email_conf_path = Blerg::Database::configuration->{data_path} . "/$username/email";
+    if (!-f $email_conf_path) {
+        return;
+    }
+
+    open CONF, $email_conf_path;
+    my $email = <CONF>;
+    close CONF;
+
+    my $blerg = Blerg::Database->open_existing($username);
+    my @items = $blerg->subscription_list;
+    $blerg->close;
+
+    if (!@items) {
+        return;
+    }
+
+    my $feeditems;
+    for my $i (@items) {
+        use Data::Dumper;
+        $blerg = Blerg::Database->open_existing($i->{author})
+            or next;
+
+        my $ts = $blerg->timestamp($i->{record});
+        if ($ts >= $t0 && $ts < $t1) {
+            my $data = $blerg->fetch($i->{record});
+            next if !defined $data;
+            my $date = strftime('%T %b %-d %Y (%Z)', localtime($ts));
+            $feeditems .= "== $i->{author} at $date\n\n$data\n\n\n";
+        }
+
+        $blerg->close;
+    }
+
+    if (!defined $feeditems) {
+        return;
+    }
+
+    Mail::Message->build(
+        From => Mail::Address->new('BlergBot', 'noreply@blerg.cc'),
+        To => Mail::Address->new($username, $email),
+        Subject => 'Blërg Daily Digest',
+        Mail::Message::Field->new('Content-Type', 'text/plain', 'charset="utf8"'),
+        data => <<EMAIL
+Hello, $username!  Here is your daily digest:
+
+$feeditems
+- Blërg
+EMAIL
+    )->send;
+}
+
+send_email('bytex64');