cleanup output
[Biblio-Z3950.git] / COBISS.pm
1 package COBISS;
2
3 use warnings;
4 use strict;
5
6 use WWW::Mechanize;
7 use MARC::Record;
8 use File::Slurp;
9
10 binmode STDOUT, ':utf8';
11
12 my $cobiss_marc21 = {
13         '010' => { a => [ '020', 'a' ] },
14          200  => {
15                         a => [  245 , 'a' ],
16                         f => [  245 , 'f' ],
17         },
18          205  => { a => [  250 , 'a' ] },
19          210  => {
20                 a => [  250 , 'a' ],
21                 c => [  260 , 'b' ],
22                 d => [  260 , 'c' ],
23         },
24         215 => {
25                 a => [  300 , 'a' ],
26                 c => [  300 , 'b' ],
27                 d => [  300 , 'c' ],
28         },
29         700 => {
30                 a => [  100 , 'a' ],
31         },
32 };
33
34 our $mech = WWW::Mechanize->new();
35 our $hits;
36
37 sub diag {
38         print "# ", @_, $/;
39 }
40
41 # Koha Z39.50 query:
42 #
43 # Bib-1 @and @and @and @and @and @and @and @or
44 # @attr 1=8 isbn-issn 
45 # @attr 1=7 isbn-issn 
46 # @attr 1=4 title 
47 # @attr 1=1003 author 
48 # @attr 1=16 dewey 
49 # @attr 1=21 subject-holding 
50 # @attr 1=12 control-no 
51 # @attr 1=1007 standard-id 
52 # @attr 1=1016 any
53
54 our $usemap = {
55         8               => 'BN',        # FIXME check
56         7               => 'SN',        # FIXME check
57         4               => 'TI',
58         1003    => 'TI',
59         16              => 'CU',
60         21              => 'SU',
61 #       12              => '',
62 #       1007    => '',
63 #       1016    => '',
64
65 };
66
67 sub usemap {
68         my $f = shift || die;
69         $usemap->{$f};
70 }
71
72 sub search {
73         my ( $self, $query ) = @_;
74
75         die "need query" unless defined $query;
76
77         my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
78
79 diag "get $url";
80
81         $mech->get( $url );
82
83 diag "got session";
84
85         $mech->follow_link( text_regex => qr/union/ );
86
87 diag "switch to advanced form (select)";
88
89         $mech->follow_link( url_regex => qr/mode=3/ );
90
91 diag "submit search $query";
92
93         $mech->submit_form(
94                 fields => {
95                         'SS1' => $query,
96                 },
97         );
98
99         $hits = 0;
100         if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
101                 $hits = $1;
102         } else {
103                 diag "get't find results in ", $mech->content;
104                 return;
105         }
106
107 diag "got $hits results, get first one";
108
109         $mech->follow_link( url_regex => qr/ukaz=DISP/ );
110
111 diag "in COMARC format";
112
113         $mech->follow_link( url_regex => qr/fmt=13/ );
114 }
115
116
117 sub fetch_marc {
118         my ($self) = @_;
119
120         my $comarc;
121
122         if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\s*</pre>}s ) {
123
124                 my $comarc = $1;
125                 my $nr = $2;
126                 my $id = $3;
127
128 diag "fetch_marc $nr [$id]";
129
130                 $comarc =~ s{</?b>}{}gs;
131                 $comarc =~ s{<font[^>]*>}{<s>}gs;
132                 $comarc =~ s{</font>}{<e>}gs;
133
134                 write_file "comarc/$id", $comarc;
135
136                 print $comarc;
137
138                 my $marc = MARC::Record->new;
139
140                 foreach my $line ( split(/[\r\n]+/, $comarc) ) {
141
142                         if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
143                                 diag "SKIP: $line";
144                         } else {
145                                 $line .= "<eol>";
146
147                                 our @f = ( $1, $2, $3 );
148                                 sub sf { push @f, @_; }
149                                 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;
150                                 diag "f:", join('|', @f), " left: |$line|";
151                                 $marc->add_fields( @f );
152                         }
153                 }
154
155                 open(my $out, '>:utf8', "marc/$id");
156                 print $out $marc->as_usmarc;
157                 close($out);
158
159                 diag $marc->as_formatted;
160
161                 return $marc->as_usmarc;
162         } else {
163                 die "can't fetch COMARC format from ", $mech->content;
164         }
165
166 }
167
168 1;