source: other-projects/bib-stinky/trunk/doi-stinky/oacore-get-doi.py@ 36331

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

Code tidy up, and refactoring to have a saveJSON function

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1#!/usr/bin/env python
2
3# Currently only tested with Python3
4from __future__ import print_function
5
6# For more info on the Core V3 API, see:
7# https://api.core.ac.uk/docs/v3
8#
9# The default DOI value used below is based on the one given in the Core V3 API documentation
10
11
12import os
13import sys
14import json
15
16import argparse
17
18import requests
19import urllib.parse
20
21import util
22
23
24apiKey = util.readKey("oacore-key.txt")
25
26
27base_url = "https://api.core.ac.uk/v3"
28
29search_work_url = base_url+"/search/works/"
30discover_url = base_url+"/discover"
31
32headers = {
33 'Content-type': 'application/json'
34}
35
36
37def getDOI(doi):
38
39 query_args = "api_key="+apiKey+"&q="+urllib.parse.quote_plus("doi:\""+doi+"\"")
40 query_url = search_work_url + "?" + query_args
41
42 # print("query_url="+query_url)
43
44 # Based on:
45 # https://stackoverflow.com/questions/25491090/how-to-use-python-to-execute-a-curl-command
46
47 response = requests.get(query_url, headers=headers)
48
49
50 #data = '{}'
51 # More work to be done to see if the following can be made to work
52 #data = {
53 # "api_key": apiKey,
54 # "q": "doi:\""+doi+"\""
55 #}
56 #response = requests.get(query_url, headers=headers, data=data)
57
58
59 returned_json_str = response.content
60
61 returned_json = json.loads(returned_json_str)
62
63 return returned_json
64
65
66
67if __name__ == "__main__":
68
69 parser = argparse.ArgumentParser()
70 #parser.add_argument('--sheetname', help="The name of the sheet within the Excel file to extractc data from")
71 #parser.add_argument('--votingtype', choices=["J","T", "JT"], help="Filter to only J=Jury, T=Tele cast votes, JT=Combined jury and tele votes")
72 parser.add_argument('doi', nargs='?')
73 parser.add_argument('output-file.json', nargs='?')
74
75 args = parser.parse_args()
76
77 #sheetname = getattr(args,'sheetname');
78 #voting_type = getattr(args,'votingtype');
79
80 doi = getattr(args,'doi');
81 if (doi == None):
82 oacore_example_doi="10.1016/0370-2693(96)00910-0"
83
84 # Bainbridge et al example, IJDL user-centric approach ...
85 stinky_doi="10.1145/1998076.1998084"
86
87 print("As a default, selecting Core example DOI: " + oacore_example_doi)
88 doi = oacore_example_doi
89
90 json_output_filename = getattr(args,'output-file.json');
91 if (json_output_filename == None):
92 encoded_doi = urllib.parse.quote_plus(doi)
93 json_output_filename = "oacore-"+encoded_doi+".json"
94
95 returned_json = getDOI(doi)
96 returned_json_prettyprint = json.dumps(returned_json, indent=2, ensure_ascii=False)
97
98 print("Saving returned JSON output to: " + json_output_filename)
99
100 with open(json_output_filename, 'w', encoding="utf8") as fout:
101 fout.write(returned_json_prettyprint)
102
103
104# Or consider using their Python API:
105#
106# https://github.com/oacore/pyoacore
107
108# They also support Java, R and (it looks like) NodeJS
109
110
111# https://core.ac.uk/search?q=doi%3A%2210.1186%2F1471-2458-6-309%22&page=1
Note: See TracBrowser for help on using the repository browser.