/[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

Diff of /COBISS.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by dpavlin, Fri Jun 19 21:51:28 2009 UTC revision 14 by dpavlin, Fri Oct 22 21:31:08 2010 UTC
# Line 5  use strict; Line 5  use strict;
5    
6  use WWW::Mechanize;  use WWW::Mechanize;
7  use MARC::Record;  use MARC::Record;
8    use Data::Dump qw/dump/;
9    
10  binmode STDOUT, ':utf8';  binmode STDOUT, ':utf8';
11    
# Line 16  my $cobiss_marc21 = { Line 17  my $cobiss_marc21 = {
17          },          },
18           205  => { a => [  250 , 'a' ] },           205  => { a => [  250 , 'a' ] },
19           210  => {           210  => {
20                  a => [  250 , 'a' ],                  a => [  260 , 'a' ],
21                  c => [  260 , 'b' ],                  c => [  260 , 'b' ],
22                  d => [  260 , 'c' ],                  d => [  260 , 'c' ],
23          },          },
# Line 30  my $cobiss_marc21 = { Line 31  my $cobiss_marc21 = {
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 search {  sub search {
68            my ( $self, $query ) = @_;
69    
70            die "need query" unless defined $query;
71    
72          my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';          my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
73    
74  warn "# get $url\n";  diag "get $url";
75    
         my $mech = WWW::Mechanize->new();  
76          $mech->get( $url );          $mech->get( $url );
77    
78  warn "# got session\n";  diag "got session";
79    
80          $mech->follow_link( text_regex => qr/union/ );          $mech->follow_link( text_regex => qr/union/ );
81    
82  warn "# submit search\n";  diag "switch to advanced form (select)";
83    
84            $mech->follow_link( url_regex => qr/mode=3/ );
85    
86    diag "submit search $query";
87    
88          $mech->submit_form(          $mech->submit_form(
89                  fields => {                  fields => {
90                          'SS1' => 'Krleza',                          'SS1' => $query,
91                  },                  },
92          );          );
93    
94          my $hits = 1;          $hits = 0;
95          if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {          if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
96                  $hits = $1;                  $hits = $1;
97          } else {          } else {
98                  warn "get't find results in ", $mech->content;                  diag "get't find results in ", $mech->content;
99                    return;
100          }          }
101    
102  warn "# got $hits results, get first one\n";  diag "got $hits results, get first one";
103    
104          $mech->follow_link( url_regex => qr/ukaz=DISP/ );          $mech->follow_link( url_regex => qr/ukaz=DISP/ );
105    
106  warn "# in COMARC format\n";  diag "in COMARC format";
107    
108          $mech->follow_link( url_regex => qr/fmt=13/ );          $mech->follow_link( url_regex => qr/fmt=13/ );
109    }
110    
111    
112    sub fetch_rec {
113            my ($self,$format) = @_;
114    
115            $format ||= 'unimarc';
116    
117            die "unknown format: $format" unless $format =~ m{(uni|us)marc};
118    
119          my $comarc;          my $comarc;
120    
121          if ( $mech->content =~ m{<pre>\s*(.+1\..+?)\s*</pre>}s ) {          if ( $mech->content =~ m{<pre>\s*(.+?(\d+)\.\s+ID=(\d+).+?)\s*</pre>}s ) {
122    
123                  my $comarc = $1;                  my $comarc = $1;
124                    my $nr = $2;
125                    my $id = $3;
126    
127    diag "fetch_marc $nr [$id] $format";
128    
129                  $comarc =~ s{</?b>}{}gs;                  $comarc =~ s{</?b>}{}gs;
130                  $comarc =~ s{<font[^>]*>}{<s>}gs;                  $comarc =~ s{<font[^>]*>}{<s>}gs;
131                  $comarc =~ s{</font>}{<e>}gs;                  $comarc =~ s{</font>}{<e>}gs;
132    
133                    open(my $out, '>:utf8', "comarc/$id");
134                    print $out $comarc;
135                    close($out);
136    
137                  print $comarc;                  print $comarc;
138    
139                  my $marc = MARC::Record->new;                  my $marc = MARC::Record->new;
140    
141                  foreach my $line ( split(/[\r\n]+/, $comarc) ) {                  foreach my $line ( split(/[\r\n]+/, $comarc) ) {
                         our @f;  
142    
143                          if ( $line !~ s{(\d\d\d)([01 ])([01 ])}{} ) {                          if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
144                                  warn "SKIP: $line\n";                                  diag "SKIP: $line";
145                          } else {                          } else {
146                                    our @f = ( $1, $2, $3 );
147                                  $line .= "<eol>";                                  $line .= "<eol>";
148    
149                                  @f = ( $1, $2, $3 );                                  if ( $format eq 'unimarc' ) {
150                                  sub sf { warn "sf",@_,"|",@f; push @f, @_; }  
151                                  $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;                                          diag dump(@f), "line: $line";
152                                  warn "# f:", join(' ', @f), " left:|$line|\n";                                          sub sf_uni {
153                                  $marc->add_fields( @f );                                                  warn "sf ",dump(@_);
154                                                    push @f, @_;
155                                            }
156                                            $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_uni($1, $2)}ges;
157                                            diag "f:", dump(@f), " left: |$line|";
158                                            $marc->add_fields( @f );
159    
160                                    } elsif ( $format eq 'usmarc' ) {
161    
162                                            my ( $f, $i1, $i2 ) = @f;
163    
164                                            our $out = {};
165    
166                                            sub sf_us {
167                                                    my ($f,$sf,$v) = @_;
168                                                    if ( my $m = $cobiss_marc21->{$f}->{$sf} ) {
169                                                            push @{ $out->{ $m->[0] } }, ( $m->[1], $v );
170                                                    }
171                                                    return;
172                                            }
173                                            $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf_us($f,$1, $2)}ges;
174    
175                                            diag "converted marc21 ",dump( $out );
176    
177                                            foreach my $f ( keys %$out ) {
178                                                    $marc->add_fields( $f, $i1, $i2, @{ $out->{$f} } );
179                                            }
180                                    }
181                          }                          }
182                  }                  }
183    
184                  open(my $out, '>:utf8', 'out.marc');                  my $path = "marc/$id.$format";
185    
186                    open($out, '>:utf8', $path);
187                  print $out $marc->as_usmarc;                  print $out $marc->as_usmarc;
188                  close($out);                  close($out);
189    
190                  warn $marc->as_formatted;                  diag "created $path ", -s $path, " bytes";
191    
192                    diag $marc->as_formatted;
193    
194                    $nr++;
195                    $mech->follow_link( url_regex => qr/rec=$nr/ );
196    
197                  return $comarc;                  return $marc->as_usmarc;
198          } else {          } else {
199                  die "can't fetch COMARC format from ", $mech->content;                  die "can't fetch COMARC format from ", $mech->content;
200          }          }

Legend:
Removed from v.2  
changed lines
  Added in v.14

  ViewVC Help
Powered by ViewVC 1.1.26