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