syrte
12/4/2017 - 1:27 AM

bibtex_abbr.py

# take from AASTex60

\aj             % AJ                            % Astronomical Journal
\araa           % ARA\&A                        % Annual Review of Astron and Astrophys
\apj            % ApJ                           % Astrophysical Journal
\apjl           % ApJL                          % Astrophysical Journal, Letters
\apjs           % ApJS                          % Astrophysical Journal, Supplement
\ao             % ApOpt                         % Applied Optics
\apss           % Ap\&SS                        % Astrophysics and Space Science
\aap            % A\&A                          % Astronomy and Astrophysics
\aapr           % A\&A~Rv                       % Astronomy and Astrophysics Reviews
\aaps           % A\&AS                         % Astronomy and Astrophysics, Supplement
\azh            % AZh                           % Astronomicheskii Zhurnal
\baas           % BAAS                          % Bulletin of the AAS
\icarus         % Icarus                        % Icarus
\jrasc          % JRASC                         % Journal of the RAS of Canada
\memras         % MmRAS                         % Memoirs of the RAS
\mnras          % MNRAS                         % Monthly Notices of the RAS
\pra            % PhRvA                         % Physical Review A: General Physics
\prb            % PhRvB                         % Physical Review B: Solid State
\prc            % PhRvC                         % Physical Review C
\prd            % PhRvD                         % Physical Review D
\pre            % PhRvE                         % Physical Review E
\prl            % PhRvL                         % Physical Review Letters
\pasp           % PASP                          % Publications of the ASP
\pasj           % PASJ                          % Publications of the ASJ
\qjras          % QJRAS                         % Quarterly Journal of the RAS
\skytel         % S\&T                          % Sky and Telescope
\solphys        % SoPh                          % Solar Physics
\sovast         % Soviet~Ast.                   % Soviet Astronomy
\ssr            % SSRv                          % Space Science Reviews
\zap            % ZA                            % Zeitschrift fuer Astrophysik
\nat            % Nature                        % Nature
\iaucirc        % IAUC                          % IAU Cirulars
\aplett         % Astrophys.~Lett.              % Astrophysics Letters
\apspr          % Astrophys.~Space~Phys.~Res.   % Astrophysics Space Physics Research
\bain           % BAN                           % Bulletin Astronomical Institute of the Netherlands
\fcp            % FCPh                          % Fundamental Cosmic Physics
\gca            % GeoCoA                        % Geochimica Cosmochimica Acta
\grl            % Geophys.~Res.~Lett.           % Geophysics Research Letters
\jcp            % JChPh                         % Journal of Chemical Physics
\jgr            % J.~Geophys.~Res.              % Journal of Geophysics Research
\jqsrt          % JQSRT                         % Journal of Quantitiative Spectroscopy and Radiative Trasfer
\memsai         % MmSAI                         % Mem. Societa Astronomica Italiana
\nphysa         % NuPhA                         % Nuclear Physics A
\physrep        % PhR                           % Physics Reports
\physscr        % PhyS                          % Physica Scripta
\planss         % Planet.~Space~Sci.            % Planetary Space Science
\procspie       % Proc.~SPIE                    % Proceedings of the SPIE
\actaa          % AcA                           % Acta Astronomica
\caa            % ChA\&A                        % Chinese Astronomy and Astrophysics
\cjaa           % ChJA\&A                       % Chinese Journal of Astronomy and Astrophysics
\jcap           % JCAP                          % Journal of Cosmology and Astroparticle Physics
\na             % NewA                          % New Astronomy
\nar            % NewAR                         % New Astronomy Review
\pasa           % PASA                          % Publications of the Astron. Soc. of Australia
\rmxaa          % RMxAA                         % Revista Mexicana de Astronomia y Astrofisica
\maps           % M\&PS                         % Meteoritics and Planetary Science
\aas            % AAS Meeting Abstracts         % American Astronomical Society Meeting Abstracts
\dps            % AAS/DPS Meeting Abstracts     % American Astronomical Society/Division for Planetary Sciences Meeting Abstracts
#!/usr/bin/env python
import requests
from lxml import etree
import bibtexparser
import io
import sys
import re


def strip_name(string):
    string = string.strip().lower().replace(':', '').replace('-', '').replace('\\', '').replace('&', '')
    string = re.sub(r'^the\s|:|&|-|\\', '', string)
    return string


def strip_abbr(string):
    string = string.strip().replace("&", "\&")
    return string


def get_journals():
    urls = ["http://adsabs.harvard.edu/abs_doc/journals1.html",
            "http://ads.bao.ac.cn/abs_doc/journals1.html"]

    r = requests.get(urls[0])
    p = etree.HTML(r.content)

    xpaths = p.xpath('body/pre/a')
    journals = {strip_name(journal.tail): strip_abbr(journal.text)
                for journal in xpaths if journal.tail is not None}

    journals.update({
        'astrophysical journal letters': 'APJ',
        'monthly notices of the royal astronomical society letters': 'MNRAS',
        'journal of physics conference series': 'JPhCS',
        'journal of statistical computation and simulation': 'JSCS',
    })

    return journals


def convert(journals, bibtex_input, bibtex_write=None):
    bibtex_database = bibtexparser.load(io.open(bibtex_input))

    for entry in bibtex_database.entries:
        if 'journal' in entry:
            journal = entry['journal']
            striped = strip_name(journal)
            if striped in journals:
                entry['journal'] = journals[striped]
            elif journal in journals.values():
                pass
            else:
                print(u"Warning: No abbreviation for '{}'".format(entry['journal']))

    if bibtex_write:
        bibtexparser.dump(bibtex_database, io.open(bibtex_write, 'w'))
    else:
        bibtexparser.dump(bibtex_database, sys.stdout)


journals = get_journals()

if len(sys.argv) == 2:
    convert(journals, sys.argv[1])
elif len(sys.argv) == 3:
    convert(journals, sys.argv[1], sys.argv[2])
else:
    raise ValueError("usage: bibtex_abbr input.bib [output.bib]")