-
Notifications
You must be signed in to change notification settings - Fork 1
/
dld-csv2xml
executable file
·150 lines (138 loc) · 3.92 KB
/
dld-csv2xml
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
use Text::CSV;
use utf8;
use open IN => ":utf8", OUT => ":utf8";
use open ':std';
my $votemap = {
'For' => 'for',
'Mot' => 'against',
'---' => 'absent'
};
my $partymap = {
'A' => 'Arbeiderpartiet',
'KrF' => 'Kristelig Folkeparti',
'H' => 'Høyre',
'FrP' => 'Fremskrittspartiet',
'SV' => 'Sosialistisk Venstreparti',
'Sp' => 'Senterpartiet',
'V' => 'Venstre',
};
my $timestamp;
my $kart;
my $sak;
my $description;
my $fylke;
my %member;
my %entries = load_member_list();
my $votefilename = shift;
open(my $fh, '<', $votefilename) || die "Unable to read from $votefilename";
my $csv = Text::CSV->new({binary => 1});
$csv->column_names($csv->getline($fh));
my %persons;
while (my $ref = $csv->getline_hr($fh)) {
my $name = $ref->{Navn};
my $perid = $entries{$name}->{perid};
my $party = $partymap->{$ref->{Parti}};
my $vote1 = $votemap->{$ref->{'Tilrådingens punkt A'}};
my $vote2 = $votemap->{$ref->{'Lovens overskrift og loven i sin helhet'}};
$persons{$name} = {
perid => $perid,
vote1 => $vote1,
vote2 => $vote2,
party => $party,
first_name => $entries{$name}->{first_name},
last_name => $entries{$name}->{last_name},
};
}
close $fh;
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
#print "<votes>\n";
print <<EOF;
<vote>
<time>2011-04-04T21:44:54</time>
<map>?</map>
<id>1</id>
<subject>Datalagringsdirektivet: Tilrådingens punkt A</subject>
<externalId>?</externalId>
<externalIssueId>48717</externalIssueId>
<counts>
<for>89</for>
<against>80</against>
<absent>0</absent>
</counts>
<method>ikke_spesifisert</method>
<resultType>ikke_spesifisert</resultType>
<representatives>
EOF
for my $name (sort keys %persons) {
my $perid = $persons{$name}->{perid};
my $vote = $persons{$name}->{vote1};
my $party = $persons{$name}->{party};
my $firstname = $persons{$name}->{first_name};
my $lastname = $persons{$name}->{last_name};
print <<EOF;
<representative>
<externalId>$perid</externalId>
<firstName>$firstname</firstName>
<lastName>$lastname</lastName>
<party>$party</party>
<voteResult>$vote</voteResult>
</representative>
EOF
}
print " </representatives>\n";
print " </vote>\n";
print <<EOF;
<vote>
<time>2011-04-04T21:45:27</time>
<map>?</map>
<id>2</id>
<subject>Datalagringsdirektivet: Lovens overskrift og loven i sin helhet</su
<externalId>?</externalId>
<externalIssueId>48717</externalIssueId>
<counts>
<for>89</for>
<against>80</against>
<absent>0</absent>
</counts>
<method>ikke_spesifisert</method>
<resultType>ikke_spesifisert</resultType>
<representatives>
EOF
for my $name (sort keys %persons) {
my $perid = $persons{$name}->{perid};
my $vote = $persons{$name}->{vote2};
my $party = $persons{$name}->{party};
my $firstname = $persons{$name}->{first_name};
my $lastname = $persons{$name}->{last_name};
print STDERR "$name missing perid\n" unless $perid;
print <<EOF;
<representative>
<externalId>$perid</externalId>
<firstName>$firstname</firstName>
<lastName>$lastname</lastName>
<party>$party</party>
<voteResult>$vote</voteResult>
</representative>
EOF
}
print " </representatives>\n";
print " </vote>\n";
#print "</votes>\n";
sub load_member_list {
my $file = 'data/stortinget-persons-scraped.xml';
my $xmlparlament = XMLin($file, KeyAttr => []);
my %entries;
for my $person (@{$xmlparlament->{person}}) {
my $name = $person->{first_name} . ' ' . $person->{last_name};
$entries{$name} = {
perid => $person->{'stortinget-perid'},
first_name => $person->{first_name},
last_name => $person->{last_name},
};
}
return %entries;
}