From dec7257eff27f8b525adf22be0aa163b7f07a3bf Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Thu, 14 Jan 2016 00:04:33 +0100 Subject: [PATCH] Add a function to look for updated arXiv versions. --- libbmc/repositories/arxiv.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/libbmc/repositories/arxiv.py b/libbmc/repositories/arxiv.py index acf6a6f..79c03d2 100644 --- a/libbmc/repositories/arxiv.py +++ b/libbmc/repositories/arxiv.py @@ -2,6 +2,7 @@ This file contains all the arXiv-related functions. """ import arxiv2bib +import bibtexparser import io import re import requests @@ -176,6 +177,36 @@ ARXIV_URL = "http://arxiv.org/abs/{arxiv_id}" ARXIV_EPRINT_URL = "http://arxiv.org/e-print/{arxiv_id}" +def get_latest_version(arxiv_id): + """ + Find the latest version of a given arXiv eprint. + + :param arxiv_id: The arXiv ID to query. + :returns: The latest version on eprint as a string, or ``None``. + """ + # Get updated bibtex + # Trick: strip the version from the arXiv id, to query updated BibTeX for + # the preprint and not the specific version + arxiv_preprint_id = strip_version(arxiv_id) + updated_bibtex = bibtexparser.loads(get_bibtex(arxiv_preprint_id)) + updated_bibtex = next(updated_bibtex.entries_dict) + + try: + return updated_bibtex["eprint"] + except KeyError: + return None + + +def strip_version(arxiv_id): + """ + Remove the version suffix from an arXiv id. + + :param arxiv_id: The arXiv ID to strip. + :returns: The arXiv ID without the suffix version + """ + return re.sub(r"v\d+\Z", '', arxiv_id) + + def is_valid(arxiv_id): """ Check that a given arXiv ID is a valid one.