8 use Data::Dump qw(dump);
12 use Digest::MD5 qw(md5_hex);
14 my $UserID = $ENV{UserID} || die "usage: UserID=1234 key=abcd $0";
15 my $key = $ENV{key} || die "key required";
17 my $FETCH = $ENV{FETCH} || 0;
19 my $db = CouchDB->new('10.60.0.92', 5984);
20 eval { $db->put("zotero_$UserID") }; # create user database
24 warn "# create $view";
25 $db->put("zotero_$UserID/_design/zotero" => decode_json $view)
28 my @urls = map { "https://api.zotero.org/users/$UserID/$_?format=atom&content=json&order=dateModified&sort=desc" } qw( collections items );
29 # we don't need to fetch tags since we can generate using CouchDB views
31 my $url = shift @urls;
39 $url .= '&key=' . $key;
41 my $file = $UserID . '.' . md5_hex($url) . '.atom';
42 $FETCH = 1 if ! -e $file;
44 warn "# $url -> $file\n";
45 if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) {
50 my $feed = XMLin( $file );
51 #warn "# feed ",dump($feed);
55 $link =~ s{.+/(items|collections)/}{}; # leave just ID
60 foreach my $entry ( keys %{ $feed->{entry} } ) {
61 warn "# entry $entry ",dump($entry);
62 my $id = link_to_id $entry;
64 my $item = $feed->{entry}->{$entry};
65 warn "# entry $entry ",dump($item);
67 foreach my $i ( 0 .. $#{ $item->{link} } ) {
68 my $link = $item->{link}->[$i];
69 warn "# link $id $i:",dump($link);
71 $item->{link}->[$i]->{key} = link_to_id $link->{href};
73 if ( $link->{rel} eq 'up' ) {
74 push @{ $tree->{$key} }, $id;
78 if ( exists $item->{content} ) {
79 my $type = ( grep { exists $item->{content}->{$_} } qw(zapi:type type) )[0];
80 warn "# content has $type";
82 $item->{zapi}->{etag} = $item->{content}->{'zapi:etag'} if exists $item->{content}->{'zapi:etag'};
84 $type = $item->{zapi}->{type} = $item->{content}->{$type};
86 if ( $type =~ m/json/ ) {
88 my $json = $item->{content}->{content};
90 $json = $item->{content} = decode_json $json;
91 warn "# json $id ", dump $json;
93 foreach my $tag ( @{ $json->{tags} } ) {
95 warn "# tag $id $tag\n";
96 next unless $tag =~ m/#(\d+)/; # XXX RT number in tag
97 push @{ $ticket_items->{$1} }, $id;
101 warn "ERROR: $type not decoded!";
105 foreach my $zapi ( grep { m/^zapi:/ } keys %$item ) {
108 $item->{zapi}->{$name} = delete $item->{$zapi};
111 $items->{$id} = $item;
113 my $json_md5 = md5_hex encode_json $item;
114 $item->{zapi}->{json_md5} = $json_md5;
116 if ( my $old_item = eval { $db->get( "zotero_$UserID/$id" ) } ) {
117 warn "# old_item ", $old_item->{_rev}; #dump($old_item);
119 if ( $old_item->{zapi}->{etag} ne $item->{zapi}->{etag} || $json_md5 ne $old_item->{zapi}->{json_md5} ) {
120 $item->{_rev} = $old_item->{_rev};
121 warn :"# update $id";
122 $db->put( "zotero_$UserID/$id" => $item );
127 $db->put( "zotero_$UserID/$id" => $item );
128 warn "# insert $id ", dump($item);
132 delete $feed->{entry};
133 warn "# feed without entry ",dump( $feed );
135 if ( my @next = map { $_->{href} } grep { $_->{rel} eq 'next' && $_->{type} eq 'application/atom+xml' } @{ $feed->{link} } ) {
136 warn "## next ",dump(@next);
141 if ( $url = shift @urls ) {
142 warn "## next url $url";
146 warn "# tree ",dump( $tree );
148 warn "# ticket_items ",dump( $ticket_items );
151 my $rt = RT::Client::REST->new(
152 server => 'http://rt.rot13.org/rt',
156 $rt->login(username => $ENV{RT_USER}, password => $ENV{RT_PASSWORD});
158 foreach my $nr ( keys %$ticket_items ) {
160 my $ticket = $rt->show(type => 'ticket', id => $nr);
161 warn "# ticket $nr ",dump($ticket);
163 if ( $ticket->{Queue} !~ m/ILL/i ) {
164 warn "SKIP $ticket not in ILL queue!";
168 foreach my $id ( @{ $ticket_items->{$nr} } ) {
169 warn "# item $id ",dump( $items->{$id} );
171 # $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) );
173 last; # FIXME just first
180 {"_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"}}}