collection_items view
[ILL-Zotero-RT] / zotero.pl
index 2dac6e0..2dda784 100755 (executable)
--- a/zotero.pl
+++ b/zotero.pl
@@ -7,6 +7,7 @@ use XML::Simple;
 use JSON;
 use Data::Dump qw(dump);
 use RT::Client::REST;
+use URI::Escape;
 
 use CouchDB;
 use Digest::MD5 qw(md5_hex);
@@ -25,6 +26,8 @@ eval {
        $db->put("zotero_$UserID/_design/zotero" => decode_json $view)
 };
 
+eval { $db->put("rt") }; # create RT database
+
 my @urls = map { "https://api.zotero.org/users/$UserID/$_?format=atom&content=json&order=dateModified&sort=desc" } qw( collections items );
 # we don't need to fetch tags since we can generate using CouchDB views
 
@@ -41,14 +44,18 @@ $url .= '&key=' . $key;
 my $file = $UserID . '.' . md5_hex($url) . '.atom';
 $FETCH = 1 if ! -e $file;
 
-warn "# $url -> $file\n";
+warn "# mirror $FETCH $url -> $file\n";
 if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) {
        warn "not modified";
-#      exit 0;
 }
 
-my $feed = XMLin( $file );
-#warn "# feed ",dump($feed);
+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);
 
 sub link_to_id {
        my $link = shift;
@@ -57,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];
@@ -72,6 +83,9 @@ foreach my $entry ( keys %{ $feed->{entry} } ) {
 
                if ( $link->{rel} eq 'up' ) {
                        push @{ $tree->{$key} }, $id;
+               } elsif ( $link->{rel} eq 'self' && $link->{href} =~ m{/collections/} ) {
+                       warn "# get items in this collection";
+                       push @urls, "$link->{href}/items?content=json";
                }
        }
 
