ignore ssl certificate errors
[Biblio-Z3950.git] / Scraper.pm
1 package Scraper;
2
3 use warnings;
4 use strict;
5
6 use IO::Socket::SSL qw();
7 use WWW::Mechanize;
8
9
10 sub new {
11     my ( $class, $database ) = @_;
12
13         $database ||= $class;
14
15     my $self = {
16                 mech => WWW::Mechanize->new(
17                         ssl_opts => {
18                             SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
19                             verify_hostname => 0, # this key is likely going to be removed in future LWP >6.04
20                         }
21                 ),
22                 database => $database,
23         };
24     bless $self, $class;
25     return $self;
26 }
27
28 sub mech { $_[0]->{mech} }
29
30 sub save_marc {
31         my ( $self, $id, $marc ) = @_;
32
33         my $database = $self->{database};
34         mkdir 'marc' unless -e 'marc';
35         mkdir "marc/$database" unless -e "marc/$database";
36
37         my $path = "marc/$database/$id";
38
39         open(my $out, '>:utf8', $path) || die "$path: $!";
40         print $out $marc;
41         close($out);
42
43         warn "# created $path ", -s $path, " bytes";
44
45 }
46
47 our $dump_nr = 1;
48
49 sub save_content {
50         my $self = shift;
51         my $path = "/tmp/$dump_nr.html";
52         open(my $html, '>', $path);
53         print $html $self->{mech}->content;
54         close($html);
55         warn "# save_content $path ", -s $path, " bytes";
56         $dump_nr++;
57 }
58
59 1;