/
get_cands
executable file
·132 lines (110 loc) · 3.35 KB
/
get_cands
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use open IO => ':encoding(utf8)';
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;
if (@ARGV) {
$cons_rs = $sch->resultset('Constituency')->search({
mapit_id => \@ARGV,
});
} else {
$cons_rs = $sch->resultset('Constituency');
}
foreach my $con ($cons_rs->all) {
my $changed = 0;
$logger->info($con->name . ' (' . $con->mapit_id . ')');
my $const_id = $con->mapit_id;
my $url = 'http://yournextmp.popit.mysociety.org/api/v0.1/posts/' .
$const_id .
'?embed=membership.person';
my $json = get($url);
my $data = decode_json $json;
my $cands = $data->{result}{memberships};
my %curr_cands;
foreach ($con->candidates) {
$curr_cands{$_->yournextmp_id} = $_;
}
my $par_rs = $sch->resultset('Party');
my $can_rs = $sch->resultset('Candidate');
my %cands;
for (@$cands) {
next unless $_->{person_id}{standing_in}{2015};
next unless $_->{person_id}{standing_in}{2015}{post_id} == $const_id;
$cands{$_->{person_id}{id}} = $_;
}
for (values %cands) {
my $cand = $_->{person_id}{versions}[0]{data};
my ($party_id) = $cand->{party_memberships}{2015}{id} =~ /(\d+)/;
my $party;
unless ($party = $par_rs->find({ yournextmp_id => $party_id })) {
$party = $par_rs->create({
name => $cand->{party_memberships}{2015}{name},
yournextmp_id => $party_id,
});
}
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) {
$logger->info("* Updating $cand->{name} / " .
$cand->{party_memberships}{2015}{name} .
" / \@$cand->{twitter_username}");
$curr_cand->update({
yournextmp_id => $cand->{id},
name => $cand->{name},
twitter => $cand->{twitter_username},
party_id => $party->id,
constituency_id => $con->id,
});
$changed = 1;
} else {
$logger->trace("* No change $cand->{name} / " .
$cand->{party_memberships}{2015}{name} .
" / \@$cand->{twitter_username}");
}
} else {
$logger->info("* Adding $cand->{name} / " .
$cand->{party_memberships}{2015}{name} .
" / \@$cand->{twitter_username}");
$can_rs->create({
yournextmp_id => $cand->{id},
name => $cand->{name},
twitter => $cand->{twitter_username},
party_id => $party->id,
constituency_id => $con->id,
});
$changed = 1;
}
}
# Any candidates left in %curr_cands need to be deleted
foreach (keys %curr_cands) {
my $curr_cand = $curr_cands{$_};
$logger->info('* Deleting ' . $curr_cand->name . ' / ' .
$curr_cand->party->name .
' / ' . $curr_cand->twitter);
$curr_cand->delete;
$changed = 1;
}
if ($changed) {
$con->update({
candidates_updated_time => DateTime->now,
});
}
}