如何编写一个健壮的Perl脚本,为一个BibTeX ID生成一个arXiv条目?
我的猜测是我应该使用arXiv API并使用XML::Atom解析它的响应。它应该为我提供构建BibTeX条目所需的部分信息。
以下是我的开场白:
use LWP::UserAgent;
use Text::BibTeX::Entry;
use XML::Atom;
my $arxivid = "hep-ph/9609357";
my $url = "http://export.arxiv.org/api/query?search_query=" . $arxivid . "&start=0&max_results=1";
my $browser = LWP::UserAgent->new();
my $response = $browser->get($url);
my $entry = Text::BibTeX::Entry->new();不使用arXiv API或XML::Atom的答案也是受欢迎的。
发布于 2020-05-23 18:46:03
下面是使用XML::Twig解析下载的XML文件的起点:
use feature qw(say);
use strict;
use warnings;
use LWP::UserAgent;
use Text::BibTeX;
use Text::BibTeX::Entry;
use XML::Twig;
use DateTime::Format::Strptime;
{
my $arxivid = "hep-ph/9609357";
my $url = "http://export.arxiv.org/api/query?search_query=" . $arxivid . "&start=0&max_results=1";
my $browser = LWP::UserAgent->new();
my $response = $browser->get($url);
my $xml = $response->content;
my $twig = XML::Twig->new->parse( $xml );
my $title = $twig->get_xpath ( '//entry/title',0 )->text;
my @authors;
for my $node ( $twig->findnodes( '//entry/author/name' )) {
push @authors, $node->text;
}
my $doi = $twig->get_xpath ( '//entry/link[@title="doi"]',0 )->att('href');
my $published = $twig->get_xpath ( '//entry/published',0 )->text;
my ( $year, $month) = parse_published( $published) ;
my $entry = Text::BibTeX::Entry->new();
$entry->set_metatype(BTE_REGULAR);
$entry->set_type('article');
$entry->set_key('article1');
$entry->set( 'title', $title );
$entry->set( 'author', join ' and ', @authors );
$entry->set( 'year', $year );
$entry->set( 'month', $month );
$entry->set( 'doi', $doi );
$entry->print(\*STDOUT);
}
sub parse_published {
my ( $published) = @_;
my $parser = DateTime::Format::Strptime->new(
pattern => '%FT%T%Z',
time_zone => 'UTC',
on_error => 'croak',
);
my $dt = $parser->parse_datetime($published);
return ( $dt->year, $dt->month_name);
}输出
@article{article1,
title = {Mixing-induced CP Asymmetries in Inclusive $B$ Decays},
author = {Martin Beneke and Gerhard Buchalla and Isard Dunietz},
year = {1996},
month = {September},
doi = {http://dx.doi.org/10.1016/S0370-2693(96)01648-6},
}https://stackoverflow.com/questions/61975686
复制相似问题