Skip to content

Commit

Permalink
Better logging for get_cand.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Jan 20, 2015
1 parent 49ebed3 commit 137ef4f
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions bin/get_cands
Expand Up @@ -10,13 +10,22 @@ use JSON;
use LWP::Simple;
use DateTime;
use Getopt::Long;
use Log::Log4perl qw[:easy];

use lib 'lib';
use TwittElection::Schema;

my $verbose;
GetOptions(verbose => \$verbose);

if ($verbose) {
Log::Log4perl->easy_init($TRACE);
} else {
Log::Log4perl->easy_init($INFO);
}

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

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

my $cons_rs;
Expand All @@ -30,7 +39,7 @@ if (@ARGV) {

foreach my $con ($cons_rs->all) {
my $changed = 0;
say $con->name, ' (', $con->mapit_id, ')';
$logger->info($con->name . ' (' . $con->mapit_id . ')');
my $const_id = $con->mapit_id;

my $url = 'http://yournextmp.popit.mysociety.org/api/v0.1/posts/' .
Expand Down Expand Up @@ -73,10 +82,10 @@ foreach my $con ($cons_rs->all) {
if (exists $curr_cands{$cand->{id}}) {
my $curr_cand = delete $curr_cands{$cand->{id}};
if ($cand->{name} ne $curr_cand->name
or $cand->{twitter_username} ne $curr_cand->twitter) {
say "* Updating $cand->{name} / ",
$cand->{party_memberships}{2015}{name},
" / \@$cand->{twitter_username}";
or $cand->{twitter_username} ne $curr_cand->twitter) {
$logger->info("* Updating $cand->{name} / " .
$cand->{party_memberships}{2015}{name} .
" / \@$cand->{twitter_username}");
$curr_cand->update({
yournextmp_id => $cand->{id},
name => $cand->{name},
Expand All @@ -86,14 +95,14 @@ foreach my $con ($cons_rs->all) {
});
$changed = 1;
} else {
say "* No change $cand->{name} / ",
$cand->{party_memberships}{2015}{name},
" / \@$cand->{twitter_username}" if $verbose;
$logger->trace("* No change $cand->{name} / " .
$cand->{party_memberships}{2015}{name} .
" / \@$cand->{twitter_username}");
}
} else {
say "* Adding $cand->{name} / ",
$cand->{party_memberships}{2015}{name},
" / \@$cand->{twitter_username}";
$logger->info("* Adding $cand->{name} / " .
$cand->{party_memberships}{2015}{name} .
" / \@$cand->{twitter_username}");
$can_rs->create({
yournextmp_id => $cand->{id},
name => $cand->{name},
Expand All @@ -108,9 +117,9 @@ foreach my $con ($cons_rs->all) {
# Any candidates left in %curr_cands need to be deleted
foreach (keys %curr_cands) {
my $curr_cand = $curr_cands{$_};
say '* Deleting ', $curr_cand->name, ' / ',
$curr_cand->party->name,
' / ', $curr_cand->twitter;
$logger->info('* Deleting ' . $curr_cand->name . ' / ' .
$curr_cand->party->name .
' / ' . $curr_cand->twitter);
$curr_cand->delete;
$changed = 1;
}
Expand All @@ -121,4 +130,3 @@ foreach my $con ($cons_rs->all) {
});
}
}

0 comments on commit 137ef4f

Please sign in to comment.