extract common code into Scraper package
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 22 Oct 2010 23:09:55 +0000 (01:09 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 22 Oct 2010 23:09:55 +0000 (01:09 +0200)
Aleph.pm
COBISS.pm
Scraper.pm [new file with mode: 0644]

index f89aaf7..71d67a7 100644 (file)
--- a/Aleph.pm
+++ b/Aleph.pm
@@ -3,11 +3,10 @@ package Aleph;
 use warnings;
 use strict;
 
-use WWW::Mechanize;
 use MARC::Record;
 use Data::Dump qw/dump/;
 
-binmode STDOUT, ':utf8';
+use base 'Scraper';
 
 our $mech = WWW::Mechanize->new();
 our $hits;
@@ -53,9 +52,7 @@ sub diag {
 # WGA - Riječi u geografskim odrednicama 
 # WYR - Godina izdavanja
 
-our $usemap = {
-#      8               => '',
-#      7               => '',
+sub usemap {{
        4               => 'WTI',
        1003    => 'WTI',
        16              => 'CU',
@@ -66,10 +63,6 @@ our $usemap = {
 
 };
 
-sub usemap {
-       my $f = shift || die;
-       $usemap->{$f};
-}
 
 sub search {
        my ( $self, $query ) = @_;
@@ -81,6 +74,7 @@ sub search {
 
 diag "get $url";
 
+       my $mech = $self->{mech} || die "no mech?";
        $mech->get( $url );
 
 diag "advanced search";
@@ -111,12 +105,16 @@ diag "got $hits results, get first one";
 diag "in MARC format";
 
        $mech->follow_link( url_regex => qr/format=001/ );
+
+       return $hits;
 }
 
 
 sub next_marc {
        my ($self,$format) = @_;
 
+       my $mech = $self->{mech} || die "no mech?";
+
 print $mech->content;
 
        if ( $mech->content =~ m{Zapis\s+(\d+)}s ) {
index ce1eea0..5611007 100644 (file)
--- a/COBISS.pm
+++ b/COBISS.pm
@@ -3,19 +3,10 @@ package COBISS;
 use warnings;
 use strict;
 
-use WWW::Mechanize;
 use MARC::Record;
 use Data::Dump qw/dump/;
 
-binmode STDOUT, ':utf8';
-
-sub new {
-    my ( $class ) = @_;
-    my $self = {};
-    bless $self, $class;
-    return $self;
-}
-
+use base 'Scraper';
 
 my $cobiss_marc21 = {
        '010' => { a => [ '020', 'a' ] },
@@ -78,7 +69,8 @@ sub search {
 
 diag "get $url";
 
-       my $mech = $self->{mech} = WWW::Mechanize->new();
+       my $mech = $self->{mech} || die "no mech?";
+
        my $hits;
        $mech->get( $url );
 
diff --git a/Scraper.pm b/Scraper.pm
new file mode 100644 (file)
index 0000000..e1b13f4
--- /dev/null
@@ -0,0 +1,18 @@
+package Scraper;
+
+use warnings;
+use strict;
+
+use WWW::Mechanize;
+
+binmode STDOUT, ':utf8';
+
+sub new {
+    my ( $class ) = @_;
+    my $self = {
+               mech => WWW::Mechanize->new(),
+       };
+    bless $self, $class;
+    return $self;
+}
+