/[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 8 by dpavlin, Sat Jun 20 22:09:33 2009 UTC
# Line 30  my $cobiss_marc21 = { Line 30  my $cobiss_marc21 = {
30          },          },
31  };  };
32    
33    our $mech = WWW::Mechanize->new();
34    our $hits;
35    
36    sub diag {
37            print "# ", @_, $/;
38    }
39    
40    # Koha Z39.50 query:
41    #
42    # Bib-1 @and @and @and @and @and @and @and @or
43    # @attr 1=8 isbn-issn
44    # @attr 1=7 isbn-issn
45    # @attr 1=4 title
46    # @attr 1=1003 author
47    # @attr 1=16 dewey
48    # @attr 1=21 subject-holding
49    # @attr 1=12 control-no
50    # @attr 1=1007 standard-id
51    # @attr 1=1016 any
52    
53    our $usemap = {
54            8               => 'BN',        # FIXME check
55            7               => 'SN',        # FIXME check
56            4               => 'TI',
57            1003    => 'TI',
58            16              => 'CU',
59            21              => 'SU',
60    #       12              => '',
61    #       1007    => '',
62    #       1016    => '',
63    
64    };
65    
66    sub usemap {
67            my $f = shift || die;
68            $usemap->{$f};
69    }
70    
71  sub search {  sub search {
72            my ( $self, $query ) = @_;
73    
74            die "need query" unless defined $query;
75    
76          my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';          my $url = 'http://cobiss.izum.si/scripts/cobiss?ukaz=GETID&lani=en';
77    
78  warn "# get $url\n";  diag "get $url";
79    
         my $mech = WWW::Mechanize->new();  
80          $mech->get( $url );          $mech->get( $url );
81    
82  warn "# got session\n";  diag "got session";
83    
84          $mech->follow_link( text_regex => qr/union/ );          $mech->follow_link( text_regex => qr/union/ );
85    
86  warn "# submit search\n";  diag "switch to advanced form (select)";
87    
88            $mech->follow_link( url_regex => qr/mode=3/ );
89    
90    diag "submit search $query";
91    
92          $mech->submit_form(          $mech->submit_form(
93                  fields => {                  fields => {
94                          'SS1' => 'Krleza',                          'SS1' => $query,
95                  },                  },
96          );          );
97    
98          my $hits = 1;          $hits = 0;
99          if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {          if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
100                  $hits = $1;                  $hits = $1;
101          } else {          } else {
102                  warn "get't find results in ", $mech->content;                  diag "get't find results in ", $mech->content;
103                    return;
104          }          }
105    
106  warn "# got $hits results, get first one\n";  diag "got $hits results, get first one";
107    
108          $mech->follow_link( url_regex => qr/ukaz=DISP/ );          $mech->follow_link( url_regex => qr/ukaz=DISP/ );
109    
110  warn "# in COMARC format\n";  diag "in COMARC format";
111    
112          $mech->follow_link( url_regex => qr/fmt=13/ );          $mech->follow_link( url_regex => qr/fmt=13/ );
113    }
114    
115    
116    sub fetch_marc {
117            my ($self) = @_;
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]";
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                                  $line .= "<eol>";                                  $line .= "<eol>";
147    
148                                  @f = ( $1, $2, $3 );                                  our @f = ( $1, $2, $3 );
149                                  sub sf { warn "sf",@_,"|",@f; push @f, @_; }                                  sub sf { push @f, @_; }
150                                  $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;                                  $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;
151                                  warn "# f:", join(' ', @f), " left:|$line|\n";                                  diag "f:", join('|', @f), " left: |$line|";
152                                  $marc->add_fields( @f );                                  $marc->add_fields( @f );
153                          }                          }
154                  }                  }
155    
156                  open(my $out, '>:utf8', 'out.marc');                  open(my $out, '>:utf8', "marc/$id");
157                  print $out $marc->as_usmarc;                  print $out $marc->as_usmarc;
158                  close($out);                  close($out);
159    
160                  warn $marc->as_formatted;                  diag $marc->as_formatted;
161    
162                    $nr++;
163                    $mech->follow_link( url_regex => qr/rec=$nr/ );
164    
165                  return $comarc;                  return $marc->as_usmarc;
166          } else {          } else {
167                  die "can't fetch COMARC format from ", $mech->content;                  die "can't fetch COMARC format from ", $mech->content;
168          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26