7 use Data::Dump qw/dump/;
13 my $debug = $ENV{DEBUG} || 0;
19 # http://dp.la/dev/wiki/Item_API
21 # Base Fields: Mapping to a set of common terms
22 # Field name Field description
23 # dpla.keyword Almost all of a record's fields get copied to this field
24 # dpla.title The title and/or subtitle of the item. Exact matching.
25 # dpla.title_keyword The title and/or subtitle of the item. Keyword matching.
26 # dpla.creator The creator(s), contributor(s), editor(s), etc. of the item. Exact matching
27 # dpla.creator_keyword The creator(s), contributor(s), editor(s), etc. of the item. Keyword matching
28 # dpla.date The item's date of publication.
29 # dpla.description The item's description. This often includes the item's Table of Contents. Exact matching.
30 # dpla.description_keyword The item's description. This often includes the item's Table of Contents. Keyword matching.
31 # dpla.subject A catchall for subject information. LCSH, Dewey, and other tag related fields are copied to this field. Exact matching.
32 # dpla.subject_keyword A catchall for subject information. LCSH, Dewey, and other tag related fields are copied to this field. Keyword matching.
33 # dpla.publisher The name of the publisher. Exact matching.
34 # dpla.language The primary language of the item. Exact matching.
35 # dpla.isbn The item's ISBN. Exact matching.
36 # dpla.oclc The item's OCLC identifier. Exact matching.
37 # dpla.lccn The item's LCCN. Exact matching.
38 # dpla.call_num The item's call number. Exact matching.
39 # dpla.content_link A link to the item's content. Exact matching.
40 # dpla.contributor The contributing partner. Exact matching.
41 # dpla.resource_type The resource's type. Common values include item and collection. Exact matching.
45 # Bib-1 @and @and @and @and @and @and @and @or
51 # @attr 1=21 subject-holding
52 # @attr 1=12 control-no
53 # @attr 1=1007 standard-id
57 4 => 'dpla.title_keyword',
59 8 => 'dpla.keyword', # XXX fake
60 1003 => 'dpla.creator_keyword',
65 1016 => 'dpla.keyword',
69 Or => '&', # FIXME sigh, not really supported?
72 my ( $prefix, $term ) = @_;
73 return 'filter=' . $prefix . ':' . $term;
78 my ( $self, $query ) = @_;
80 die "need query" unless defined $query;
82 my $url = 'http://api.dp.la/v0.03/item/?' . $query;
86 my $mech = $self->mech;
90 my $json = decode_json $mech->content;
91 diag "# json = ", dump($json) if $debug;
95 if ( exists $json->{num_found} ) {
96 $hits = $json->{num_found};
98 diag "get't find num_found in ", $mech->content;
102 diag "got $hits results";
104 $self->{_json} = $json;
106 return $self->{hits} = $hits;
110 my ($self,$format) = @_;
114 my $item = shift @{ $self->{_json}->{docs} };
116 my $marc = MARC::Record->new;
117 $marc->encoding('utf-8');
119 my $fields; # empty marc
121 foreach my $key ( sort keys %$item ) {
122 my $v = $item->{$key};
123 warn "# item ",dump( $key, $v ) if $debug;
124 if ( $key =~ m/^(\d\d\d)(\w)$/ ) {
125 my ($f,$sf) = ($1,$2);
127 # XXX do magic and unroll into proper MARC record
129 $v = [ $v ] unless ref $v eq 'ARRAY';
132 if ( $fields->[0]->[0] ne $f ) {
133 $marc->add_fields( @$fields );
134 warn "# add_fields ",dump($fields) if $debug;
138 foreach my $i ( 0 .. $#$v ) {
139 $fields->[$i]->[0] = $f;
140 $fields->[$i]->[1] = ' ';
141 $fields->[$i]->[2] = ' ';
142 push @{ $fields->[$i] }, $sf, decode('utf-8', $v->[$i]); # decode here fixes API encoding error
146 warn "# IGNORED: $key ", dump($item->{$key}), "\n";
150 $marc->add_fields( @$fields );
152 diag "# marc ", $marc->as_formatted;
154 warn dump( $marc->as_usmarc );
156 $self->mech->back; # return to search results for next page
158 my $id = $item->{'dpla.id'};
161 warn "no dpla.id in ",dump($item);
165 $self->save_marc( "$id.marc", $marc->as_usmarc );