replicate collections and items
[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
11 use CouchDB;
12 use Digest::MD5 qw(md5_hex);
13
14 my $UserID = $ENV{UserID} || die "usage: UserID=1234 key=abcd $0";
15 my $key    = $ENV{key}    || die "key required";
16
17 my $FETCH  = $ENV{FETCH}  || 0;
18
19 my $db = CouchDB->new('10.60.0.92', 5984);
20 eval { $db->put("zotero_$UserID") }; # create user database
21
22 my @urls = map { "https://api.zotero.org/users/$UserID/$_?format=atom&content=json&order=dateModified&sort=desc" } qw( collections items );
23 # we don't need to fetch tags since we can generate using CouchDB views
24
25 my $url = shift @urls;
26
27 my $tree;
28 my $ticket_items;
29 my $items;
30
31 restart:
32
33 $url .= '&key=' . $key;
34
35 my $file = $UserID . '.' . md5_hex($url) . '.atom';
36 $FETCH = 1 if ! -e $file;
37
38 warn "# $url -> $file\n";
39 if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) {
40         warn "not modified";
41 #       exit 0;
42 }
43
44 my $feed = XMLin( $file );
45 #warn "# feed ",dump($feed);
46
47 sub link_to_id {
48         my $link = shift;
49         $link =~ s{.+/(items|collections)/}{}; # leave just ID
50         $link =~ s{\?.+}{};
51         return $link;
52 }
53
54 foreach my $entry ( keys %{ $feed->{entry} } ) {
55         warn "# entry $entry ",dump($entry);
56         my $id = link_to_id $entry;
57
58         my $item = $feed->{entry}->{$entry};
59         warn "# entry $entry ",dump($item);
60
61         foreach my $i ( 0 .. $#{ $item->{link} } ) {
62                 my $link = $item->{link}->[$i];
63                 warn "# link $id $i:",dump($link);
64
65                 $item->{link}->[$i]->{key} = link_to_id $link->{href};
66
67                 if ( $link->{rel} eq 'up' ) {
68                         push @{ $tree->{$key} }, $id;
69                 }
70         }
71
72         if ( exists $item->{content} ) {
73                 my $type = ( grep { exists $item->{content}->{$_} } qw(zapi:type type) )[0];
74                 warn "# content has $type";
75
76                 $item->{zapi}->{etag} = $item->{content}->{'zapi:etag'} if exists $item->{content}->{'zapi:etag'};
77
78                 $type = $item->{zapi}->{type} = $item->{content}->{$type};
79
80                 if ( $type =~ m/json/ ) {
81
82                         my $json = $item->{content}->{content};
83                         warn "# $json\n";
84                         $json = $item->{content} = decode_json $json;
85                         warn "# json $id ", dump $json;
86
87                         foreach my $tag ( @{ $json->{tags} } ) {
88                                 $tag = $tag->{tag};
89                                 warn "# tag $id $tag\n";
90                                 next unless $tag =~ m/#(\d+)/; # XXX RT number in tag
91                                 push @{ $ticket_items->{$1} }, $id;
92                         }
93
94                 } else {
95                         warn "ERROR: $type not decoded!";
96                 }
97         }
98
99         foreach my $zapi ( grep { m/^zapi:/ } keys %$item ) {
100                 my $name = $zapi;
101                 $name =~ s/^zapi://;
102                 $item->{zapi}->{$name} = delete $item->{$zapi};
103         }
104
105         $items->{$id} = $item;
106
107         my $json_md5 = md5_hex encode_json $item;
108         $item->{zapi}->{json_md5} = $json_md5;
109
110         if ( my $old_item = eval { $db->get( "zotero_$UserID/$id" ) } ) {
111                 warn "# old_item ", $old_item->{_rev}; #dump($old_item);
112
113                 if ( $old_item->{zapi}->{etag} ne $item->{zapi}->{etag} || $json_md5 ne $old_item->{zapi}->{json_md5} ) {
114                         $item->{_rev} = $old_item->{_rev};
115                         warn :"# update $id";
116                         $db->put( "zotero_$UserID/$id" => $item );
117                 } else {
118                         warn "# unchanged";
119                 }
120         } else {
121                 $db->put( "zotero_$UserID/$id" => $item );
122                 warn "# insert $id ", dump($item);
123         }
124 }
125
126 delete $feed->{entry};
127 warn "# feed without entry ",dump( $feed );
128
129 if ( my @next = map { $_->{href} } grep { $_->{rel} eq 'next' && $_->{type} eq 'application/atom+xml' } @{ $feed->{link} } ) {
130         warn "## next ",dump(@next);
131         $url = $next[0];
132         goto restart;
133 }
134
135 if ( $url = shift @urls ) {
136         warn "## next url $url";
137         goto restart;
138 }
139
140 warn "# tree ",dump( $tree );
141
142 warn "# ticket_items ",dump( $ticket_items );
143
144
145 my $rt = RT::Client::REST->new(
146         server => 'http://rt.rot13.org/rt',
147         timeout => 30,
148 );
149
150 $rt->login(username => $ENV{RT_USER}, password => $ENV{RT_PASSWORD});
151
152 foreach my $nr ( keys %$ticket_items ) {
153
154         my $ticket = $rt->show(type => 'ticket', id => $nr);
155         warn "# ticket $nr ",dump($ticket);
156
157         if ( $ticket->{Queue} !~ m/ILL/i ) {
158                 warn "SKIP $ticket not in ILL queue!";
159                 next;
160         }
161
162         foreach my $id ( @{ $ticket_items->{$nr} } ) {
163                 warn "# item $id ",dump( $items->{$id} );
164
165 #               $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) );
166
167                 last; # FIXME just first
168
169         }
170
171 }