@@ -110,23 +124,17 @@ foreach my $entry ( keys %{ $feed->{entry} } ) {
 
        $items->{$id} = $item;
 
-       my $json_md5 = md5_hex encode_json $item;
-       $item->{zapi}->{json_md5} = $json_md5;
+       $db->modify( "zotero_$UserID/$id" => $item );
 
-       if ( my $old_item = eval { $db->get( "zotero_$UserID/$id" ) } ) {
-               warn "# old_item ", $old_item->{_rev}; #dump($old_item);
+}
 
-               if ( $old_item->{zapi}->{etag} ne $item->{zapi}->{etag} || $json_md5 ne $old_item->{zapi}->{json_md5} ) {
-                       $item->{_rev} = $old_item->{_rev};
-                       warn :"# update $id";
-                       $db->put( "zotero_$UserID/$id" => $item );
-               } else {
-                       warn "# unchanged";
-               }
-       } else {
-               $db->put( "zotero_$UserID/$id" => $item );
-               warn "# insert $id ", dump($item);
-       }
+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};
@@ -138,6 +146,8 @@ if ( my @next = map { $_->{href} } grep { $_->{rel} eq 'next' && $_->{type} eq '
        goto restart;
 }
 
+skip_url:
+
 if ( $url = shift @urls ) {
        warn "## next url $url";
        goto restart;
@@ -157,9 +167,54 @@ $rt->login(username => $ENV{RT_USER}, password => $ENV{RT_PASSWORD});
 
 foreach my $nr ( keys %$ticket_items ) {
 
-       my $ticket = $rt->show(type => 'ticket', id => $nr);
+       my $ticket = eval { $rt->show(type => 'ticket', id => $nr) };
        warn "# ticket $nr ",dump($ticket);
 
+       next unless $ticket;
+
+       $ticket->{zotero_items} = $ticket_items->{$nr};
+
+       my $modified = $db->modify( "rt/$nr" => sub {
+               my $doc = shift;
+               $doc->{$_} = $ticket->{$_} foreach keys %$ticket;
+               return $doc;
+       });
+
+       warn "# modified ",dump($modified);
+
+       # copy attachments to CouchDB (they never change, so do it just once
+       if ( my @attachment_ids = $rt->get_attachment_ids( id => $nr ) ) {
+
+               warn "# get_attachment_ids = ",dump( @attachment_ids );
+               my $doc = $db->get("rt/$nr");
+               my @attachments;
+
+               foreach my $attachment_id ( @attachment_ids ) {
+                       my $attachment = $rt->get_attachment( parent_id => $nr, id => $attachment_id );
+                       if ( $attachment->{Filename} && $attachment->{ContentEncoding} eq 'base64' ) {
+                               #$attachment->{Filename} ||= $attachment_id;
+                               my $content = delete $attachment->{Content};
+                               if ( ! exists $doc->{_attachments}->{ $attachment->{Filename} } ) {
+                                       utf8::encode($content) || warn "utf8::encode error!";
+                                       warn "# extracted ",length( $content ), " bytes";
+                                       warn "## attachment ",dump( $attachment );
+                                       my $url = sprintf 'rt/%d/%s?rev=%s', $nr, uri_escape($attachment->{Filename}), $modified->{rev};
+#                                      $modified = $db->request( PUT => $url, $content, $attachment->{ContentType} );
+                               }
+                       }
+                       push @attachments, $attachment;
+               }
+
+
+               $db->modify( "rt/$nr" => sub {
+                       my $doc = shift;
+                       $doc->{attachments} = [ @attachments ];
+                       warn "## attachments on $nr = ", $#attachments + 1;
+                       return $doc;
+               }) if @attachments;
+       
+       }
+
        if ( $ticket->{Queue} !~ m/ILL/i ) {
                warn "SKIP $ticket not in ILL queue!";
                next;
@@ -170,11 +225,9 @@ foreach my $nr ( keys %$ticket_items ) {
 
 #              $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) );
 
-               last; # FIXME just first
-
        }
 
 }
 
 __DATA__
-{"_id":"_design/zotero","language":"javascript","views":{"itemType":{"map":"function(doc) {\n  emit(doc.zapi.itemType,1);\n}","reduce":"_count"},"updated":{"map":"function(doc) {\n  emit(doc.updated,1);\n}","reduce":"_count"},"tags":{"map":"function(doc) {\n  \n  doc.content.tags.forEach( function(v) {\n    emit(v, doc._id);\n  });\n}","reduce":"_count"},"link_up":{"map":"function(doc) {\n  if ( doc.link[1].rel == 'up' )\n  emit( doc.link[1].key, doc._id );\n}","reduce":"_count"},"year,publisher":{"map":"function(doc) {\n  if ( doc.zapi.year )\n  emit([doc.zapi.year, doc.content.publisher], 1);\n}","reduce":"_count"}}}
+{"_id":"_design/zotero","views":{"link_up":{"map":"function(doc) {\n  if ( doc.link[1].rel == 'up' )\n  emit( doc.link[1].key, doc._id );\n}","reduce":"_count"},"year,publisher":{"map":"function(doc) {\n  if ( doc.zapi.year )\n  emit([doc.zapi.year, doc.content.publisher], 1);\n}","reduce":"_count"},"updated":{"map":"function(doc) {\n  emit(doc.updated,1);\n}","reduce":"_count"},"itemType":{"map":"function(doc) {\n  emit(doc.zapi.itemType,1);\n}","reduce":"_count"},"tags":{"map":"function(doc) {\n  \n  doc.content.tags.forEach( function(v) {\n    emit(v, doc._id);\n  });\n}","reduce":"_count"},"collection_items":{"map":"function(doc) {\n  if ( doc.x_meta ) {\n    doc.x_meta.collection_items.forEach( function(id) {\n      emit(doc.content.name, id);\n    });\n  }\n}","reduce":"_count"}},"language":"javascript"}