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

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

More careful saving of JSON as UTF8, without the \u being used

  • 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 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
27# Based on:
28# https://stackoverflow.com/questions/25491090/how-to-use-python-to-execute-a-curl-command
29
30base_url = "https://api.core.ac.uk/v3"
31
32search_work_url = base_url+"/search/works/"
33discover_url = base_url+"/discover"
34
35headers = {
36 'Content-type': 'application/json'
37}
38
39
40def getDOI(doi):
41
42 query_args = "api_key="+apiKey+"&q="+urllib.parse.quote_plus("doi:\""+doi+"\"")
43 query_url = search_work_url + "?" + query_args
44
45
46 print("query_url="+query_url)
47
48 response = requests.get(query_url, headers=headers)
49
50
51 #data = '{}'
52 # More work to be done to see if the following can be made to work
53 #data = {
54 # "api_key": apiKey,
55 # "q": "doi:\""+doi+"\""
56 #}
57 #response = requests.get(query_url, headers=headers, data=data)
58
59
60 returned_json_str = response.content
61
62 returned_json = json.loads(returned_json_str)
63
64 return returned_json
65
66
67
68if __name__ == "__main__":
69
70 parser = argparse.ArgumentParser()
71 #parser.add_argument('--sheetname', help="The name of the sheet within the Excel file to extractc data from")
72 #parser.add_argument('--votingtype', choices=["J","T", "JT"], help="Filter to only J=Jury, T=Tele cast votes, JT=Combined jury and tele votes")
73 parser.add_argument('doi', nargs='?')
74 parser.add_argument('output-file.json', nargs='?')
75
76 args = parser.parse_args()
77
78 #sheetname = getattr(args,'sheetname');
79 #voting_type = getattr(args,'votingtype');
80
81 doi = getattr(args,'doi');
82 if (doi == None):
83 oacore_example_doi="10.1016/0370-2693(96)00910-0"
84
85 # Bainbridge et al example, IJDL user-centric approach ...
86 stinky_doi="10.1145/1998076.1998084"
87
88 print("As a default, selecting Core example DOI: " + oacore_example_doi)
89 doi = oacore_example_doi
90
91 json_output_filename = getattr(args,'output-file.json');
92 if (json_output_filename == None):
93 encoded_doi = urllib.parse.quote_plus(doi)
94 json_output_filename = "oacore-"+encoded_doi+".json"
95
96 returned_json = getDOI(doi)
97 returned_json_prettyprint = json.dumps(returned_json, indent=2, ensure_ascii=False)
98
99 print("Saving returned JSON output to: " + json_output_filename)
100
101 with open(json_output_filename, 'w', encoding="utf8") as fout:
102 fout.write(returned_json_prettyprint)
103
104
105# Or consider using their Python API:
106#
107# https://github.com/oacore/pyoacore
108
109# They also support Java, R and (it looks like) NodeJS
110
111
112# 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.