Browse Source

added bibtex_entry_from_pmid() and script entrypoint

Fabian Peter Hammerle 5 years ago
commit
121364ca36
2 changed files with 69 additions and 0 deletions
  1. 43 0
      pubmed_bibtex/__init__.py
  2. 26 0
      tests/test_bibtex_entry_from_pmid.py

+ 43 - 0
pubmed_bibtex/__init__.py

@@ -0,0 +1,43 @@
+import argparse
+import html.parser
+import re
+
+import requests
+
+_TEXMED_URL_PATTERN = 'https://www.bioinformatics.org/texmed/cgi-bin' \
+                      '/list.cgi?PMID={pmid}&linkOut'
+
+
+class _TeXMedHtmlParser(html.parser.HTMLParser):
+
+    def __init__(self):
+        self.bibtex_entry = None
+        super().__init__()
+
+    @staticmethod
+    def _strip_bibtex_entry(data: str) -> str:
+        return re.sub(r'\n\% \d+\s?\n', '', data).strip() + '\n'
+
+    def handle_data(self, data: str) -> None:
+        if 'Author' in data:
+            self.bibtex_entry = self._strip_bibtex_entry(data)
+
+
+def bibtex_entry_from_pmid(pmid: str) -> str:
+    assert pmid.isdigit(), pmid
+    resp = requests.get(_TEXMED_URL_PATTERN.format(pmid=pmid))
+    resp.raise_for_status()
+    parser = _TeXMedHtmlParser()
+    parser.feed(resp.text)
+    return parser.bibtex_entry
+
+
+def main():
+    argparser = argparse.ArgumentParser()
+    argparser.add_argument('pmid')
+    args = argparser.parse_args()
+    print(bibtex_entry_from_pmid(pmid=args.pmid),
+          end='')
+
+if __name__ == '__main__':
+    main()

+ 26 - 0
tests/test_bibtex_entry_from_pmid.py

@@ -0,0 +1,26 @@
+import unittest.mock
+
+from pubmed_bibtex import bibtex_entry_from_pmid, main
+
+TEST_PMID = '31025164'
+TEST_BIBTEX_ENTRY = r"""@Article{pmid31025164,
+   Author="Egger, F.  and Hofer, C.  and Hammerle, F. P.  and Lofler, S.  and Nurnberg, M.  and Fiedler, L.  and Kriz, R.  and Kern, H.  and Huber, K. ",
+   Title="{{I}nfluence of electrical stimulation therapy on permanent pacemaker function}",
+   Journal="Wien. Klin. Wochenschr.",
+   Year="2019",
+   Month="Apr",
+   Note={[DOI:\href{https://dx.doi.org/10.1007/s00508-019-1494-5}{10.1007/s00508-019-1494-5}] [PubMed:\href{https://www.ncbi.nlm.nih.gov/pubmed/31025164}{31025164}] }
+}
+"""
+
+
+def test_bibtex_entry_from_pmid():
+    assert bibtex_entry_from_pmid(pmid=TEST_PMID) == TEST_BIBTEX_ENTRY
+
+
+def test_main(capsys):
+    with unittest.mock.patch('sys.argv', ['', TEST_PMID]):
+        main()
+    out, err = capsys.readouterr()
+    assert not err
+    assert out == TEST_BIBTEX_ENTRY