7 use Data::Dump qw/dump/;
12 my $pageCount_suffix = 'p.'; # English
13 $pageCount_suffix = ' str.'; # Croatian
15 my $debug = $ENV{DEBUG} || 0;
21 # based on http://code.google.com/apis/books/docs/v1/using.html#PerformingSearch
23 # https://www.googleapis.com/books/v1/volumes?q=search+terms
25 # This request has a single required parameter:
27 # 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:
28 # intitle: Returns results where the text following this keyword is found in the title.
29 # inauthor: Returns results where the text following this keyword is found in the author.
30 # inpublisher: Returns results where the text following this keyword is found in the publisher.
31 # subject: Returns results where the text following this keyword is listed in the category list of the volume.
32 # isbn: Returns results where the text following this keyword is the ISBN number.
33 # lccn: Returns results where the text following this keyword is the Library of Congress Control Number.
34 # oclc: Returns results where the text following this keyword is the Online Computer Library Center number.
39 # Bib-1 @and @and @and @and @and @and @and @or
45 # @attr 1=21 subject-holding
46 # @attr 1=12 control-no
47 # @attr 1=1007 standard-id
53 8 => 'isbn:', # FIXME?
63 my ( $self, $query ) = @_;
65 die "need query" unless defined $query;
67 my $url = 'https://www.googleapis.com/books/v1/volumes?q=' . $query;
71 my $mech = $self->{mech} || die "no mech?";
74 my $json = decode_json $mech->content;
75 diag "# json = ", dump($json) if $debug;
79 if ( exists $json->{items} ) {
80 $hits = $#{ $json->{items} } + 1;
82 diag "get't find results in ", $mech->content;
86 diag "got $hits results, get first one";
88 $self->{_json} = $json;
89 $self->{_json_item} = 0;
91 return $self->{hits} = $hits;
96 my ($self,$format) = @_;
100 my $item = $self->{_json}->{items}->[ $self->{_json_item}++ ];
102 warn "# item = ",dump($item) if $debug;
104 my $id = $item->{id} || die "no id";
106 my $marc = MARC::Record->new;
107 $marc->encoding('utf-8');
109 if ( my $vi = $item->{volumeInfo} ) {
111 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
113 $marc->add_fields('008',sprintf("%02d%02d%02ds%04d%25s%-3s",
114 $year % 100, $mon + 1, $mday, substr($vi->{publishedDate},0,4), ' ', $vi->{language}));
116 if ( ref $vi->{industryIdentifiers} eq 'ARRAY' ) {
117 foreach my $i ( @{ $vi->{industryIdentifiers} } ) {
118 if ( $i->{type} =~ m/ISBN/i ) {
119 $marc->add_fields('020',' ',' ','a' => $i->{identifier} )
121 $marc->add_fields('035',' ',' ','a' => $i->{identifier} )
127 if ( ref $vi->{authors} eq 'ARRAY' ) {
128 $first_author = shift @{ $vi->{authors} };
129 $marc->add_fields(100,'0',' ','a' => $first_author );
130 $marc->add_fields(700,'0',' ','a' => $_ ) foreach @{ $vi->{authors} };
133 $marc->add_fields(245, ($first_author ? '1':'0') ,' ',
135 $vi->{subtitle} ? ( 'b' => $vi->{subtitle} ) : (),
138 if ( exists $vi->{publisher} or exists $vi->{publishedDate} ) {
139 $marc->add_fields(260,' ',' ',
140 $vi->{publisher} ? ( 'b' => $vi->{publisher} ) : (),
141 $vi->{publishedDate} ? ( 'c' => $vi->{publishedDate} ) : ()
145 $marc->add_fields(300,' ',' ','a' => $vi->{pageCount} . $pageCount_suffix ) if $vi->{pageCount};
147 $marc->add_fields(520,' ',' ','a' => $vi->{description} ) if $vi->{description};
149 if ( ref $vi->{categories} eq 'ARRAY' ) {
150 $marc->add_fields(650,' ','4','a' => $_ ) foreach @{ $vi->{categories} };
153 if ( exists $vi->{imageLinks} ) {
155 $marc->add_fields(856,'4','2',
157 'u' => $vi->{imageLinks}->{smallThumbnail},
158 'x' => 'smallThumbnail',
159 ) if exists $vi->{imageLinks}->{smallThumbnail};
160 $marc->add_fields(856,'4','2',
162 'u' => $vi->{imageLinks}->{thumbnail},
164 ) if exists $vi->{imageLinks}->{thumbnail};
168 $marc->add_fields(856,'4','2',
170 'u' => $vi->{infoLink},
172 $marc->add_fields(856,'4','2',
173 '3'=> 'Show reviews link',
174 'u' => $vi->{showReviewsLink},
177 my $leader = $marc->leader;
178 warn "# leader [$leader]";
179 $leader =~ s/^(....).../$1nam/;
180 $marc->leader( $leader );
183 warn "ERROR: no volumeInfo in ",dump($item);
186 $marc->add_fields( 856, ' ', ' ', 'u' => $item->{accessInfo}->{webReaderLink} );
187 # $marc->add_fields( 520, ' ', ' ', 'a' => $item->{searchInfo}->{textSnippet} ); # duplicate of description
189 # diag "# hash ",dump($hash);
190 diag "# marc ", $marc->as_formatted;
192 $self->save_marc( "$id.marc", $marc->as_usmarc );