save collection_items in x_meta
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 2 Aug 2012 12:02:13 +0000 (14:02 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 2 Aug 2012 12:02:13 +0000 (14:02 +0200)
zotero.pl

index e3b9051..a0a424b 100755 (executable)
--- a/zotero.pl
+++ b/zotero.pl
@@ -49,12 +49,13 @@ if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) {
        warn "not modified";
 }
 
-my $feed = eval { XMLin( $file ) };
+my $xml = XML::Simple->new(ForceArray => [ qw( entry ) ]);
+my $feed = eval { $xml->XMLin( $file ) };
 if ( $! ) {
        warn "ERROR $file $!\n";
        goto skip_url;
 }
-#warn "# feed ",dump($feed);
+warn "# feed ",dump($feed);
 
 sub link_to_id {
        my $link = shift;
@@ -63,12 +64,16 @@ sub link_to_id {
        return $link;
 }
 
+my @collection_items;
+
 foreach my $entry ( keys %{ $feed->{entry} } ) {
        warn "# entry $entry ",dump($entry);
        my $id = link_to_id $entry;
 
+       push @collection_items, $id if $url =~ m{/collections/(\w+)/items};
+
        my $item = $feed->{entry}->{$entry};
-       warn "# entry $entry ",dump($item);
+       warn "# item $id $entry ",dump($item),$/;
 
        foreach my $i ( 0 .. $#{ $item->{link} } ) {
                my $link = $item->{link}->[$i];
@@ -80,7 +85,7 @@ foreach my $entry ( keys %{ $feed->{entry} } ) {
                        push @{ $tree->{$key} }, $id;
                } elsif ( $link->{rel} eq 'self' && $link->{href} =~ m{/collections/} ) {
                        warn "# get items in this collection";
-                       push @urls, "$link->{href}/items"; 
+                       push @urls, "$link->{href}/items?content=json";
                }
        }
 
@@ -123,6 +128,15 @@ foreach my $entry ( keys %{ $feed->{entry} } ) {
 
 }
 
+if ( @collection_items ) {
+       my $id = $1 if $url =~ m{/collections/(\w+)/items};
+       $db->modify( "zotero_$UserID/$id" => sub {
+               my $doc = shift;
+               $doc->{x_meta}->{collection_items} = [ @collection_items ];
+               return $doc;
+       });
+}
+
 delete $feed->{entry};
 warn "# feed without entry ",dump( $feed );