Monday, April 27, 2009

Journal club cgi

For our journal club, I wrote a small cgi script that provides a web page where people can dump arxiv.org identifies of papers they are interested in and then everybody can see title, abstract and authours as well as a link to the pdf.

You can copy the file to your cgi-bin directory, make it world executable and create a world writeable directory /opt/journal . You might need to install the XML::TreeBuilder module (as root run 'perl -MCPAN -e shell' and then do 'install XML::TreeBuilder'.


#!/usr/bin/perl
#
# Journal club cgi
# Written by Robert C. Helling (helling@atdotde.de)
# Published under the Gnu Public License (most recent version)
#

use XML::TreeBuilder;
use LWP::Simple;
use CGI;

my $g = CGI::new();
print "Content-type: text/html\n\n";
my @ids = ();

system "touch /opt/journal/numbers";
open (IN,'/opt/journal/numbers') ||die "Cannot open /opt/journal/numbers:$!!";;
while(){
chomp;
if(/(\d\d\d\d\.\d\d\d\d)/){
push @ids, $1;
}
elsif(/^\-\-\-/){
@ids = ();
}
}
close IN;

if($new_paper = $g->param('paper_id')){
my ($new_id) = $new_paper =~ /(\d\d\d\d\.\d\d\d\d)/;
push @ids,$new_id;
open (OUT, ">>/opt/journal/numbers") || die "Cannot write to /opt/journal/numbers:$!";
print OUT "$new_id\n";
print "\nNEW$new_id\n";
close OUT;
}

my %seen = ();
@ids = grep { ! $seen{$_} ++ } @ids;

dbmopen %papers, '/opt/journal/papers', 0666;

print join '
<hr>', map {&show($_)} @ids;

print ($g->start_form(),
$g->textfield('paper_id'),
$g->submit(-name => "add paper"),
$g->end_form());

sub show{
my $id = shift;
my $data;

unless($data = $papers{$id}){
$data = get("http://export.arxiv.org/oai2?verb=GetRecord\&identifier=oai:arXiv.org:$id\&metadataPrefix=arXivRaw");
$papers{$id} = $data;
}

my $all = XML::TreeBuilder->new;
$all->parse($data);

my $authors = $all->find('authors')->as_text;
my $title = $all->find('title')->as_text;
my $abstract = $all->find('abstract')->as_text;

return "$authors\n<a href=\"http://arxiv.org/pdf/$id\">$title</a>\n$abstract\n";
}

dbmclose %papers;