| 1 | #!/usr/bin/env perl | ||
| 2 | # | ||
| 3 | ## no critic | ||
| 4 | # | ||
| 5 | # PODNAME: import.pl | ||
| 6 | our $VERSION = '0.5'; # VERSION: | ||
| 7 | # ABSTRACT: simple CSV importer | ||
| 8 | # | ||
| 9 | ## use critic | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use 5.010; | ||
| 13 | #use lib 'lib'; | ||
| 14 | use Mail::Chimp2; | ||
| 15 | use Text::CSV; | ||
| 16 | use Getopt::Long; | ||
| 17 | use Data::Dump qw/dump/; | ||
| 18 | |||
| 19 | my $api_key; | ||
| 20 | my $list_id; | ||
| 21 | my $sep_char = ';'; | ||
| 22 | my $file; | ||
| 23 | my $debug = 0; | ||
| 24 | |||
| 25 | GetOptions( | ||
| 26 | "debug|d" => \$debug, | ||
| 27 | "apikey|a=s" => \$api_key, | ||
| 28 | "listid|l=s" => \$list_id, | ||
| 29 | "file|f=s" => \$file, | ||
| 30 | ) or die; | ||
| 31 | |||
| 32 | die "Usage: $0 [CSV File]\n" unless $file; | ||
| 33 | |||
| 34 | my $csv = Text::CSV->new({ binary => 1, sep_char => $sep_char }) | ||
| 35 | or die "Cannot use CSV: " . Text::CSV->error_diag(); | ||
| 36 | |||
| 37 | open my $fh, "<:encoding(utf8)", $file or die "Could not open $file: $!"; | ||
| 38 | say "reading CSV: $file" if $debug; | ||
| 39 | my $batch; | ||
| 40 | # format: | ||
| 41 | # *|EMAIL|* *|FNAME|* *|LNAME|* *|DOMAINS|* | ||
| 42 | # | ||
| 43 | while (my $row = $csv->getline($fh)) { | ||
| 44 | |||
| 45 | next unless $row->[0]; | ||
| 46 | push( | ||
| 47 | @{$batch}, { | ||
| 48 | email => { email => $row->[0] }, | ||
| 49 | merge_vars => { | ||
| 50 | FNAME => $row->[1], | ||
| 51 | LNAME => $row->[2], | ||
| 52 | DOMAINS => $row->[3], | ||
| 53 | } }); | ||
| 54 | } | ||
| 55 | |||
| 56 | dump $batch if $debug; | ||
| 57 | |||
| 58 | my $chimp = Mail::Chimp2->new(api_key => $api_key); | ||
| 59 | |||
| 60 | my $ok = $chimp->lists_batch_subscribe( | ||
| 61 | id => $list_id, | ||
| 62 | double_optin => 'false', | ||
| 63 | update_existing => 'true', | ||
| 64 | batch => $batch); | ||
| 65 | dump $ok; | ||
| 66 | |||
| 67 | __END__ |