make ticket copy in CouchDB
[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 "# $url -> $file\n";
48 if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) {
49         warn "not modified";
50 #       exit 0;
51 }
52
53 my $feed = XMLin( $file );
54 #warn "# feed ",dump($feed);
55
56 sub link_to_id {
57         my $link = shift;
58         $link =~ s{.+/(items|collections)/}{}; # leave just ID
59         $link =~ s{\?.+}{};
60         return $link;
61 }
62
63 foreach my $entry ( keys %{ $feed->{entry} } ) {
64         warn "# entry $entry ",dump($entry);
65         my $id = link_to_id $entry;
66
67         my $item = $feed->{entry}->{$entry};
68         warn "# entry $entry ",dump($item);
69
70         foreach my $i ( 0 .. $#{ $item->{link} } ) {
71                 my $link = $item->{link}->[$i];
72                 warn "# link $id $i:",dump($link);
73
74                 $item->{link}->[$i]->{key} = link_to_id $link->{href};
75
76                 if ( $link->{rel} eq 'up' ) {
77                         push @{ $tree->{$key} }, $id;
78                 }
79         }
80
81         if ( exists $item->{content} ) {
82                 my $type = ( grep { exists $item->{content}->{$_} } qw(zapi:type type) )[0];
83                 warn "# content has $type";
84
85                 $item->{zapi}->{etag} = $item->{content}->{'zapi:etag'} if exists $item->{content}->{'zapi:etag'};
86
87                 $type = $item->{zapi}->{type} = $item->{content}->{$type};
88
89                 if ( $type =~ m/json/ ) {
90
91                         my $json = $item->{content}->{content};
92                         warn "# $json\n";
93                         $json = $item->{content} = decode_json $json;
94                         warn "# json $id ", dump $json;
95
96                         foreach my $tag ( @{ $json->{tags} } ) {
97                                 $tag = $tag->{tag};
98                                 warn "# tag $id $tag\n";
99                                 next unless $tag =~ m/#(\d+)/; # XXX RT number in tag
100                                 push @{ $ticket_items->{$1} }, $id;
101                         }
102
103                 } else {
104                         warn "ERROR: $type not decoded!";
105                 }
106         }
107
108         foreach my $zapi ( grep { m/^zapi:/ } keys %$item ) {
109                 my $name = $zapi;
110                 $name =~ s/^zapi://;
111                 $item->{zapi}->{$name} = delete $item->{$zapi};
112         }
113
114         $items->{$id} = $item;
115
116         $db->modify( "zotero_$UserID/$id" => $item );
117
118 }
119
120 delete $feed->{entry};
121 warn "# feed without entry ",dump( $feed );
122
123 if ( my @next = map { $_->{href} } grep { $_->{rel} eq 'next' && $_->{type} eq 'application/atom+xml' } @{ $feed->{link} } ) {
124         warn "## next ",dump(@next);
125         $url = $next[0];
126         goto restart;
127 }
128
129 if ( $url = shift @urls ) {
130         warn "## next url $url";
131         goto restart;
132 }
133
134 warn "# tree ",dump( $tree );
135
136 warn "# ticket_items ",dump( $ticket_items );
137
138
139 my $rt = RT::Client::REST->new(
140         server => 'http://rt.rot13.org/rt',
141         timeout => 30,
142 );
143
144 $rt->login(username => $ENV{RT_USER}, password => $ENV{RT_PASSWORD});
145
146 foreach my $nr ( keys %$ticket_items ) {
147
148         my $ticket = eval { $rt->show(type => 'ticket', id => $nr) };
149         warn "# ticket $nr ",dump($ticket);
150
151         next unless $ticket;
152
153         $ticket->{zotero_items} = $ticket_items->{$nr};
154
155         my $modified = $db->modify( "rt/$nr" => sub {
156                 my $doc = shift;
157                 $doc->{$_} = $ticket->{$_} foreach keys %$ticket;
158                 return $doc;
159         });
160
161         warn "# modified ",dump($modified);
162
163         # copy attachments to CouchDB (they never change, so do it just once
164         if ( my @attachment_ids = $rt->get_attachment_ids( id => $nr ) ) {
165
166                 warn "# get_attachment_ids = ",dump( @attachment_ids );
167                 my $doc = $db->get("rt/$nr");
168                 my @attachments;
169
170                 foreach my $attachment_id ( @attachment_ids ) {
171                         my $attachment = $rt->get_attachment( parent_id => $nr, id => $attachment_id );
172                         if ( $attachment->{Filename} && $attachment->{ContentEncoding} eq 'base64' ) {
173                                 #$attachment->{Filename} ||= $attachment_id;
174                                 my $content = delete $attachment->{Content};
175                                 if ( ! exists $doc->{_attachments}->{ $attachment->{Filename} } ) {
176                                         utf8::encode($content) || warn "utf8::encode error!";
177                                         warn "# extracted ",length( $content ), " bytes";
178                                         warn "## attachment ",dump( $attachment );
179                                         my $url = sprintf 'rt/%d/%s?rev=%s', $nr, uri_escape($attachment->{Filename}), $modified->{rev};
180 #                                       $modified = $db->request( PUT => $url, $content, $attachment->{ContentType} );
181                                 }
182                         }
183                         push @attachments, $attachment;
184                 }
185
186
187                 $db->modify( "rt/$nr" => sub {
188                         my $doc = shift;
189                         $doc->{attachments} = [ @attachments ];
190                         warn "## attachments on $nr = ", $#attachments + 1;
191                         return $doc;
192                 }) if @attachments;
193         
194         }
195
196         if ( $ticket->{Queue} !~ m/ILL/i ) {
197                 warn "SKIP $ticket not in ILL queue!";
198                 next;
199         }
200
201         foreach my $id ( @{ $ticket_items->{$nr} } ) {
202                 warn "# item $id ",dump( $items->{$id} );
203
204 #               $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) );
205
206         }
207
208 }
209
210 __DATA__
211 {"_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"}}}