-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.pl
executable file
·135 lines (121 loc) · 2.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
#!/usr/bin/env perl
# Copyright 2015 Michal Špaček <tupinek@gmail.com>
# Pragmas.
use strict;
use warnings;
# Modules.
use Database::DumpTruck;
use Digest::MD5;
use Encode qw(decode_utf8);
use English;
use File::Spec::Functions qw(catfile);
use File::Temp qw(tempfile);
use LWP::UserAgent;
use Net::FTP;
use URI;
# Don't buffer.
$OUTPUT_AUTOFLUSH = 1;
# Timeout.
my $TIMEOUT = 1;
# URI of service.
my $base_uri = URI->new('ftp://medical.nema.org/medical/dicom/2015b/source/docbook/');
# Open a database handle.
my $dt = Database::DumpTruck->new({
'dbname' => 'data.sqlite',
'table' => 'data',
});
# Connect to FTP.
my $host = $base_uri->host;
my $ftp = Net::FTP->new($host);
if (! $ftp) {
die "Cannot open '$host' ftp connection.";
}
# Login.
if (! $ftp->login('anonymous', 'anonymous@')) {
die 'Cannot login.';
}
# Get files.
$ftp->cwd($base_uri->path);
process_files($base_uri->path);
# List.
sub list {
my @list = $ftp->dir;
my @ret;
foreach my $list_item (@list) {
my ($date, $time, $type, $file) = split qr{\s+}, $list_item, 4;
push @ret, [$type, $file];
}
return @ret;
}
# Get link and compute MD5 sum.
sub md5 {
my $link = shift;
my (undef, $temp_file) = tempfile();
my $ua = LWP::UserAgent->new(
'agent' => 'Mozilla/5.0',
);
my $get = $ua->get($link, ':content_file' => $temp_file);
my $md5_sum;
if ($get->is_success) {
my $md5 = Digest::MD5->new;
open my $temp_fh, '<', $temp_file;
$md5->addfile($temp_fh);
$md5_sum = $md5->hexdigest;
close $temp_fh;
unlink $temp_file;
}
return $md5_sum;
}
# Process files from FTP.
sub process_files {
my $path = shift;
my @files;
foreach my $file_or_dir_ar (list()) {
if ($file_or_dir_ar->[0] ne '<DIR>') {
my $file = catfile($path, $file_or_dir_ar->[1]);
save_file($file);
} else {
my $pwd = $ftp->pwd;
$ftp->cwd($file_or_dir_ar->[1]);
process_files(catfile($path, $file_or_dir_ar->[1]));
$ftp->cwd($pwd);
}
}
return @files;
}
# Save file.
sub save_file {
my $file = shift;
my $part;
if ($file =~ m/part(\d+)/ms) {
$part = int($1);
}
my $link = $base_uri->scheme.'://'.$base_uri->host.$file;
my $ret_ar = eval {
$dt->execute('SELECT COUNT(*) FROM data '.
'WHERE Link = ?', $link);
};
if ($EVAL_ERROR || ! @{$ret_ar}
|| ! exists $ret_ar->[0]->{'count(*)'}
|| ! defined $ret_ar->[0]->{'count(*)'}
|| $ret_ar->[0]->{'count(*)'} == 0) {
my $md5 = md5($link);
if (! defined $md5) {
print "Cannot get document for $link.\n";
} else {
if (defined $part) {
print "Part $part: ";
}
print "$link\n";
$dt->insert({
'Part' => $part,
'Link' => $link,
'MD5' => $md5,
});
# TODO Move to begin with create_table().
$dt->create_index(['MD5'], 'data', 1, 0);
}
sleep $TIMEOUT;
}
return;
}