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