-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmponline_templates.py
106 lines (87 loc) · 3.45 KB
/
dmponline_templates.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
import sys
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from dotenv import load_dotenv
from datetime import datetime
import os
import uuid
import time
# Simple script that harvests all templates from DMPonline in order to get correct template ids.
# Todo: currently only get up to 100 entries (1 page). At the moment this is sufficient.
# Example: ./python3 dmponline_export_templates.py
#
# // [email protected]
# Settings
load_dotenv() # loads the .env file which contains login-information
dmpurl = os.getenv("DMPONLINE_API_URL")
dmpuser = os.getenv("DMPONLINE_USER")
dmppw = os.getenv("DMPONLINE_PW")
dmp_id_prefix = os.getenv("DMP_ID_PREFIX")
logfile = os.getenv("LOGFILE")
# Input params
parser = ArgumentParser(description="Create new DMP using data from Swecris.",
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-v", "--verbose", action="store_true", help="increase verbosity")
#parser.add_argument("-i", "--grantid", default="", help="Grant ID, i.e. 2023-012345", required=True)
args = parser.parse_args()
# Create Swecris ID and look up corresponding project in Swecris API
#grantid = args.grantid
# Go ahead and create DMP in DMPOnline from here...
print('Should I fetch all templates from DMPonline? (y/n)')
yes = {'yes', 'y', 'ye', 'j', 'ja', ''}
no = {'no', 'n', 'nej'}
choice = input().lower()
if choice in yes:
# Authorize
dmp_auth_bearer = ""
dmp_auth_url = os.getenv("DMPONLINE_API_URL") + "authenticate"
auth_headers = {
"Accept": "application/json",
"Content-Type": "application/json",
}
auth_body = {
"grant_type": "authorization_code",
"email": os.getenv("DMPONLINE_USER"),
"code": os.getenv("DMPONLINE_AUTH_CODE"),
}
try:
authdata = requests.post(
url=dmp_auth_url, json=auth_body, headers=auth_headers
).text
if "Internal server error" in authdata:
print("Authentication request failed! Exiting.")
exit()
authdata = json.loads(authdata)
dmp_auth_bearer = authdata["access_token"]
print("Authorized! Access token: " + dmp_auth_bearer)
except requests.exceptions.HTTPError as e:
print("Failed! authdata: " + authdata)
exit()
# Request templates using the authentication token (mainly as a test)
try:
dmp_template_url = os.getenv("DMPONLINE_API_URL") + 'templates'
template_headers = {'Accept': 'application/json','Authorization': 'Bearer ' + dmp_auth_bearer}
templatedata = requests.get(url=dmp_template_url, headers=template_headers).text
print(templatedata)
if not os.path.exists('Templates'):
os.makedirs('Templates')
timestr = time.strftime("%Y%m%d-%H%M%S")
filename = "Templates_from_DMPonline_" + timestr + ".json"
path = os.path.join('Templates', filename)
out_file = open(path, "w",encoding="utf-8")
out_file.write(templatedata)
out_file.close()
print("Stored as: " + path)
except requests.exceptions.HTTPError as e:
print('Failed! templatedata: ' + templatedata)
exit()
elif choice in no:
print('OK. Will exit then.')
exit()
else:
sys.stdout.write("Please respond with 'y'(es) or 'n'(o)")
exit()