forked from tsutterley/reference-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_doi.py
71 lines (62 loc) · 2.29 KB
/
open_doi.py
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
#!/usr/bin/env python
u"""
open_doi.py (12/2020)
Opens the webpages and crossref.org API webpages associated with a Digital
Object Identifier (DOI)
CALLING SEQUENCE:
python open_doi.py 10.1038/ngeo102
COMMAND LINE OPTIONS:
-C, --crossref: Open crossref.org API associated with a DOI
-V, --verbose: Verbose output of webpages opened
UPDATE HISTORY:
Updated 12/2020: using argparse to set command line options
Written 10/2017
"""
from __future__ import print_function
import future.standard_library
import sys
import os
import argparse
import posixpath
import webbrowser
with future.standard_library.hooks():
import urllib.parse
#-- PURPOSE: open webpage associated with a Digital Object Identifier (DOI)
def doiopen(DOI, VERBOSE=False):
#-- Open URL for DOI in a new tab, if browser window is already open
URL = posixpath.join('https://doi.org',DOI)
print(URL) if VERBOSE else None
webbrowser.open_new_tab(URL)
#-- PURPOSE: open crossref.org API associated with a DOI
def crossrefopen(DOI, VERBOSE=False):
#-- Open URL for DOI in a new tab, if browser window is already open
URL = posixpath.join('https://api.crossref.org','works',
urllib.parse.quote_plus(doi))
print(URL) if VERBOSE else None
webbrowser.open_new_tab(URL)
#-- main program that calls doiopen() and crossrefopen()
def main():
#-- Read the system arguments listed after the program
parser = argparse.ArgumentParser(
description="""Opens the webpages and crossref.org API webpages
associated with a Digital Object Identifier (DOI)
"""
)
#-- command line parameters
parser.add_argument('doi',
type=str, nargs='+',
help='Digital Object Identifier (DOI) of the publication')
parser.add_argument('--crossref','-C',
default=False, action='store_true',
help='Open crossref.org API associated with a DOI')
parser.add_argument('--verbose','-V',
default=False, action='store_true',
help='Verbose output of webpages opened')
args = parser.parse_args()
#-- run for each DOI entered after the program
for DOI in args.doi:
doiopen(DOI, VERBOSE=args.verbose)
crossrefopen(DOI, VERBOSE=args.verbose) if args.crossref else None
#-- run main program
if __name__ == '__main__':
main()