From 71ffa85cdd50cf657eee7d2b4ab1c60f65c70283 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 26 Jul 2012 18:39:25 +0200 Subject: [PATCH] store items in CouchDB --- CouchDB.pm | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ zotero.pl | 19 +++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 CouchDB.pm diff --git a/CouchDB.pm b/CouchDB.pm new file mode 100644 index 0000000..8b13b36 --- /dev/null +++ b/CouchDB.pm @@ -0,0 +1,77 @@ +package CouchDB; + +use strict; +use warnings; + +use LWP::UserAgent; +use JSON; + +sub new { + my ($class, $host, $port, $options) = @_; + + my $ua = LWP::UserAgent->new; + $ua->timeout(10); + $ua->env_proxy; + + return bless { + ua => $ua, + host => $host, + port => $port, + base_uri => "http://$host:$port/", + }, $class; +} + +sub ua { shift->{ua} } +sub base { shift->{base_uri} } + +sub request { + my ($self, $method, $uri, $content) = @_; + + my $full_uri = $self->base . $uri; + my $req; + + if (defined $content) { + #Content-Type: application/json + + $req = HTTP::Request->new( $method, $full_uri, undef, encode_json $content ); + $req->header('Content-Type' => 'application/json'); + } else { + $req = HTTP::Request->new( $method, $full_uri ); + } + + my $response = $self->ua->request($req); + + if ($response->is_success) { + return decode_json $response->content; + } else { + die($response->status_line . ":" . $response->content); + } +} + +sub delete { + my ($self, $url) = @_; + + $self->request(DELETE => $url); +} + +sub get { + my ($self, $url) = @_; + + warn "## GET $url"; + $self->request(GET => $url); +} + +sub put { + my ($self, $url, $json) = @_; + + warn "## PUT $url"; + $self->request(PUT => $url, $json); +} + +sub post { + my ($self, $url, $json) = @_; + + $self->request(POST => $url, $json); +} + +1; diff --git a/zotero.pl b/zotero.pl index 2546c34..7f99401 100755 --- a/zotero.pl +++ b/zotero.pl @@ -8,11 +8,17 @@ use JSON; use Data::Dump qw(dump); use RT::Client::REST; +use CouchDB; +use Digest::MD5 qw(md5_hex); + my $UserID = $ENV{UserID} || die "usage: UserID=1234 key=abcd $0"; my $key = $ENV{key} || die "key required"; my $FETCH = $ENV{FETCH} || 0; +my $db = CouchDB->new('10.60.0.95', 5984); +eval { $db->put("z_$UserID") }; # create user database + my $url = "https://api.zotero.org/users/$UserID/items?key=$key&format=atom&content=json&order=dateModified&sort=desc"; my $file = "$UserID.atom"; @@ -37,6 +43,7 @@ foreach my $entry ( keys %{ $feed->{entry} } ) { $id =~ s{.+/items/}{}; # leave just ID my $item = $feed->{entry}->{$entry}; + warn "# entry $entry ",dump($item); foreach my $link ( @{ $item->{link} } ) { warn "# link $id ",dump($link); @@ -48,6 +55,8 @@ foreach my $entry ( keys %{ $feed->{entry} } ) { } } + $item->{zapi_etag} = $item->{content}->{'zapi:etag'}; + if ( $item->{content}->{'zapi:type'} eq 'json' ) { my $json = $item->{content}->{content}; warn "# $json\n"; @@ -64,6 +73,14 @@ foreach my $entry ( keys %{ $feed->{entry} } ) { $items->{$id} = $item; + my $old_item = $db->get( "z_$UserID/$id" ); + warn "# old_item ",dump($old_item); + my $item_md5 = md5_hex encode_json $item; + $item->{item_md5} = $item_md5; + if ( $old_item->{zapi_etag} ne $item->{zapi_etag} || $item_md5 ne $old_item->{item_md5} ) { + $item->{_rev} = $old_item->{_rev}; + $db->put( "z_$UserID/$id" => $item ); + } } warn "# tree ",dump( $tree ); @@ -91,7 +108,7 @@ foreach my $nr ( keys %$ticket_items ) { foreach my $id ( @{ $ticket_items->{$nr} } ) { warn "# item $id ",dump( $items->{$id} ); - $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) ); +# $rt->comment( ticket_id => $nr, message => dump( $items->{$id} ) ); last; # FIXME just first -- 2.20.1