/[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 5 by dpavlin, Sat Jun 20 19:42:00 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            $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_marc {
118            my ($self) = @_;
119    
120          my $comarc;          my $comarc;
121    
122          if ( $mech->content =~ m{<pre>\s*(.+1\..+?)\s*</pre>}s ) {          if ( $mech->content =~ m{<pre>\s*(.+?(\d+\.)\s+ID=(\d+).+?)\s*</pre>}s ) {
123    
124                  my $comarc = $1;                  my $comarc = $1;
125                    my $nr = $2;
126                    my $id = $3;
127    
128    diag "# fetch_marc $nr [$id]";
129    
130                  $comarc =~ s{</?b>}{}gs;                  $comarc =~ s{</?b>}{}gs;
131                  $comarc =~ s{<(/?font)[^>]*>}{<sf>}gs;                  $comarc =~ s{<font[^>]*>}{<s>}gs;
132                    $comarc =~ s{</font>}{<e>}gs;
133    
134                    write_file "comarc/$id", $comarc;
135    
136                  print $comarc;                  print $comarc;
137    
138                  return $comarc;                  my $marc = MARC::Record->new;
139    
140                    foreach my $line ( split(/[\r\n]+/, $comarc) ) {
141                            our @f;
142    
143                            if ( $line !~ s{^(\d\d\d)([01 ])([01 ])}{} ) {
144                                    diag "SKIP: $line";
145                            } else {
146                                    $line .= "<eol>";
147    
148                                    @f = ( $1, $2, $3 );
149                                    sub sf { push @f, @_; }
150                                    $line =~ s{<s>(\w)<e>([^<]+)\s*}{sf($1, $2)}ges;
151                                    diag "# f:", join('|', @f), " left: |$line|";
152                                    $marc->add_fields( @f );
153                            }
154                    }
155    
156                    open(my $out, '>:utf8', "marc/$id");
157                    print $out $marc->as_usmarc;
158                    close($out);
159    
160                    diag $marc->as_formatted;
161    
162                    return $marc->as_usmarc;
163          } else {          } else {
164                  die "can't fetch COMARC format from ", $mech->content;                  die "can't fetch COMARC format from ", $mech->content;
165          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26