-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSV_ZENODO.py
142 lines (104 loc) · 4.77 KB
/
CSV_ZENODO.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from fileinput import close
from importlib.metadata import files, metadata
from importlib.resources import path
from logging import FileHandler, exception
from urllib import request
from wsgiref import headers
import requests
import json
import os
import logging
#This funtion is used to produce a usefull metadata array needed for the Upload_zenodo funtion
#able to edit at the will of the user.
def Metadata_builder(Title, Description, name, Affiliation, type):
Data = {
metadata:{
'title' : Title,
'upload_type' : type,
'description' : Description,
'creators' : [{'name' : name, 'affiliation' : Affiliation}]
}
}
return Data
#This funtion has the objetive to upload any file to an account at the zenodo repertory, using
# the folowing funtions we are able to access the API and create a publication, editing it
# who ever we want, and after that publishing it.
def Upload_Zenodo(Token, Meta_Data, Path, Filename):
# Variables introduced by user.
ACCESS_TOKEN = Token # Access key to zenodo profile
path = Path # Direction to file position
data_new = Meta_Data # Information needed for the documentation
filename = Filename
headers = {"Content-Type": "application/json"}
logging.basicConfig(format = '%(asctime)-5s %(name)-15s %(levelname)-8s %(message)s', level = logging.DEBUG, filename = "/home/osoc22/STARS4ALL/logger/photometer.log", filemode = "a")
r = requests.get('https://zenodo.org/api/deposit/depositions', params={'access_token': ACCESS_TOKEN})
print(r.status_code)
logging.info("API has been connected, Status 200")
#Funtion Empy_File() has the objetive to test the API
# so we will be able to send usefull information next time.
def Empty_File():
r = requests.post('https://zenodo.org/api/deposit/depositions',
params={'access_token': ACCESS_TOKEN},
json={},
headers=headers)
if(r.status_code == 201):
print(r.status_code)
logging.info("Empty file created, Status 201")
return r
else:
logging.info("Error at creating empty file")
raise exception("There has been an error at accessing to the Id")
#funtion Create_file is designed to upload the file selected
# to zenodo, at the access_token account
def Create_file(Name):
with open(path, "rb") as fp:
r = requests.put(
"%s/%s" % (bucket_url, Name),
data=fp,
params={'access_token': ACCESS_TOKEN}
)
if(r.status_code == 200):
print(r.status_code)
logging.info("File created, Status 200")
else:
logging.info("Error at creating File")
raise exception("There has been an error at creating the file")
#The funtion Edit_File allow us to edit the file
# and introduce any information we need.
def Edit_File(Id, data):
r = requests.put('https://zenodo.org/api/deposit/depositions/%s' % Id,
params={'access_token': ACCESS_TOKEN}, data=json.dumps(data),
headers=headers)
if(r.status_code == 200):
print(r.status_code)
logging.info("Filed edited, Status 200")
else:
logging.info("Error at editing file")
raise exception("There has been an error at editing the file")
#And finally the funtion Publish_File publish
# the file stored at the users account.
def Publish_File(Id):
r = requests.post('https://zenodo.org/api/deposit/depositions/%s/actions/publish' % Id,
params={'access_token': ACCESS_TOKEN} )
if(r.status_code == 202):
print(r.status_code)
logging.info("File publised, Status 202")
else:
logging.info("Error at publising file")
raise exception("There has been an error at publising the file")
#Execution of the funtions.
try:
p = Empty_File()
deposition_id = p.json()['id'] #Identificator that allow us to access the file at our Zenodo account.
bucket_url = p.json()["links"]["bucket"] #URL used to access the API file receptor.
logging.debug("Creating file :"+filename)
Create_file(filename) #We create the file
logging.debug()
Edit_File(deposition_id, data_new) #we edit the file
#Publish_File(deposition_id) #we publish the file
logging.info("File upload to Zenodo successfully")
except:
print("The Scrip used to upload the file to Zenodo has failed")
logging.info("Error at uploding to zenodo")
close()
#End of the funtion.