X-Git-Url: http://git.rot13.org/?p=ILL-Zotero-RT;a=blobdiff_plain;f=zotero.pl;h=2dda7841ea0fbbfdd8b325882cd8501d20728ea1;hp=bc240f9c16f07cf3e050bb7861f967217043db8d;hb=HEAD;hpb=3b589c57c8b5dab4d1967e2407fb39914978231d diff --git a/zotero.pl b/zotero.pl index bc240f9..2dda784 100755 --- a/zotero.pl +++ b/zotero.pl @@ -44,14 +44,18 @@ $url .= '&key=' . $key; my $file = $UserID . '.' . md5_hex($url) . '.atom'; $FETCH = 1 if ! -e $file; -warn "# $url -> $file\n"; +warn "# mirror $FETCH $url -> $file\n"; if ( $FETCH && mirror( $url => $file ) == RC_NOT_MODIFIED ) { warn "not modified"; -# exit 0; } -my $feed = XMLin( $file ); -#warn "# feed ",dump($feed); +my $xml = XML::Simple->new(ForceArray => [ qw( entry ) ]); +my $feed = eval { $xml->XMLin( $file ) }; +if ( $! ) { + warn "ERROR $file $!\n"; + goto skip_url; +} +warn "# feed ",dump($feed); sub link_to_id { my $link = shift; @@ -60,12 +64,16 @@ sub link_to_id { return $link; } +my @collection_items; + foreach my $entry ( keys %{ $feed->{entry} } ) { warn "# entry $entry ",dump($entry); my $id = link_to_id $entry; + push @collection_items, $id if $url =~ m{/collections/(\w+)/items}; + my $item = $feed->{entry}->{$entry}; - warn "# entry $entry ",dump($item); + warn "# item $id $entry ",dump($item),$/; foreach my $i ( 0 .. $#{ $item->{link} } ) { my $link = $item->{link}->[$i]; @@ -75,6 +83,9 @@ foreach my $entry ( keys %{ $feed->{entry} } ) { if ( $link->{rel} eq 'up' ) { push @{ $tree->{$key} }, $id; + } elsif ( $link->{rel} eq 'self' && $link->{href} =~ m{/collections/} ) { + warn "# get items in this collection"; + push @urls, "$link->{href}/items?content=json"; } } @@ -117,6 +128,15 @@ foreach my $entry ( keys %{ $feed->{entry} } ) { } +if ( @collection_items ) { + my $id = $1 if $url =~ m{/collections/(\w+)/items}; + $db->modify( "zotero_$UserID/$id" => sub { + my $doc = shift; + $doc->{x_meta}->{collection_items} = [ @collection_items ]; + return $doc; + }); +} + delete $feed->{entry}; warn "# feed without entry ",dump( $feed ); @@ -126,6 +146,8 @@ if ( my @next = map { $_->{href} } grep { $_->{rel} eq 'next' && $_->{type} eq ' goto restart; } +skip_url: + if ( $url = shift @urls ) { warn "## next url $url"; goto restart; @@ -208,4 +230,4 @@ foreach my $nr ( keys %$ticket_items ) { } __DATA__ -{"_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"}}} +{"_id":"_design/zotero","views":{"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"},"updated":{"map":"function(doc) {\n emit(doc.updated,1);\n}","reduce":"_count"},"itemType":{"map":"function(doc) {\n emit(doc.zapi.itemType,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"},"collection_items":{"map":"function(doc) {\n if ( doc.x_meta ) {\n doc.x_meta.collection_items.forEach( function(id) {\n emit(doc.content.name, id);\n });\n }\n}","reduce":"_count"}},"language":"javascript"}