forked from frezik/bodgery_signin_forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_mailing_list_signups.pl
89 lines (73 loc) · 1.88 KB
/
export_mailing_list_signups.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!perl
use v5.14;
use warnings;
use DBI;
use Net::Google::Drive::Simple;
use DateTime;
use constant DB_NAME => 'bodgery_liability';
use constant DB_USERNAME => '';
use constant DB_PASSWORD => '';
sub get_dbh
{
return $dbh if defined $dbh;
$dbh = DBI->connect(
'dbi:Pg:dbname=' . DB_NAME,
DB_USERNAME,
DB_PASSWORD,
{
AutoCommit => 1,
RaiseError => 0,
},
) or die "Could not connect to database: " . DBI->errstr;
return $dbh;
}
sub get_pending_signups
{
my ($dbh) = @_;
my $sql = 'SELECT id, email FROM guest_signin'
. ' WHERE is_mailing_list_exported = FALSE';
my $sth = $dbh->prepare_cached( $sql )
or die "Can't prepare statement: " . $dbh->errstr;
$sth->execute()
or die "Can't execute statement: " . $sth->errstr;
my (@ids, @emails);
while( my $row = $sth->fetchrow_hashref ) {
push @ids => $row->{id};
push @emails => $row->{email};
}
$sth->finish;
return (\@ids, \@emails);
}
sub make_filename
{
my $now = DateTime->now;
my $name = 'email_signups_' . $now->ymd('_');
return $name;
}
sub save_emails_to_google
{
my ($emails) = @_;
my @emails = @$emails;
my $gd = Net::Google::Drive::Simple->new;
my $filename = make_filename();
return;
}
sub mark_signups_done
{
my ($ids) = @_;
my @ids = @$ids;
my $sql = 'UPDATE guest_signin SET is_mailing_list_exported = TRUE'
. ' WHERE id IN (' . join( ',', ('?') x scalar(@ids) ) . ')';
my $sth = $dbh->prepare_cached( $sql )
or die "Can't prepare statement: " . $dbh->errstr;
$sth->execute( @ids )
or die "Can't execute statement: " . $sth->errstr;
$sth->finish;
return;
}
{
my $dbh = get_dbh;
my ($ids, $emails) = get_pending_signups( $dbh );
save_emails_to_google( $emails );
mark_signups_done( $ids );
}