7 use Data::Dump qw/dump/;
12 my $debug = $ENV{DEBUG} || 0;
18 # based on http://code.google.com/apis/books/docs/v1/using.html#PerformingSearch
20 # https://www.googleapis.com/books/v1/volumes?q=search+terms
22 # This request has a single required parameter:
24 # q - Search for volumes that contain this text string. There are special keywords you can specify in the search terms to search in particular fields, such as:
25 # intitle: Returns results where the text following this keyword is found in the title.
26 # inauthor: Returns results where the text following this keyword is found in the author.
27 # inpublisher: Returns results where the text following this keyword is found in the publisher.
28 # subject: Returns results where the text following this keyword is listed in the category list of the volume.
29 # isbn: Returns results where the text following this keyword is the ISBN number.
30 # lccn: Returns results where the text following this keyword is the Library of Congress Control Number.
31 # oclc: Returns results where the text following this keyword is the Online Computer Library Center number.
36 # Bib-1 @and @and @and @and @and @and @and @or
42 # @attr 1=21 subject-holding
43 # @attr 1=12 control-no
44 # @attr 1=1007 standard-id
50 8 => 'isbn:', # FIXME?
60 my ( $self, $query ) = @_;
62 die "need query" unless defined $query;
64 my $url = 'https://www.googleapis.com/books/v1/volumes?q=' . $query;
68 my $mech = $self->{mech} || die "no mech?";
71 my $json = decode_json $mech->content;
72 diag "# json = ", dump($json) if $debug;
76 if ( exists $json->{items} ) {
77 $hits = $#{ $json->{items} } + 1;
79 diag "get't find results in ", $mech->content;
83 diag "got $hits results, get first one";
85 $self->{_json} = $json;
86 $self->{_json_item} = 0;
88 return $self->{hits} = $hits;
93 my ($self,$format) = @_;
97 my $item = $self->{_json}->{items}->[ $self->{_json_item}++ ];
99 warn "# item = ",dump($item) if $debug;
101 my $id = $item->{id} || die "no id";
103 my $marc = MARC::Record->new;
104 $marc->encoding('utf-8');
106 if ( my $vi = $item->{volumeInfo} ) {
108 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
110 $marc->add_fields('008',sprintf("%02d%02d%02ds%04d%25s%-3s",
111 $year % 100, $mon + 1, $mday, substr($vi->{publishedDate},0,4), ' ', $vi->{language}));
113 if ( ref $vi->{industryIdentifiers} eq 'ARRAY' ) {
114 foreach my $i ( @{ $vi->{industryIdentifiers} } ) {
115 if ( $i->{type} =~ m/ISBN/i ) {
116 $marc->add_fields('020',' ',' ','a' => $i->{identifier} )
118 $marc->add_fields('035',' ',' ','a' => $i->{identifier} )
124 if ( ref $vi->{authors} eq 'ARRAY' ) {
125 $first_author = shift @{ $vi->{authors} };
126 $marc->add_fields(100,'0',' ','a' => $first_author );
127 $marc->add_fields(700,'0',' ','a' => $_ ) foreach @{ $vi->{authors} };
130 $marc->add_fields(245, ($first_author ? '1':'0') ,' ',
132 $vi->{subtitle} ? ( 'b' => $vi->{subtitle} ) : (),
135 $marc->add_fields(260,' ',' ',
136 $vi->{publisher} ? ( 'b' => $vi->{publisher} ) : (),
137 $vi->{publishedDate} ? ( 'c' => $vi->{publishedDate} ) : ()
140 $marc->add_fields(300,' ',' ','a' => $vi->{pageCount} . 'p.' ) if $vi->{pageCount};
142 $marc->add_fields(520,' ',' ','a' => $vi->{description} ) if $vi->{description};
144 if ( ref $vi->{categories} eq 'ARRAY' ) {
145 $marc->add_fields(650,' ','4','a' => $_ ) foreach @{ $vi->{categories} };
148 if ( exists $vi->{imageLinks} ) {
150 $marc->add_fields(856,'4','2',
152 'u' => $vi->{imageLinks}->{smallThumbnail},
153 'x' => 'smallThumbnail',
154 ) if exists $vi->{imageLinks}->{smallThumbnail};
155 $marc->add_fields(856,'4','2',
157 'u' => $vi->{imageLinks}->{thumbnail},
159 ) if exists $vi->{imageLinks}->{thumbnail};
163 $marc->add_fields(856,'4','2',
165 'u' => $vi->{infoLink},
167 $marc->add_fields(856,'4','2',
168 '3'=> 'Show reviews link',
169 'u' => $vi->{showReviewsLink},
172 my $leader = $marc->leader;
173 warn "# leader [$leader]";
174 $leader =~ s/^(....).../$1nam/;
175 $marc->leader( $leader );
178 warn "ERROR: no volumeInfo in ",dump($item);
181 $marc->add_fields( 856, ' ', ' ', 'u' => $item->{accessInfo}->{webReaderLink} );
182 # $marc->add_fields( 520, ' ', ' ', 'a' => $item->{searchInfo}->{textSnippet} ); # duplicate of description
184 # diag "# hash ",dump($hash);
185 diag "# marc ", $marc->as_formatted;
187 $self->save_marc( "$id.marc", $marc->as_usmarc );