-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.pl
executable file
·199 lines (175 loc) · 3.96 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
194
195
196
197
198
199
#!/usr/bin/env perl
# Copyright 2014 Michal Špaček <[email protected]>
package Dicom::Handler;
# Pragmas.
use strict;
use warnings;
# Modules.
use English;
# Constructor.
sub new {
my ($type, %params) = @_;
return bless {
%params,
'table_flag' => 0,
'td_index' => -1,
'td' => ['', '', '', '', '', ''],
'td_ok' => 0,
'tr_index' => 0,
}, $type;
}
# Start element.
sub start_element {
my ($self, $element) = @_;
if (exists $element->{'Attributes'}
&& exists $element->{'Attributes'}->{'{}label'}
&& $element->{'Attributes'}->{'{}label'}->{'Value'} eq '6-1') {
$self->{'table_flag'} = 1;
}
if (! $self->{'table_flag'}) {
return;
}
# Right td.
if (! $self->{'td_ok'}) {
return;
}
if ($element->{'Name'} eq 'td') {
$self->{'td_index'}++;
}
# XREF.
if ($element->{'Name'} eq 'xref') {
my $note = $element->{'Attributes'}->{'{}linkend'}->{'Value'};
$note =~ s/^note_//ms;
$note =~ s/_/\./gms;
$self->{'td'}->[$self->{'td_index'}] .= $note;
}
return;
}
# End element.
sub end_element {
my ($self, $element) = @_;
if (! $self->{'table_flag'}) {
return;
}
if ($element->{'Name'} eq 'table') {
$self->{'table_flag'} = 0;
}
# Right tr element.
if ($element->{'Name'} eq 'tr') {
if ($self->{'tr_index'} == 0) {
$self->{'tr_index'} = 1;
$self->{'td_ok'} = 1;
return;
}
my ($tag, $name, $keyword, $vr, $vm, $note)
= @{$self->{'td'}};
my ($group_number, $element_number) = $tag
=~ m/^\(([\d\w]+),([\d\w]+)\)$/ms;
$name =~ s/\x{200b}//gms;
$keyword =~ s/\x{200b}//gms;
my $retired = 0;
if ($note =~ m/RET/ms) {
$retired = 1;
$note =~ s/RET//ms;
$note =~ s/\s{2}/\ /gms;
$note =~ s/^\s*-\s*//ms;
}
my $ret_ar = eval {
$self->{'dt'}->execute('SELECT COUNT(*) FROM data '.
'WHERE Group_number = ? AND Element_number = ?',
$group_number, $element_number);
};
if ($EVAL_ERROR || ! @{$ret_ar}
|| ! exists $ret_ar->[0]->{'count(*)'}
|| ! defined $ret_ar->[0]->{'count(*)'}
|| $ret_ar->[0]->{'count(*)'} == 0) {
print "($group_number,$element_number): $keyword\n";
$self->{'dt'}->insert({
'Group_number' => $group_number,
'Element_number' => $element_number,
'Name' => $name,
'Keyword' => $keyword,
'VR' => $vr,
'VM' => $vm,
'Retired' => $retired,
'Note' => $note,
});
}
$self->{'dt'}->create_index(['Group_number', 'Element_number'],
'data', 1, 1);
$self->{'td'} = ['', '', '', '', '', ''];
$self->{'td_index'} = -1;
}
return;
}
# Characters.
sub characters {
my ($self, $characters) = @_;
if (! $self->{'table_flag'}) {
return;
}
# Skip blank data.
if ($characters->{'Data'} =~ m/^\s*$/ms) {
return;
}
# Right td.
if (! $self->{'td_ok'}) {
return;
}
$self->{'td'}->[$self->{'td_index'}] .= $characters->{'Data'};
return;
}
package main;
# Pragmas.
use strict;
use warnings;
# Modules.
use Database::DumpTruck;
use Encode qw(decode_utf8 encode_utf8);
use English;
use File::Temp qw(tempfile);
use LWP::UserAgent;
use URI;
use XML::SAX::Expat;
# Don't buffer.
$OUTPUT_AUTOFLUSH = 1;
# URI of service.
my $base_uri = URI->new('ftp://medical.nema.org/medical/dicom/2014a/source/docbook/part06/part06.xml');
# Open a database handle.
my $dt = Database::DumpTruck->new({
'dbname' => 'data.sqlite',
'table' => 'data',
});
# Create a user agent object.
my $ua = LWP::UserAgent->new(
'agent' => 'Mozilla/5.0',
);
# Get base root.
print 'Page: '.$base_uri->as_string."\n";
my $xml_file = get_file($base_uri);
my $h = Dicom::Handler->new(
'dt' => $dt,
);
my $p = XML::SAX::Expat->new('Handler' => $h);
$p->parse_file($xml_file);
unlink $xml_file;
# Get file
sub get_file {
my $uri = shift;
my (undef, $tempfile) = tempfile();
my $get = $ua->get($uri->as_string,
':content_file' => $tempfile,
);
if ($get->is_success) {
return $tempfile;
} else {
die "Cannot GET '".$uri->as_string." page.";
}
}
# Removing trailing whitespace.
sub remove_trailing {
my $string_sr = shift;
${$string_sr} =~ s/^\s*//ms;
${$string_sr} =~ s/\s*$//ms;
return;
}