~adigitoleo/dotconf

dotconf/.local/bin/doi2bib -rwxr-xr-x 2.1 KiB
bee3885e — adigitoleo fix: Add workaround for qutebrowser renderer crashes 21 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python
"""Generate BibTeX reference metadata from DOI links.

INPUT_FILE must contain at most one DOI link per line.
Lines beginning with '#' in INPUT_FILE are safe to use for comments.

"""

import argparse
import os
import urllib.error as ue
import urllib.request as ur


def _get_args() -> argparse.Namespace:
    description, epilog = __doc__.split(os.linesep + os.linesep, 1)
    parser = argparse.ArgumentParser(description=description, epilog=epilog)
    parser.add_argument(
        "-f",
        "--fromfile",
        action="store_true",
        help="interpret the operand as an input file containing DOI links",
    )
    parser.add_argument(
        "-t",
        "--plaintext",
        action="store_true",
        help="request plain text citation instead of BibTeX",
    )
    parser.add_argument(
        "doi_or_file",
        metavar="DOI|INPUT_FILE",
        help="single DOI link, or INPUT_FILE if -f was given",
    )
    return parser.parse_args()


def getbib(doi: str, plain: bool = False) -> str:
    """Get BibTeX metadata or plain text citation from a DOI link."""
    url = doi.rstrip().lstrip()
    if plain:
        content_negotiation = "text/x-bibliography"
    else:
        content_negotiation = "application/x-bibtex"

    if not url.startswith("https://doi.org/"):
        url = "https://doi.org/" + url
    try:
        response = ur.urlopen(
            ur.Request(url, headers={"Accept": content_negotiation})
        )
    except ue.URLError:
        return "failed to open URL"
    return response.read().decode("utf-8")


def _main():
    args = _get_args()
    if args.fromfile:
        with open(args.doi_or_file) as file:
            for line in file:
                if line.startswith("#"):  # Treat hash as comment marker.
                    continue
                if line.rstrip():  # Skip blank lines.
                    if args.plaintext:
                        print(getbib(line, plain=args.plaintext))

    else:
        print(getbib(args.doi_or_file, plain=args.plaintext))


if __name__ == "__main__":
    try:
        _main()
    except KeyboardInterrupt:
        pass