Skip to content

Commit

Permalink
Load 2024 parliamentary constituencies
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Apr 30, 2024
1 parent e7d04f6 commit da39bac
Show file tree
Hide file tree
Showing 6 changed files with 1,365 additions and 4,201 deletions.
28 changes: 17 additions & 11 deletions bin/load_constit
Expand Up @@ -5,35 +5,41 @@ use warnings;
use 5.010;
use lib 'lib';

use JSON;
use Data::Printer;

use Text::CSV;
use Text::Unidecode;

use TwittElection::Schema;

open my $js_fh, '<', 'db/constit.json' or die $!;

my $json = join '', <$js_fh>;
my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 });

my $data = decode_json($json);
my @fields = @{ $csv->getline(*ARGV) };

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

my $con_rs = $sch->resultset('Constituency');

my $m = 1;
foreach (@$data) {
while (my $row = $csv->getline(*ARGV)) {
my %data;
@data{@fields} = @$row;

p %data;

$con_rs->create({
name => $_->{name},
list_name => list_name($_->{name}),
mapit_id => $m++,
name => $data{name},
list_name => list_name($data{name}),
three_code => $data{three_code},
region => $data{region},
nation => $data{nation},
});
}

sub list_name {
my ($name) = @_;

$name = lc unidecode($name);
$name =~ s/\s+/_/g;
$name =~ s/\s+/-/g;

return $name;
}
Binary file modified data/twittelection.sqlite
Binary file not shown.
8 changes: 5 additions & 3 deletions data/twittelection_ddl.sql
Expand Up @@ -15,16 +15,18 @@ CREATE INDEX "constituency_id" ON "candidate" ("constituency_id");
CREATE UNIQUE INDEX "yournextmp_id" ON "candidate" ("yournextmp_id");
CREATE TABLE IF NOT EXISTS "constituency" (
"id" INTEGER PRIMARY KEY NOT NULL,
"mapit_id" int(11) NOT NULL DEFAULT 0,
"demclub_id" varchar(20) DEFAULT NULL,
"three_code" char(3) NOT NULL DEFAULT 0,
"name" varchar(200) NOT NULL,
"region" varchar(50) NOT NULL,
"nation" varchar(50) NOT NULL,
"list_name" varchar(25) NOT NULL,
"list_id" varchar(20) DEFAULT NULL,
"candidates_updated_time" datetime NOT NULL DEFAULT '2000-01-01 00:00:00',
"list_rebuilt_time" datetime NOT NULL DEFAULT '2000-01-01 00:00:00',
"list_checked_time" datetime DEFAULT NULL
);
CREATE UNIQUE INDEX "mapit_id" ON "constituency" ("mapit_id");
CREATE UNIQUE INDEX "con_three_code" ON "constituency" ("three_code");
CREATE UNIQUE INDEX "con_name" ON "constituency" ("name");
CREATE TABLE IF NOT EXISTS "party" (
"id" INTEGER PRIMARY KEY NOT NULL,
"yournextmp_id" int(11) NOT NULL,
Expand Down

0 comments on commit da39bac

Please sign in to comment.