-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.pl
193 lines (150 loc) · 4.71 KB
/
scraper.pl
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/perl
# Public Domain: Can be used, modified and distributed without any restriction
# Copyright 2011, 2014, 2015 Lubomir Rintel <[email protected]>
use strict;
use warnings;
use LWP::Simple;
use HTML::Parser;
use URI;
use File::Basename qw/basename/;
use XML::Simple;
use Database::DumpTruck;
binmode STDOUT, ':utf8';
my $dt = new Database::DumpTruck ({ dbname => 'data.sqlite', table => 'data' });
# Uniquely identifies a category
my @id = ('Department Code', 'Department', 'Category', 'Sub-category', 'Sub-category 2', 'Sub-category 3');
# Uniquely identifies an database entry (usable in an unique index)
my @keys = ('Year', 'Month', 'Month_Year', @id);
# Return only sanitized content, no styles
sub sane_row
{
my $row = shift;
my @cells = map { $_->{Data}{content} }
(ref $row->{Cell} eq 'ARRAY' ? @{$row->{Cell}} : ($_->{Cell}));
# Chuck trailing undefs
pop @cells unless defined $cells[$#cells];
return \@cells;
}
# Return data from a single worksheet
sub grok_sheet
{
my $sheet = shift;
my $dept_code = $sheet->{'ss:Name'};
return {} if $dept_code eq 'SR'; # Aggregation
my @rows = map { sane_row $_ } @{$sheet->{Table}{Row}};
# Assertion check
shift (@rows)->[0] =~ /Zdroj Ministerstvo/
or die 'The front fell off';
# Sanitize date span
shift (@rows)->[0] =~ /ZA OBDOBIE\s+(\d+)\.(\d+)\.\s+-\s+(\d+)\.(\d+)\.(\d+)/
or die 'A wave hit the ship';
my ($month, $year) = (sprintf ('%02d', $4), $5);
# Department
shift (@rows)->[0] =~ /^(.daje za .*)/
or die 'One in million';
my $dept_human = $1;
# Assertion check, nothing useful in this header
join ('', map { $_ || '' } @{shift (@rows)}) =~ /daje o trestn.*daje o st/
or die 'Nothing else there';
# Header
my (undef, @headings) = @{shift (@rows)};
splice @headings, 2, 1; # meaningless %
# Read the actual body
my %result;
my @cat_path = (undef, undef, undef, undef);
while (my $row = shift @rows) {
my ($category, @data) = @$row;
next unless defined $category;
next if $category =~ /SPOLU$/;
next if $category =~ /^CELKOV/;
splice @data, 2, 1; # meaningless %
# Clean up trailing whitespace
$category =~ s/\s*$//;
# Toplevel aggregation/category
if ($category =~ /^[[:upper:]\s+]+$/) {
# Sanitize the name
$category =~ s/\s+/ /g;
$category =~ s/(.)(.*)/$1\L$2/;
@cat_path[0..$#cat_path] = ($category, '', '', '');
} elsif ($category !~ /^\s+-/) {
@cat_path[1..$#cat_path] = ($category, '', '');
} elsif ($category =~ /^\s{2,4}-\s*(.+)/) {
@cat_path[2..$#cat_path] = ($1, '');
} elsif ($category =~ /^\s{5,8}-\s*(.+)/) {
@cat_path[3..$#cat_path] = ($1);
} else {
die "Could not grok category: '$category'"
}
# Fix data
@data = map { $_ || 0 } @data;
my %row;
@row{@keys, @headings} = ($year, $month, $year.$month, $dept_code, $dept_human, @cat_path, @data);
my $key = join "|", map { $row{$_} } @id;
$result{$key} = \%row;
}
return \%result;
}
# Format document
sub grok_xml
{
# Absolutize
my $uri = shift;
my %month;
use URI::Escape;
my $base = uri_unescape (basename($uri));
return unless ($base =~ /_1_/ or $base =~ /^1/);
my @rows;
my $xml = XMLin (get($uri));
%month = (%month, %{grok_sheet ($_)}) foreach @{$xml->{Worksheet}};
return \%month;
}
sub month_sub
{
my $a = shift;
my $b = shift;
return $a unless $b;
foreach my $id (keys %$a) {
foreach my $key (keys %{$a->{$id}}) {
next if grep { $_ eq $key } @keys;
$a->{$id}{$key} -= $b->{$id}{$key};
}
}
return $a;
}
sub process_year
{
my $uri = shift;
my $previous_month;
my $parser = new HTML::Parser (api_version => 3);
$parser->handler (start => sub {
my $tag = shift;
my $attr = shift;
return unless $tag eq 'a';
return unless exists $attr->{href};
return unless $attr->{href} =~ m{/swift_data/source/policia/statistiky.*.xml};
my $this_month = grok_xml (new URI ($attr->{href})->abs ($uri)) or return;
$this_month = month_sub ($this_month, $previous_month);
$previous_month = $this_month;
$dt->upsert ([values %$this_month]);
}, 'tag, attr');
$parser->parse (get $uri or die "I get it, but I don't get it. -- Alice Cooper");
$parser->eof;
}
my $root = 'http://www.minv.sk/?statistika-kriminality-v-slovenskej-republike-xml';
# Roll!
my $parser = new HTML::Parser (api_version => 3);
$parser->unbroken_text (1);
$parser->handler (start => sub {
my $tag = shift;
my $attr = shift;
return unless $tag eq 'a';
return unless exists $attr->{href};
return unless $attr->{href} =~ m{/.*za.rok.(\d\d\d\d)$}
or $attr->{href} =~ m{/.*kriminalita.(\d\d\d\d).xml$};
return unless $1 >= 2014;
process_year (new URI ($attr->{href})->abs ($root));
}, 'tag, attr');
$parser->parse (get $root or die);
$parser->eof;
# Ensure there's indices when we know there's a scheme
$dt->create_index (\@keys, undef, 1, 1);