/[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 1 by dpavlin, Fri Jun 19 17:50:34 2009 UTC revision 4 by dpavlin, Sat Jun 20 19:28:04 2009 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5    
6  use WWW::Mechanize;  use WWW::Mechanize;
7    use MARC::Record;
8    use File::Slurp;
9    
10    binmode STDOUT, ':utf8';
11    
12    my $cobiss_marc21 = {
13            '010' => { a => [ '020', 'a' ] },
14             200  => {
15                            a => [  245 , 'a' ],
16                            f => [  245 , 'f' ],
17            },
18             205  => { a => [  250 , 'a' ] },
19             210  => {
20                    a => [  250 , 'a' ],
21                    c => [  260 , 'b' ],
22                    d => [  260 , 'c' ],
23            },
24            215 => {
25                    a => [  300 , 'a' ],
26                    c => [  300 , 'b' ],
27                    d => [  300 , 'c' ],
28            },
29            700 => {
30                    a => [  100 , 'a' ],
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    diag $mech->content;
90    
91            $mech->follow_link( url_regex => qr/mode=3/ );
92    
93    diag "# submit search $query";
94    
95          $mech->submit_form(          $mech->submit_form(
96                  fields => {                  fields => {
97                          'SS1' => 'Krleza',                          'SS1' => $query,
98                  },                  },
99          );          );
100    
101          my $hits = 1;          $hits = 0;
102          if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {          if ( $mech->content =~ m{hits:\s*<b>\s*(\d+)\s*</b>}s ) {
103                  $hits = $1;                  $hits = $1;
104          } else {          } else {
105                  warn "get't find results in ", $mech->content;                  diag "get't find results in ", $mech->content;
106                    return;
107          }          }
108    
109  warn "# got $hits results, get first one\n";  diag "# got $hits results, get first one";
110    
111          $mech->follow_link( url_regex => qr/ukaz=DISP/ );          $mech->follow_link( url_regex => qr/ukaz=DISP/ );
112    
113  warn "# in COMARC format\n";  diag "# in COMARC format";
114    
115          $mech->follow_link( url_regex => qr/fmt=13/ );          $mech->follow_link( url_regex => qr/fmt=13/ );
116    }
117    
118    
119    sub fetch_marc {
120            my ($self) = @_;
121    
122          my $comarc;          my $comarc;
123    
124          if ( $mech->content =~ m{<pre>\s*(.+1\..+?)\s*</pre>}s ) {          if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\s*</pre>}s ) {
125    
126                  my $comarc = $1;                  my $comarc = $1;
127                    my $nr = $2;
128                    my $id = $3;
129    
130    diag "# fetch_marc $nr [$id]";
131    
132                  $comarc =~ s{</?b>}{}gs;                  $comarc =~ s{</?b>}{}gs;
133                  $comarc =~ s{<(/?font)[^>]*>}{<sf>}gs;                  $comarc =~ s{<font[^>]*>}{<s>}gs;
134                    $comarc =~ s{</font>}{<e>}gs;
135    
136                    write_file "comarc/$id", $comarc;
137    
138                  print $comarc;                  print $comarc;
139    
140                  return $comarc;                  my $marc = MARC::Record->new;
141    
142                    foreach my $line ( split(/[\r\n]+/, $comarc) ) {
143                            our @f;
144    
145                            if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
146                                    diag "SKIP: $line";
147                            } else {
148                                    $line .= "<eol>";
149    
150                                    @f = ( $1, $2, $3 );
151                                    sub sf { push @f, @_; }
152                                    $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;
153                                    diag "# f:", join('|', @f), " left: |$line|";
154                                    $marc->add_fields( @f );
155                            }
156                    }
157    
158                    open(my $out, '>:utf8', "marc/$id");
159                    print $out $marc->as_usmarc;
160                    close($out);
161    
162                    diag $marc->as_formatted;
163    
164                    return $marc->as_usmarc;
165          } else {          } else {
166                  die "can't fetch COMARC format from ", $mech->content;                  die "can't fetch COMARC format from ", $mech->content;
167          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26