source: other-projects/bib-stinky/trunk/doi-stinky/crossref-get-doi.py@ 36327

Last change on this file since 36327 was 36327, checked in by davidb, 21 months ago

CrossRef version, based on a tidied up Core

  • Property svn:executable set to *
File size: 1.9 KB
Line 
1#!/usr/bin/env python
2
3# Currently only tested with Python3
4from __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
12import os
13import sys
14import json
15
16import argparse
17
18import requests
19import urllib.parse
20
21import util
22
23base_url = "https://api.crossref.org"
24search_work_url = base_url+"/works/"
25
26
27# JSON is returned by default by CrossRef, but let's be explicit!
28headers = {
29 'Content-type': 'application/json'
30}
31
32
33def 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
50if __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
Note: See TracBrowser for help on using the repository browser.