-
Notifications
You must be signed in to change notification settings - Fork 1
/
08_addHistoryItems
executable file
·177 lines (133 loc) · 4.55 KB
/
08_addHistoryItems
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/perl
# This script takes apart the lines of the history files. If 11 items in the history file are found and only 10 are in the database, then
# the 11th is added.
#
# This relies on the fact that the history file is only added to.
#
require "cmds.pl";
%options = &readOptions;
$session = $options{"session"};
for ($idx = 0; $idx < scalar(@ARGV); $idx++) {
if ($ARGV[$idx] eq "-s") { $session = $ARGV[$idx+1]; }
if ($ARGV[$idx] eq "-b") { $bill_num = $ARGV[$idx+1]; }
}
%month = (
"Jan." => "01",
"Feb." => "02",
"Mar." => "03",
"Apr." => "04",
"May" => "05",
"June" => "06",
"July" => "07",
"Aug." => "08",
"Sep." => "09",
"Sept." => "09",
"Oct." => "10",
"Nov." => "11",
"Dec." => "12"
);
sub fixedAction {
local $act = $_[0];
# while ($act =~ /Com\./) { $act =~ s/Com\./Committee/; }
# $act =~ s/NAT\. RES\./Natural Resources/g;
# $act =~ s/APPR\./Appropriations/g;
# $act =~ s/L\. GOV\./Local Government/g;
# $act =~ s/G\.O\./Government Organization/g;
# $act =~ s/E\.Q\./Environmental Quality/g;
# $act =~ s/RLS\./Rules/g;
# $act =~ s/BUDGET/Budget/g;
# $act =~ s/TRANS\./Transportation/g;
# $act =~ s/E\. & R\./Elections and Redistricting/g;
# $act =~ s/HIGHER ED\./Higher Education/g;
# $act =~ s/ED\./Education/g;
return $act;
}
$pk = &doSqlResult("select max(pk) from bill_actions;");
if ($pk eq "NULL") { $pk = 0; }
$pk++;
#print "next pk: ".$pk."\n";
if ( ! defined($bill_num)) {
@bills = &doSqlResults("select pk, history_file from bills where session_yrs = '".$session."';");
} else {
@bills = &doSqlResults("select pk, history_file from bills where session_yrs = '".$session."' and bill_num = '".$bill_num."';");
if (scalar(@bills) == 1) {
($billPk, $history) = split '\t', $bills[0];
&doSqlResult("delete from bill_actions where bill_pk = ".$billPk.";");
}
}
#print "bills # ".(scalar(@bills))."\n";
@actionCounts = &doSqlResults("select a1.bill_pk, count(*) from bill_actions a1, bills b1 ".
"where a1.bill_pk = b1.pk and b1.session_yrs = '".$session."' group by a1.bill_pk;");
chomp(@actionCounts);
foreach (@actionCounts) {
($billPk, $count) = split '\t', $_;
$counts{$billPk} = $count;
}
$skipped = 0;
foreach (@bills) {
($billPk, $historyFile) = split '\t', $_;
undef @actions;
undef @dates;
#print "billPk: ".$billPk, " history: ".$historyFile."\n";
if (substr($historyFile,0,5) ne $session) { next; }
$fullFile = $billDir."/".$historyFile;
#print "history file: \"".$fullFile."\"\n";
open F, $billDir."/".$historyFile;
@lines = <F>;
chomp(@lines);
close F;
#print "lines # ".(scalar(@lines))."\n";
undef $inHistory;
foreach (@lines) {
$line = $_;
if ($line eq "") { undef $inHistory; }
if (defined($inHistory)) {
if ($line =~ /^20[0-9]{2}$/) { $year = $line; }
else {
if ($line =~ /^\t/) {
$line =~ s/^\t//;
$last = pop @actions;
push @actions, $last." ".$line;
} else {
($date, $action) = split '\t', $line;
($month, $dom) = split ' ', $date;
if (length($dom) == 1) { $dom = "0".$dom; }
push @dates, $year."-".$month{$month}."-".$dom;
push @actions, &fixedAction($action);
}
}
}
if ($line eq "BILL HISTORY") { $inHistory = 1; }
}
#print "actions # ".(scalar(@actions))."\n";
#print "dates # ".(scalar(@dates))."\n";
#print "\n";
if (defined($counts{$billPk})) {
$already = $counts{$billPk};
} else {
$already = 0;
}
@actions = reverse @actions;
@dates = reverse @dates;
$skipped = $skipped + $already;
for ($idx = $already; $idx < scalar(@actions); $idx++) {
# print "DATE: ".$dates[$idx]."\n";
# print "ACTION: (".(length($actions[$idx])).") ".$actions[$idx]."\n\n";
$actions[$idx] =~ s/'/''/g;
push @sql, "insert into bill_actions (pk, bill_pk, action_num, when_act, action) ".
"values (".$pk.", ".$billPk.", ".$idx.", '".$dates[$idx]."', '".$actions[$idx]."');\n";
$pk++;
}
}
print "\nInserting actions # ".(scalar(@sql)).", skipping # ".$skipped."\n\n";
open M, "| ".&mysql;
$num = 1;
foreach (@sql) {
#print $_;
print M $_;
if ($num % 100 == 0) { close M; open M, "| ".&mysql; }
$num++;
}
close M;
print "Done.\n\n";
exit(0);