replicate collections and items
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 27 Jul 2012 23:43:46 +0000 (01:43 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 27 Jul 2012 23:43:52 +0000 (01:43 +0200)
We don't need tags, this can be generated using CouchDB views.
However, I still don't have idea how to link items and collections...

zotero.pl

index 23cdd9a..1364606 100755 (executable)
--- a/zotero.pl
+++ b/zotero.pl
@@ -19,7 +19,10 @@ my $FETCH  = $ENV{FETCH}  || 0;
 my $db = CouchDB->new('10.60.0.92', 5984);
 eval { $db->put("zotero_$UserID") }; # create user database
 
-my $url = "https://api.zotero.org/users/$UserID/items?format=atom&content=json&order=dateModified&sort=desc";
+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
+
+my $url = shift @urls;
 
 my $tree;
 my $ticket_items;
@@ -41,10 +44,16 @@ if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) {
 my $feed = XMLin( $file );
 #warn "# feed ",dump($feed);
 
+sub link_to_id {
+       my $link = shift;
+       $link =~ s{.+/(items|collections)/}{}; # leave just ID
+       $link =~ s{\?.+}{};
+       return $link;
+}
+
 foreach my $entry ( keys %{ $feed->{entry} } ) {
        warn "# entry $entry ",dump($entry);
-       my $id = $entry;
-       $id =~ s{.+/items/}{}; # leave just ID
+       my $id = link_to_id $entry;
 
        my $item = $feed->{entry}->{$entry};
        warn "# entry $entry ",dump($item);
@@ -53,30 +62,37 @@ foreach my $entry ( keys %{ $feed->{entry} } ) {
                my $link = $item->{link}->[$i];
                warn "# link $id $i:",dump($link);
 
-               my $key = $link->{href};
-               $key =~ s{.+/items/}{};
-               $key =~ s{\?.+}{};
-               $item->{link}->[$i]->{key} = $key;
+               $item->{link}->[$i]->{key} = link_to_id $link->{href};
 
                if ( $link->{rel} eq 'up' ) {
                        push @{ $tree->{$key} }, $id;
                }
        }
 
-       $item->{zapi}->{etag} = $item->{content}->{'zapi:etag'};
-       $item->{zapi}->{type} = $item->{content}->{'zapi:type'};
+       if ( exists $item->{content} ) {
+               my $type = ( grep { exists $item->{content}->{$_} } qw(zapi:type type) )[0];
+               warn "# content has $type";
+
+               $item->{zapi}->{etag} = $item->{content}->{'zapi:etag'} if exists $item->{content}->{'zapi:etag'};
+
+               $type = $item->{zapi}->{type} = $item->{content}->{$type};
+
+               if ( $type =~ m/json/ ) {
 
-       if ( $item->{content}->{'zapi:type'} eq 'json' ) {
-               my $json = $item->{content}->{content};
-               warn "# $json\n";
-               $json = $item->{content} = decode_json $json;
-               warn "# json $id ", dump $json;
+                       my $json = $item->{content}->{content};
+                       warn "# $json\n";
+                       $json = $item->{content} = decode_json $json;
+                       warn "# json $id ", dump $json;
 
-               foreach my $tag ( @{ $json->{tags} } ) {
-                       $tag = $tag->{tag};
-                       warn "# tag $id $tag\n";
-                       next unless $tag =~ m/#(\d+)/;
-                       push @{ $ticket_items->{$1} }, $id;
+                       foreach my $tag ( @{ $json->{tags} } ) {
+                               $tag = $tag->{tag};
+                               warn "# tag $id $tag\n";
+                               next unless $tag =~ m/#(\d+)/; # XXX RT number in tag
+                               push @{ $ticket_items->{$1} }, $id;
+                       }
+
+               } else {
+                       warn "ERROR: $type not decoded!";
                }
        }
 
@@ -89,19 +105,21 @@ foreach my $entry ( keys %{ $feed->{entry} } ) {
        $items->{$id} = $item;
 
        my $json_md5 = md5_hex encode_json $item;
-       $item->{json_md5} = $json_md5;
+       $item->{zapi}->{json_md5} = $json_md5;
 
        if ( my $old_item = eval { $db->get( "zotero_$UserID/$id" ) } ) {
-               warn "# old_item ",dump($old_item);
+               warn "# old_item ", $old_item->{_rev}; #dump($old_item);
 
-               if ( $old_item->{zapi}->{etag} ne $item->{zapi}->{etag} || $json_md5 ne $old_item->{json_md5} ) {
+               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);
        }
 }
 
@@ -114,6 +132,11 @@ if ( my @next = map { $_->{href} } grep { $_->{rel} eq 'next' && $_->{type} eq '
        goto restart;
 }
 
+if ( $url = shift @urls ) {
+       warn "## next url $url";
+       goto restart;
+}
+
 warn "# tree ",dump( $tree );
 
 warn "# ticket_items ",dump( $ticket_items );