/[Z3950-HTML-Scraper]/COBISS.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /COBISS.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (show annotations)
Sat Jun 20 19:28:04 2009 UTC (14 years, 9 months ago) by dpavlin
File size: 2875 byte(s)
split COBISS into search (from advanced form) and fetch_rec,
create rewriter from Z39.50 to COBISS syntax,
added diag for output

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 diag $mech->content;
90
91 $mech->follow_link( url_regex => qr/mode=3/ );
92
93 diag "# submit search $query";
94
95 $mech->submit_form(
96 fields => {
97 'SS1' => $query,
98 },
99 );
100
101 $hits = 0;
102 if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
103 $hits = $1;
104 } else {
105 diag "get't find results in ", $mech->content;
106 return;
107 }
108
109 diag "# got $hits results, get first one";
110
111 $mech->follow_link( url_regex => qr/ukaz=DISP/ );
112
113 diag "# in COMARC format";
114
115 $mech->follow_link( url_regex => qr/fmt=13/ );
116 }
117
118
119 sub fetch_marc {
120 my ($self) = @_;
121
122 my $comarc;
123
124 if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\s*</pre>}s ) {
125
126 my $comarc = $1;
127 my $nr = $2;
128 my $id = $3;
129
130 diag "# fetch_marc $nr [$id]";
131
132 $comarc =~ s{</?b>}{}gs;
133 $comarc =~ s{<font[^>]*>}{<s>}gs;
134 $comarc =~ s{</font>}{<e>}gs;
135
136 write_file "comarc/$id", $comarc;
137
138 print $comarc;
139
140 my $marc = MARC::Record->new;
141
142 foreach my $line ( split(/[\r\n]+/, $comarc) ) {
143 our @f;
144
145 if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
146 diag "SKIP: $line";
147 } else {
148 $line .= "<eol>";
149
150 @f = ( $1, $2, $3 );
151 sub sf { push @f, @_; }
152 $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;
153 diag "# f:", join('|', @f), " left: |$line|";
154 $marc->add_fields( @f );
155 }
156 }
157
158 open(my $out, '>:utf8', "marc/$id");
159 print $out $marc->as_usmarc;
160 close($out);
161
162 diag $marc->as_formatted;
163
164 return $marc->as_usmarc;
165 } else {
166 die "can't fetch COMARC format from ", $mech->content;
167 }
168
169 }
170
171 1;

  ViewVC Help
Powered by ViewVC 1.1.26