my $FETCH = $ENV{FETCH} || 0;
-my $db = CouchDB->new('10.60.0.95', 5984);
-eval { $db->put("z_$UserID") }; # create user database
+my $db = CouchDB->new('10.60.0.92', 5984);
+eval { $db->put("zotero_$UserID") }; # create user database
+eval {
+ local $/ = undef;
+ my $view = <DATA>;
+ warn "# create $view";
+ $db->put("zotero_$UserID/_design/zotero" => decode_json $view)
+};
-my $url = "https://api.zotero.org/users/$UserID/items?format=atom&content=json&order=dateModified&sort=desc";
+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
+
+my $url = shift @urls;
my $tree;
my $ticket_items;
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);
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'};
+ if ( exists $item->{content} ) {
+ my $type = ( grep { exists $item->{content}->{$_} } qw(zapi:type type) )[0];
+ warn "# content has $type";
- 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;
+ $item->{zapi}->{etag} = $item->{content}->{'zapi:etag'} if exists $item->{content}->{'zapi:etag'};
- foreach my $tag ( @{ $json->{tags} } ) {
- $tag = $tag->{tag};
- warn "# tag $id $tag\n";
- next unless $tag =~ m/#(\d+)/;
- push @{ $ticket_items->{$1} }, $id;
- }
- }
+ $type = $item->{zapi}->{type} = $item->{content}->{$type};
- $items->{$id} = $item;
+ if ( $type =~ m/json/ ) {
- my $json_md5 = md5_hex encode_json $item;
- $item->{json_md5} = $json_md5;
+ my $json = $item->{content}->{content};
+ warn "# $json\n";
+ $json = $item->{content} = decode_json $json;
+ warn "# json $id ", dump $json;
- if ( my $old_item = eval { $db->get( "z_$UserID/$id" ) } ) {
- warn "# old_item ",dump($old_item);
+ 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;
+ }
- if ( $old_item->{zapi_etag} ne $item->{zapi_etag} || $json_md5 ne $old_item->{json_md5} ) {
- $item->{_rev} = $old_item->{_rev};
- $db->put( "z_$UserID/$id" => $item );
} else {
- warn "# unchanged";
+ warn "ERROR: $type not decoded!";
}
- } else {
- $db->put( "z_$UserID/$id" => $item );
}
+
+ foreach my $zapi ( grep { m/^zapi:/ } keys %$item ) {
+ my $name = $zapi;
+ $name =~ s/^zapi://;
+ $item->{zapi}->{$name} = delete $item->{$zapi};
+ }
+
+ $items->{$id} = $item;
+
+ $db->update( "zotero_$UserID/$id" => $item );
+
}
delete $feed->{entry};
goto restart;
}
+if ( $url = shift @urls ) {
+ warn "## next url $url";
+ goto restart;
+}
+
warn "# tree ",dump( $tree );
warn "# ticket_items ",dump( $ticket_items );
my $ticket = $rt->show(type => 'ticket', id => $nr);
warn "# ticket $nr ",dump($ticket);
+ $db->update( "rt/$nr" => $ticket );
+
if ( $ticket->{Queue} !~ m/ILL/i ) {
warn "SKIP $ticket not in ILL queue!";
next;
}
}
+
+__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"}}}