=head1 NAME
EPrints::Plugin::Import::MSAcad
=cut
package EPrints::Plugin::Import::MSAcad;
use EPrints::Plugin::Import;
@ISA = qw( EPrints::Plugin::Import );
use strict;
sub new
{
my( $class, %opts ) = @_;
my $self = $class->SUPER::new( %opts );
$self->{name} = "Import (Microsoft Academic Search)";
$self->{produce} = [qw( dataobj/eprint dataobj/document )];
$self->{accept} = [qw( )];
$self->{advertise} = 1;
return $self;
}
sub input_fh
{
my( $self, %opts ) = @_;
my $fh = $opts{"fh"};
my $id = join "", <$fh>;
my $dataset = $opts{dataset};
my $dataobj = $self->getPubFromMSAcad($id,$dataset);
my @ids;
push @ids, $dataobj->id if $dataobj;
return EPrints::List->new(
session => $self->{session},
dataset => $opts{dataset},
ids => \@ids
);
}
sub getPubFromMSAcad
{
my ( $self, $id, $dataset ) = @_;
my $repo = $self->{repository};
use HTTP::Request::Common qw(GET);
my $ua = LWP::UserAgent->new;
my $app_id = "d485d5b2-3e14-42d7-b090-a0d3eb8296d4";
my $req = GET 'http://academic.research.microsoft.com/json.svc/search?AppId='.$app_id.'&PublicationID='.$id.'&ResultObjects=Publication&StartIdx=1&EndIdx=2';
my $res = $ua->request($req);
if ($res->is_success)
{
my $content = $res->content;
my $dataobj = $self->getPubsFromJson($content,$dataset);
return $dataobj;
}
else
{
print STDERR "\n\n MSAcad_Locate (query error): " . $res->status_line . "\n\n";
}
return undef;
}
sub getPubsFromJson
{
my ( $self, $content, $dataset ) = @_;
my $repository = $self->{repository};
my $json = JSON->new->allow_nonref;
#my $json_text = $json->allow_nonref->utf8->relaxed->loose->allow_singlequote->allow_barekey->decode($content);
my $json_text = eval { $json->utf8()->decode($content); };
if ($@)
{
print STDERR "SOMETHING WENT WRONG \n\n";
}
my @ret_publications;
my $publications = $json_text->{d}->{Publication}->{Result};
foreach my $publication(@{$publications}) {
my %phash = ();
my $epdata = ();
$epdata->{id_number} = $publication->{DOI};
my $url = "http://academic.research.microsoft.com/Publication/" . $publication->{ID};
$epdata->{related_url}->[0]->{url} = $url;
$epdata->{title} = $publication->{Title};
$epdata->{abstract} = $publication->{Abstract};
$epdata->{date} = $publication->{Year};
foreach my $journal($publication->{Journal})
{
# Need to pass the journal data and then referee it.
if (defined($phash{Journal}))
{
$phash{Journal} .= ", ";
}
$phash{Journal} .= $journal->{FullName};
$epdata->{type} = "article";
$epdata->{publication} = $phash{Journal};
}
my $author_count = 0;
foreach my $author(@{$publication->{Author}})
{
$epdata->{creators}->[$author_count]->{id} = $author->{ID};
$epdata->{creators}->[$author_count]->{name}->{family} = $author->{LastName};
$epdata->{creators}->[$author_count]->{name}->{given} = $author->{FirstName} . " " . $author->{MiddleName};
$epdata->{creators}->[$author_count]->{name}->{given} = $author->{FirstName} . " " . $author->{MiddleName};
$author_count += 1;
}
foreach my $keyword(@{$publication->{Keyword}})
{
if (defined($phash{Keyword}))
{
$phash{Keyword} .= ", ";
}
$phash{Keyword} .= $keyword->{Name};
$epdata->{keywords} = $phash{Keyword};
}
$epdata->{userid} = $repository->current_user->id;
my $dataobj = $self->epdata_to_dataobj( $dataset, $epdata );
if (!defined $dataobj)
{
return undef;
}
my $urls = $publication->{FullVersionURL};
my $success = 0;
my $count = 0;
my $document = $dataobj->create_subdataobj( "documents", {
format => "other",
});
my $continue = 1;
while(!$success and $continue)
{
my $url = $urls->[$count];
$count += 1;
$success = $document->upload_url( $url );
if ($count > length($urls)) {
$continue = 0;
}
}
if ($continue)
{
if( !$document->is_set( "main" ) )
{
# only one file so main must be it
my $files = $document->files;
if( scalar @$files == 1 )
{
$document->set_main( $files->[0] );
}
}
}
$document->commit;
return $dataobj;
}
}
1;
=head1 COPYRIGHT
=for COPYRIGHT BEGIN
Copyright 2000-2011 University of Southampton.
=for COPYRIGHT END
=for LICENSE BEGIN
This file is part of EPrints L.
EPrints is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
EPrints is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public
License along with EPrints. If not, see L.
=for LICENSE END