map all Google Books JSON to MARC21
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 17 Apr 2012 22:25:53 +0000 (00:25 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 17 Apr 2012 22:25:53 +0000 (00:25 +0200)
GoogleBooks.pm
t/3-GoogleBooks.t

index 68da2a1..df4d09a 100644 (file)
@@ -100,17 +100,70 @@ sub next_marc {
 
        warn "# item = ",dump($item) if $debug;
 
+       my $id = $item->{id} || die "no id";
+
        $marc = MARC::Record->new;
        $marc->encoding('utf-8');
 
+       if ( my $vi = $item->{volumeInfo} ) {
+
+               my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
+
+               $marc->add_fields('008',sprintf("%02d%02d%02ds%04d%25s%-3s",
+                               $year % 100, $mon + 1, $mday, $vi->{publishedDate}, ' ', $vi->{language}));
+
+               $marc->add_fields('020',' ',' ','a' => $_ ) foreach map { $_->{identifier} } @{ $vi->{industryIdentifiers} };
+
+               my $first_author = shift @{ $vi->{authors} };
+               $marc->add_fields(100,'0',' ','a' => $first_author );
+               $marc->add_fields(700,'0',' ','a' => $_ ) foreach @{ $vi->{authors} };
+               $marc->add_fields(245, ($first_author ? '1':'0') ,' ',
+                       'a' => $vi->{title},
+                       'b' => $vi->{subtitle},
+               );
+               $marc->add_fields(260,' ',' ',
+                       'b' => $vi->{publisher},
+                       'c' => $vi->{publishedDate},
+               );
+               $marc->add_fields(300,' ',' ','a' => $vi->{pageCount} . 'p.' );
+               
+               $marc->add_fields(520,' ',' ','a' => $vi->{description} );
+               $marc->add_fields(650,' ','4','a' => $_ ) foreach @{ $vi->{categories} };
+
+               $marc->add_fields(856,'4','2',
+                       '3'=> 'Image link',
+                       'u' => $vi->{imageLinks}->{smallThumbnail},
+                       'x' => 'smallThumbnail',
+               );
+               $marc->add_fields(856,'4','2',
+                       '3'=> 'Image link',
+                       'u' => $vi->{imageLinks}->{thumbnail},
+                       'x' => 'thumbnail',
+               );
+               $marc->add_fields(856,'4','2',
+                       '3'=> 'Info link',
+                       'u' => $vi->{infoLink},
+               );
+               $marc->add_fields(856,'4','2',
+                       '3'=> 'Show reviews link',
+                       'u' => $vi->{showReviewsLink},
+               );
+
+               my $leader = $marc->leader;
+               warn "# leader [$leader]";
+               $leader =~ s/^(....).../$1nam/;
+               $marc->leader( $leader );
+
+       } else {
+               warn "ERROR: no volumeInfo in ",dump($item);
+       }
+
        $marc->add_fields( 856, ' ', ' ', 'u' => $item->{accessInfo}->{webReaderLink} );
-       $marc->add_fields( 520, ' ', ' ', 'a' => $item->{searchInfo}->{textSnippet} );
+#      $marc->add_fields( 520, ' ', ' ', 'a' => $item->{searchInfo}->{textSnippet} ); # duplicate of description
 
 #      diag "# hash ",dump($hash);
        diag "# marc ", $marc->as_formatted;
 
-       my $id = $item->{id} || die "no id";
-
        $self->save_marc( "$id.marc", $marc->as_usmarc );
 
        return $id;
index 3889d7c..62156d0 100755 (executable)
@@ -3,7 +3,7 @@
 use warnings;
 use strict;
 
-use Test::More tests => 7;
+use Test::More tests => 14;
 
 use_ok 'GoogleBooks';
 
@@ -12,7 +12,7 @@ ok( my $o = GoogleBooks->new(), 'new' );
 ok( my $hits = $o->search( 'croatia' ), 'search' );
 like $hits, qr/^\d+$/, "hits: $hits";
 
-foreach ( 1 .. 3 ) {
+foreach ( 1 .. 10 ) {
 
        ok( my $marc = $o->next_marc, "next_marc $_" );
        diag $marc;