App-arxiv2bib

 view release on metacpan or  search on metacpan

script/arxiv2bib  view on Meta::CPAN

#!/usr/bin/env perl
use warnings;
use v5.20;

use feature qw(signatures);
no warnings qw(experimental::signatures);

use Data::Dumper;
use Getopt::Long;
use Mojo::UserAgent;
use Pod::Usage;

my($help,$man,$dry,$verbose);
my($sortBy,$sortOrder) = qw(submittedDate descending);
my($id_list,$start,$max_results) = ("",0,200);
my %type;
my $label;

GetOptions (
    \%type,
    "h|help" => \$help,
    "man" => \$man,
    "n|dry" => \$dry,
    "v|verbose" => \$verbose,

    "amsrefs",
    "raw",
    
    "l|label:s" => \$label,

    "sortBy:s" => \$sortBy,
    "sortOrder:s" => \$sortOrder,

    "id_list:s" => \$id_list,
    "start:i" => \$start,
    "max_results:i" => \$max_results,    
    ) or pod2usage(-verbose => 0);

($help) && do {
    pod2usage(-verbose => 1);
    exit;
};

($man) && do {
    pod2usage(-verbose => 2);
    exit;
};

sub entry2hsh($entry) {
    my %h;
    for (qw(id updated published title summary)) {
	$h{$_} = $entry->at($_)->text;
    }
    $h{'authors'} = $entry->find('author name')->map('text');
    $h{'category'} = $entry->find('category')->map(sub {$_->{'term'}});
    $h{'abs'} = $entry->at('link[type="text/html"]')->{'href'};
    $h{'pdf'} = $entry->at('link[type="application/pdf"]')->{'href'};
    return \%h;
}

sub hsh2strings($h,$label) {
    my $authors = $h->{'authors'}->join(', ');
    my $title = $h->{'title'};
    my $year = ($h->{'published'} =~ /^(\d*)/) && $1;
    my $id = $h->{'id'};
    $label//= ($id =~ /.*\/(.*)/) && $1;
    return [$label,$authors,$title,$year,$id];
}

sub hsh2bbtx($h,$label) {
    my ($l,$authors,$title,$year,$id) = @{hsh2strings($h,$label)};
    my $msg = << "EOF";
\@misc{$l,
Author = {$authors},
Title = {$title},
Year = {$year},
note = {$id},
}
EOF
    return $msg;
}

sub hsh2ams($h,$label) {
    my ($l,$authors,$title,$year,$id) = @{hsh2strings($h,$label)};
    my $msg = << "EOF";
\\bib{$l}{misc}{
   author={$authors},
   title={$title},
   date={$year},
   note={$id},   
}
EOF
    return $msg;
}

sub renderEntry($entry,$label,$type) {
    (exists $type->{raw}) && return Dumper(entry2hsh($entry));
    (exists $type->{amsrefs}) && return hsh2ams(entry2hsh($entry),$label);
    return hsh2bbtx(entry2hsh($entry),$label);    
}

my $ua = Mojo::UserAgent->new;
$ua = $ua->max_redirects(3);

my $baseURL="http://export.arxiv.org/api/query";
my %form=(
    search_query => join(" ", @ARGV),
    id_list => $id_list,
    start => $start,
    max_results => $max_results,
    sortBy => $sortBy,



( run in 0.318 second using v1.01-cache-2.11-cpan-bbe5e583499 )