die on unsupported format
[Biblio-Z3950.git] / Scraper.pm
1 package Scraper;
2
3 use warnings;
4 use strict;
5
6 use WWW::Mechanize;
7
8 sub new {
9     my ( $class, $database ) = @_;
10
11         $database ||= $class;
12
13     my $self = {
14                 mech => WWW::Mechanize->new(),
15                 database => $database,
16         };
17     bless $self, $class;
18     return $self;
19 }
20
21 sub save_marc {
22         my ( $self, $id, $marc ) = @_;
23
24         my $database = $self->{database};
25         mkdir 'marc' unless -e 'marc';
26         mkdir "marc/$database" unless -e "marc/$database";
27
28         my $path = "marc/$database/$id";
29
30         open(my $out, '>:utf8', $path) || die "$path: $!";
31         print $out $marc;
32         close($out);
33
34         warn "# created $path ", -s $path, " bytes";
35
36 }
37
38 our $dump_nr = 1;
39
40 sub save_content {
41         my $self = shift;
42         my $path = "/tmp/$dump_nr.html";
43         open(my $html, '>', $path);
44         print $html $self->{mech}->content;
45         close($html);
46         warn "# save_content $path ", -s $path, " bytes";
47 }
48
49 1;