Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Last active December 16, 2015 08:19
Show Gist options
  • Save sharifulin/5405333 to your computer and use it in GitHub Desktop.
Save sharifulin/5405333 to your computer and use it in GitHub Desktop.
Working with Facebook Ads stats (iOS, Android campaigns). I like Perl! :-)
#!/usr/bin/env perl
use v5.12;
use Encode;
use Text::CSV;
use Data::Dumper;
my $dir = '/Users/sharifulin/Downloads/refb814';
while (<$dir/*.csv>) {
say "File: $_";
# get
my $temp;
{
my $csv = Text::CSV->new({ binary => 1 }) or die 'Cannot use CSV: ' . Text::CSV->error_diag;
open my $fh, '<:utf8', $_ or die "Can't open file $_: $!";
my($i, $fields);
while (my $row = $csv->getline($fh)) {
unless ($i++) {
$row->[0] = 'Date'; # XXX
$fields = $row;
}
else {
my $item;
@$item{ @$fields } = @$row;
push @$temp, $item;
}
}
$csv->eof or $csv->error_diag;
}
# die Dumper $temp;
# map
my $data;
for my $t (@$temp) {
(my $d = $t->{Date}) =~ s{(\d+)/(\d+).*}{sprintf "%02d.%02d", $2, $1}e;
$data->{ $d }->{ $_ } += $t->{ $_ } for qw(Clicks Actions);
}
# output
for (sort keys %$data) {
my $item = $data->{$_};
say join "\t\t", $_, @$item{qw(Clicks Actions)};
}
say "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment