1 | #!/usr/bin/env python
|
---|
2 |
|
---|
3 | # Currently only tested with Python3
|
---|
4 | from __future__ import print_function
|
---|
5 |
|
---|
6 |
|
---|
7 | # For more info on the CrossRef API, see:
|
---|
8 | # https://www.crossref.org/documentation/retrieve-metadata/rest-api/a-non-technical-introduction-to-our-api/
|
---|
9 | #
|
---|
10 | # The default DOI value used below is based on the one given in the CrossRef documentation
|
---|
11 |
|
---|
12 | import os
|
---|
13 | import sys
|
---|
14 | import json
|
---|
15 |
|
---|
16 | import argparse
|
---|
17 |
|
---|
18 | import requests
|
---|
19 | import urllib.parse
|
---|
20 |
|
---|
21 | import util
|
---|
22 |
|
---|
23 | base_url = "https://api.crossref.org"
|
---|
24 | search_work_url = base_url+"/works/"
|
---|
25 |
|
---|
26 |
|
---|
27 | # JSON is returned by default by CrossRef, but let's be explicit!
|
---|
28 | headers = {
|
---|
29 | 'Content-type': 'application/json'
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | def getDOI(doi):
|
---|
34 |
|
---|
35 | query_url = search_work_url + urllib.parse.quote_plus(doi)
|
---|
36 |
|
---|
37 | print("query_url="+query_url)
|
---|
38 |
|
---|
39 | # Based on:
|
---|
40 | # https://stackoverflow.com/questions/25491090/how-to-use-python-to-execute-a-curl-command
|
---|
41 | response = requests.get(query_url, headers=headers)
|
---|
42 |
|
---|
43 | returned_json_str = response.content
|
---|
44 | returned_json = json.loads(returned_json_str)
|
---|
45 |
|
---|
46 | return returned_json
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | if __name__ == "__main__":
|
---|
51 |
|
---|
52 | parser = argparse.ArgumentParser()
|
---|
53 |
|
---|
54 | parser.add_argument('doi', nargs='?')
|
---|
55 | parser.add_argument('output-file.json', nargs='?')
|
---|
56 |
|
---|
57 | args = parser.parse_args()
|
---|
58 |
|
---|
59 | doi = getattr(args,'doi');
|
---|
60 | if (doi == None):
|
---|
61 | # CrossRef Example
|
---|
62 | crossref_example_doi = "10.1155/2014/413629"
|
---|
63 |
|
---|
64 | # Bainbridge et al example, IJDL user-centric approach ...
|
---|
65 | stinky_doi = "10.1145/1998076.1998084"
|
---|
66 |
|
---|
67 | print("As a default, selecting CrossRef example DOI: " + crossref_example_doi)
|
---|
68 | doi = crossref_example_doi
|
---|
69 |
|
---|
70 | json_output_filename = getattr(args,'output-file.json');
|
---|
71 | if (json_output_filename == None):
|
---|
72 | encoded_doi = urllib.parse.quote_plus(doi)
|
---|
73 | json_output_filename = "crossref-"+encoded_doi+".json"
|
---|
74 |
|
---|
75 | returned_json = getDOI(doi)
|
---|
76 |
|
---|
77 | util.writeJSON(json_output_filename,returned_json)
|
---|
78 |
|
---|