collection_items view
[ILL-Zotero-RT] / zotero.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use LWP::Simple;
6 use XML::Simple;
7 use JSON;
8 use Data::Dump qw(dump);
9 use RT::Client::REST;
10 use URI::Escape;
11
12 use CouchDB;
13 use Digest::MD5 qw(md5_hex);
14
15 my $UserID = $ENV{UserID} || die "usage: UserID=1234 key=abcd $0";
16 my $key    = $ENV{key}    || die "key required";
17
18 my $FETCH  = $ENV{FETCH}  || 0;
19
20 my $db = CouchDB->new('10.60.0.92', 5984);
21 eval { $db->put("zotero_$UserID") }; # create user database
22 eval {
23         local $/ = undef;
24         my $view = <DATA>;
25         warn "# create $view";
26         $db->put("zotero_$UserID/_design/zotero" => decode_json $view)
27 };
28
29 eval { $db->put("rt") }; # create RT database
30
31 my @urls = map { "https://api.zotero.org/users/$UserID/$_?format=atom&content=json&order=dateModified&sort=desc" } qw( collections items );
32 # we don't need to fetch tags since we can generate using CouchDB views
33
34 my $url = shift @urls;
35
36 my $tree;
37 my $ticket_items;
38 my $items;
39
40 restart:
41
42 $url .= '&key=' . $key;
43
44 my $file = $UserID . '.' . md5_hex($url) . '.atom';
45 $FETCH = 1 if ! -e $file;
46
47 warn "# mirror $FETCH $url -> $file\n";
48 if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) {
49         warn "not modified";
50 }
51
52 my $xml = XML::Simple->new(ForceArray => [ qw( entry ) ]);
53 my $feed = eval { $xml->XMLin( $file ) };
54 if ( $! ) {
55         warn "ERROR $file $!\n";
56         goto skip_url;
57 }
58 warn "# feed ",dump($feed);
59
60 sub link_to_id {
61         my $link = shift;
62         $link =~ s{.+/(items|collections)/}{}; # leave just ID
63         $link =~ s{\?.+}{};
64         return $link;
65 }
66
67 my @collection_items;
68
69 foreach my $entry ( keys %{ $feed->{entry} } ) {
70         warn "# entry $entry ",dump($entry);
71         my $id = link_to_id $entry;
72
73         push @collection_items, $id if $url =~ m{/collections/(\w+)/items};
74
75         my $item = $feed->{entry}->{$entry};
76         warn "# item $id $entry ",dump($item),$/;
77
78         foreach my $i ( 0 .. $#{ $item->{link} } ) {
79                 my $link = $item->{link}->[$i];
80                 warn "# link $id $i:",dump($link);
81
82                 $item->{link}->[$i]->{key} = link_to_id $link->{href};
83
84                 if ( $link->{rel} eq 'up' ) {
85                         push @{ $tree->{$key} }, $id;
86                 } elsif ( $link->{rel} eq 'self' && $link->{href} =~ m{/collections/} ) {
87                         warn "# get items in this collection";
88                         push @urls, "$link->{href}/items?content=json";
89                 }
90         }
91
92         if ( exists $item->{content} ) {
93                 my $type = ( grep { exists $item->{content}->{$_} } qw(zapi:type type) )[0];
94                 warn "# content has $type";
95
96                 $item->{zapi}->{etag} = $item->{content}->{'zapi:etag'} if exists $item->{content}->{'zapi:etag'};
97
98                 $type = $item->{zapi}->{type} = $item->{content}->{$type};
99
100                 if ( $type =~ m/json/ ) {
101
102                         my $json = $item->{content}->{content};
103                         warn "# $json\n";
104                         $json = $item->{content} = decode_json $json;
105                         warn "# json $id ", dump $json;
106
107                         foreach my $tag ( @{ $json->{tags} } ) {
108                                 $tag = $tag->{tag};
109                                 warn "# tag $id $tag\n";
110                                 next unless $tag =~ m/#(\d+)/; # XXX RT number in tag
111                                 push @{ $ticket_items->{$1} }, $id;
112                         }
113
114                 } else {
115                         warn "ERROR: $type not decoded!";
116                 }
117         }
118
119         foreach my $zapi ( grep { m/^zapi:/ } keys %$item ) {
120                 my $name = $zapi;
121                 $name =~ s/^zapi://;
122                 $item->{zapi}->{$name} = delete $item->{$zapi};
123         }
124
125         $items->{$id} = $item;
126
127         $db->modify( "zotero_$UserID/$id" => $item );
128
129 }
130
131 if ( @collection_items ) {
132         my $id = $1 if $url =~ m{/collections/(\w+)/items};
133         $db->modify( "zotero_$UserID/$id" => sub {
134                 my $doc = shift;
135                 $doc->{x_meta}->{collection_items} = [ @collection_items ];
136                 return $doc;
137         });
138 }
139
140 delete $feed->{entry};
141 warn "# feed without entry ",dump( $feed );
142
143 if ( my @next = map { $_->{href} } grep { $_->{rel} eq 'next' && $_->{type} eq 'application/atom+xml' } @{ $feed->{link} } ) {
144         warn "## next ",dump(@next);
145         $url = $next[0];
146         goto restart;
147 }
148
149 skip_url:
150
151 if ( $url = shift @urls ) {
152         warn "## next url $url";
153         goto restart;
154 }
155
156 warn "# tree ",dump( $tree );
157
158 warn "# ticket_items ",dump( $ticket_items );
159
160
161 my $rt = RT::Client::REST->new(
162         server => 'http://rt.rot13.org/rt',
163         timeout => 30,
164 );
165
166 $rt->login(username => $ENV{RT_USER}, password => $ENV{RT_PASSWORD});
167
168 foreach my $nr ( keys %$ticket_items ) {
169
170         my $ticket = eval { $rt->show(type => 'ticket', id => $nr) };
171         warn "# ticket $nr ",dump($ticket);
172
173         next unless $ticket;
174
175         $ticket->{zotero_items} = $ticket_items->{$nr};
176
177         my $modified = $db->modify( "rt/$nr" => sub {
178                 my $doc = shift;
179                 $doc->{$_} = $ticket->{$_} foreach keys %$ticket;
180                 return $doc;
181         });
182
183         warn "# modified ",dump($modified);
184
185         # copy attachments to CouchDB (they never change, so do it just once
186         if ( my @attachment_ids = $rt->get_attachment_ids( id => $nr ) ) {
187
188                 warn "# get_attachment_ids = ",dump( @attachment_ids );
189                 my $doc = $db->get("rt/$nr");
190                 my @attachments;
191
192                 foreach my $attachment_id ( @attachment_ids ) {
193                         my $attachment = $rt->get_attachment( parent_id => $nr, id => $attachment_id );
194                         if ( $attachment->{Filename} && $attachment->{ContentEncoding} eq 'base64' ) {
195                                 #$attachment->{Filename} ||= $attachment_id;
196                                 my $content = delete $attachment->{Content};
197                                 if ( ! exists $doc->{_attachments}->{ $attachment->{Filename} } ) {
198                                         utf8::encode($content) || warn "utf8::encode error!";
199                                         warn "# extracted ",length( $content ), " bytes";
200                                         warn "## attachment ",dump( $attachment );
201                                         my $url = sprintf 'rt/%d/%s?rev=%s', $nr, uri_escape($attachment->{Filename}), $modified->{rev};
202 #                                       $modified = $db->request( PUT => $url, $content, $attachment->{ContentType} );
203                                 }
204                         }
205                         push @attachments, $attachment;
206                 }
207
208
209                 $db->modify( "rt/$nr" => sub {
210                         my $doc = shift;
211                         $doc->{attachments} = [ @attachments ];
212                         warn "## attachments on $nr = ", $#attachments + 1;
213                         return $doc;
214                 }) if @attachments;
215         
216         }
217
218         if ( $ticket->{Queue} !~ m/ILL/i ) {
219                 warn "SKIP $ticket not in ILL queue!";
220                 next;
221         }
222
223         foreach my $id ( @{ $ticket_items->{$nr} } ) {
224                 warn "# item $id ",dump( $items->{$id} );
225
226 #               $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) );
227
228         }
229
230 }
231
232 __DATA__
233 {"_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"}