/
load_cand
executable file
·46 lines (32 loc) · 939 Bytes
/
load_cand
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
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use lib 'lib';
use TwittElection::Schema;
my $file = shift || 'db/candidate.tsv';
my $sch = TwittElection::Schema->get_schema;
my $con_rs = $sch->resultset('Constituency');
my $par_rs = $sch->resultset('Party');
open my $fh, '<:encoding(UTF-8)', $file or die "$!: $file";
#delete existing candidates
$sch->resultset('Candidate')->delete;
while (<$fh>) {
chomp;
my ($name, $yournextmp_id, $twitter, $party_id, $constituency_id)
= split /\t/;
my $con = $con_rs->find({
mapit_id => $constituency_id,
});
warn "Can't find constituency: $constituency_id", next unless $con;
my $par = $par_rs->find({
yournextmp_id => $party_id,
});
warn "Can't find party: $party_id", next unless $par;
$con->add_to_candidates({
yournextmp_id => $yournextmp_id,
name => $name,
twitter => $twitter,
party_id => $par->id,
});
}