Skip to content

Commit

Permalink
Better logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Jan 20, 2015
1 parent 3497267 commit 49ebed3
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions bin/create_lists
Expand Up @@ -7,10 +7,15 @@ use lib 'lib';

use DateTime;
use Scalar::Util qw[blessed];
use Log::Log4perl qw[:easy];

use TwittElection::Twitter;
use TwittElection::Schema;

Log::Log4perl->easy_init($INFO);

my $logger = Log::Log4perl->get_logger;

my $sch = TwittElection::Schema->get_schema;

my $con_rs;
Expand Down Expand Up @@ -66,20 +71,20 @@ $t->authorise;
# Only attempt to rebuild lists that haven't been rebuilt in the last 24 hours.

foreach my $con ($con_rs->all) {
say $con->name, ' (', $con->mapit_id, ')';
$logger->info($con->name . ' (' . $con->mapit_id . ')');

unless ($con->candidates->count) {
warn "No candidates found\n";
$logger->info('No candidates found');
next;
}

if ($con->candidates_updated_time <= $con->list_rebuilt_time) {
warn "No need to rebuild list\n";
$logger->info('No need to rebuild list');
next;
}

if ($delay and $con->list_rebuilt_time > DateTime->now->subtract(days=>1)) {
warn "List rebuilt too recently\n";
$logger->info('List rebuilt too recently');
next;
}

Expand All @@ -98,27 +103,27 @@ foreach my $con ($con_rs->all) {
});
}
} else {
say 'Create ', $con->name;
$logger->info('Create ' . $con->name);
$list = $t->create_list({
owner_screen_name => 'twittelection',
name => $con->list_name,
});

say $list->{slug};
$logger->info($list->{slug});

$con->update({
list_name => $list->{slug},
list_id => $list->{id},
});
}
} else {
say 'Create ', $con->name;
$logger->info('Create ' . $con->name);
$list = $t->create_list({
owner_screen_name => 'twittelection',
name => $con->list_name,
});

say $list->{slug};
$logger->info($list->{slug});

# Remove the number that Twitter will sometimes add at the end
$list->{slug} =~ s/-\d+$//;
Expand All @@ -131,7 +136,7 @@ foreach my $con ($con_rs->all) {

foreach my $cand ($con->candidates) {
next unless $cand->twitter;
say "$list->{id} -> ", $cand->name, ' / ', $cand->twitter;
$logger->info("$list->{id} -> " . $cand->name . ' / ' . $cand->twitter);
$t->add_list_member({
list_id => $list->{id},
screen_name => $cand->twitter,
Expand All @@ -145,10 +150,11 @@ foreach my $con ($con_rs->all) {

if ($@) {
my $err = $@;
die $err unless blessed $err and $err->isa('Net::Twitter::Error');
$logger->logdie($err)
unless blessed $err and $err->isa('Net::Twitter::Error');
if ($err->code == 429 or $err->code == 403) {
warn $err->code, ': ', $err->error, "\n";
die "Rate limit exceeded\n";
$logger->logwarn($err->code . ': ' . $err->error);
$logger->logdie('Rate limit exceeded');
} else {
next;
}
Expand Down

0 comments on commit 49ebed3

Please sign in to comment.