/[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 4 by dpavlin, Sat Jun 20 19:28:04 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 File::Slurp;  use Data::Dump qw/dump/;
9    
10  binmode STDOUT, ':utf8';  binmode STDOUT, ':utf8';
11    
# Line 17  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 76  sub search { Line 76  sub search {
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  diag "# get $url";  diag "get $url";
80    
81          $mech->get( $url );          $mech->get( $url );
82    
83  diag "# got session";  diag "got session";
84    
85          $mech->follow_link( text_regex => qr/union/ );          $mech->follow_link( text_regex => qr/union/ );
86    
87  diag "# switch to advanced form (select)";  diag "switch to advanced form (select)";
   
 diag $mech->content;  
88    
89          $mech->follow_link( url_regex => qr/mode=3/ );          $mech->follow_link( url_regex => qr/mode=3/ );
90    
91  diag "# submit search $query";  diag "submit search $query";
92    
93          $mech->submit_form(          $mech->submit_form(
94                  fields => {                  fields => {
# Line 106  diag "# submit search $query"; Line 104  diag "# submit search $query";
104                  return;                  return;
105          }          }
106    
107  diag "# got $hits results, get first one";  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  diag "# in COMARC format";  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_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    
126          if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\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;                  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;
136                  $comarc =~ s{</font>}{<e>}gs;                  $comarc =~ s{</font>}{<e>}gs;
137    
138                  write_file "comarc/$id", $comarc;                  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                                  diag "SKIP: $line";                                  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 { 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++;
200                    $mech->follow_link( url_regex => qr/rec=$nr/ );
201    
202                  return $marc->as_usmarc;                  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;

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

  ViewVC Help
Powered by ViewVC 1.1.26