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

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

  ViewVC Help
Powered by ViewVC 1.1.26