/[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 8 by dpavlin, Sat Jun 20 22:09:33 2009 UTC revision 9 by dpavlin, Sun Jun 21 08:16:41 2009 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 17  my $cobiss_marc21 = { Line 18  my $cobiss_marc21 = {
18           205  => { a => [  250 , 'a' ] },           205  => { a => [  250 , 'a' ] },
19           210  => {           210  => {
20                  a => [  250 , 'a' ],                  a => [  250 , 'a' ],
21                  c => [  260 , 'b' ],                  c => [  250 , 'b' ],
22                  d => [  260 , 'c' ],                  d => [  250 , 'c' ],
23          },          },
24          215 => {          215 => {
25                  a => [  300 , 'a' ],                  a => [  300 , 'a' ],
# Line 113  diag "in COMARC format"; Line 114  diag "in COMARC format";
114  }  }
115    
116    
117  sub fetch_marc {  sub fetch_rec {
118          my ($self) = @_;          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    
# Line 124  sub fetch_marc { Line 129  sub fetch_marc {
129                  my $nr = $2;                  my $nr = $2;
130                  my $id = $3;                  my $id = $3;
131    
132  diag "fetch_marc $nr [$id]";  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;
# Line 143  diag "fetch_marc $nr [$id]"; Line 148  diag "fetch_marc $nr [$id]";
148                          if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {                          if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
149                                  diag "SKIP: $line";                                  diag "SKIP: $line";
150                          } else {                          } else {
151                                    our @f = ( $1, $2, $3 );
152                                  $line .= "<eol>";                                  $line .= "<eol>";
153    
154                                  our @f = ( $1, $2, $3 );                                  if ( $format eq 'unimarc' ) {
155                                  sub sf { push @f, @_; }  
156                                  $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;                                          diag dump(@f), "line: $line";
157                                  diag "f:", join('|', @f), " left: |$line|";                                          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', "marc/$id");                  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                    diag "created $path ", -s $path, " bytes";
196    
197                  diag $marc->as_formatted;                  diag $marc->as_formatted;
198    
199                  $nr++;                  $nr++;

Legend:
Removed from v.8  
changed lines
  Added in v.9

  ViewVC Help
Powered by ViewVC 1.1.26