29
29
if not os .path .exists (caltechdata_directory ):
30
30
os .makedirs (caltechdata_directory )
31
31
32
+
32
33
def generate_key ():
33
34
return Fernet .generate_key ()
34
35
36
+
35
37
# Load the key from a file or generate a new one if not present
36
38
def load_or_generate_key ():
37
39
key_file = os .path .join (caltechdata_directory , "key.key" )
@@ -44,19 +46,22 @@ def load_or_generate_key():
44
46
f .write (key )
45
47
return key
46
48
49
+
47
50
# Encrypt the token
48
51
def encrypt_token (token , key ):
49
52
f = Fernet (key )
50
53
return f .encrypt (token .encode ())
51
54
55
+
52
56
# Decrypt the token
53
57
def decrypt_token (encrypted_token , key ):
54
58
f = Fernet (key )
55
59
return f .decrypt (encrypted_token ).decode ()
56
60
61
+
57
62
# Function to get or set token
58
63
def get_or_set_token ():
59
-
64
+
60
65
key = load_or_generate_key ()
61
66
token_file = os .path .join (caltechdata_directory , "token.txt" )
62
67
try :
@@ -75,7 +80,8 @@ def get_or_set_token():
75
80
return token
76
81
else :
77
82
print ("Tokens do not match. Please try again." )
78
-
83
+
84
+
79
85
def welcome_message ():
80
86
print ("Welcome to CaltechDATA CLI" )
81
87
@@ -245,6 +251,7 @@ def get_names(orcid):
245
251
return None , None
246
252
return family_name , given_name
247
253
254
+
248
255
def write_s3cmd_config (access_key , secret_key , endpoint ):
249
256
configf = os .path .join (home_directory , ".s3cfg" )
250
257
if not os .path .exists (key_file ):
@@ -257,6 +264,8 @@ def write_s3cmd_config(access_key, secret_key, endpoint):
257
264
secret_key = { secret_key }
258
265
"""
259
266
)
267
+
268
+
260
269
def upload_supporting_file (record_id = None ):
261
270
filepath = ""
262
271
filepaths = []
@@ -328,7 +337,9 @@ def upload_supporting_file(record_id=None):
328
337
print (
329
338
f"Error: File '{ filename } ' not found. Please enter a valid filename."
330
339
)
331
- add_more = get_user_input ("Do you want to add more files? (y/n): " ).lower ()
340
+ add_more = get_user_input (
341
+ "Do you want to add more files? (y/n): "
342
+ ).lower ()
332
343
if add_more != "y" :
333
344
break
334
345
break
@@ -337,6 +348,8 @@ def upload_supporting_file(record_id=None):
337
348
else :
338
349
print ("Invalid input. Please enter 'link' or 'upload' or 'n'." )
339
350
return filepaths , file_links
351
+
352
+
340
353
def upload_data_from_file ():
341
354
while True :
342
355
print ("Current JSON files in the directory:" )
@@ -388,24 +401,25 @@ def create_record():
388
401
if existing_data :
389
402
if filepath != "" :
390
403
response = caltechdata_write (
391
- existing_data , token , filepath , production = False , publish = False
404
+ existing_data , token , filepath , production = True , publish = False
392
405
)
393
406
elif file_link != "" :
394
407
response = caltechdata_write (
395
408
existing_data ,
396
409
token ,
397
410
file_links = [file_link ],
398
411
s3_link = file_link ,
399
- production = False ,
412
+ production = True ,
400
413
publish = False ,
401
414
)
402
415
else :
403
416
response = caltechdata_write (
404
- existing_data , token , production = False , publish = False
417
+ existing_data , token , production = True , publish = False
405
418
)
406
419
rec_id = response
407
420
print (
408
- f"""You can view and publish this record at https://data.caltechlibrary.dev/uploads/{ rec_id }
421
+ f"""You can view and publish this record at
422
+ https://data.caltech.edu/uploads/{ rec_id }
409
423
If you need to upload large files to S3, you can type
410
424
`s3cmd put DATA_FILE s3://ini230004-bucket01/{ rec_id } /"""
411
425
)
@@ -454,23 +468,24 @@ def create_record():
454
468
if confirm_upload ():
455
469
if filepath != "" :
456
470
response = caltechdata_write (
457
- metadata , token , filepath , production = False , publish = False
471
+ metadata , token , filepath , production = True , publish = False
458
472
)
459
473
elif file_link != "" :
460
474
response = caltechdata_write (
461
475
metadata ,
462
476
token ,
463
477
file_links = [file_link ],
464
- production = False ,
478
+ production = True ,
465
479
publish = False ,
466
480
)
467
481
else :
468
482
response = caltechdata_write (
469
- metadata , token , production = False , publish = False
483
+ metadata , token , production = True , publish = False
470
484
)
471
485
rec_id = response
472
486
print (
473
- f"""You can view and publish this record at https://data.caltechlibrary.dev/uploads/{ rec_id }
487
+ f"""You can view and publish this record at
488
+ https://data.caltech.edu/uploads/{ rec_id }
474
489
If you need to upload large files to S3, you can type
475
490
`s3cmd put DATA_FILE s3://ini230004-bucket01/{ rec_id } /"""
476
491
)
@@ -493,7 +508,7 @@ def edit_record():
493
508
with open (file_name , "r" ) as file :
494
509
metadata = json .load (file )
495
510
response = caltechdata_edit (
496
- record_id , metadata , token , production = False , publish = False
511
+ record_id , metadata , token , production = True , publish = False
497
512
)
498
513
if response :
499
514
print ("Metadata edited successfully." )
@@ -505,46 +520,52 @@ def edit_record():
505
520
print ("No metadata file found." )
506
521
choice = get_user_input ("Do you want to add files? (y/n): " ).lower ()
507
522
if choice == "y" :
508
- API_URL_TEMPLATE = "https://data.caltechlibrary.dev /api/records/{record_id}/files"
523
+ API_URL_TEMPLATE = "https://data.caltech.edu /api/records/{record_id}/files"
509
524
url = API_URL_TEMPLATE .format (record_id = record_id )
510
525
511
- API_URL_TEMPLATE2 = "https://data.caltechlibrary.dev/api/records/{record_id}/draft/files"
526
+ API_URL_TEMPLATE2 = (
527
+ "https://data.caltech.edu/api/records/{record_id}/draft/files"
528
+ )
512
529
url2 = API_URL_TEMPLATE2 .format (record_id = record_id )
513
530
response = requests .get (url )
514
531
response2 = requests .get (url2 )
515
532
filepath , file_link = upload_supporting_file (record_id )
516
533
print (file_link )
517
534
if response .status_code == 404 and response2 .status_code == 404 :
518
535
keepfile = False
519
- else :
536
+ else :
520
537
keepfile = input ("Do you want to keep existing files? y/n: " )
521
538
if keepfile == "y" :
522
539
keepfile = True
523
540
else :
524
541
keepfile = False
525
542
if filepath != "" :
526
543
response = caltechdata_edit (
527
- record_id , token = token , files = filepath , production = False , publish = False , keepfiles = keepfile ,
544
+ record_id ,
545
+ token = token ,
546
+ files = filepath ,
547
+ production = True ,
548
+ publish = False ,
549
+ keepfiles = keepfile ,
528
550
)
529
551
elif file_link != "" :
530
552
response = caltechdata_edit (
531
553
record_id ,
532
554
metadata ,
533
555
token = token ,
534
556
file_links = file_link ,
535
- production = False ,
557
+ production = True ,
536
558
publish = False ,
537
- keepfile = keepfile
559
+ keepfile = keepfile ,
538
560
)
539
561
rec_id = response
540
562
print (
541
- f"You can view and publish this record at https://data.caltechlibrary.dev /uploads/{ rec_id } \n "
563
+ f"You can view and publish this record at https://data.caltech.edu /uploads/{ rec_id } \n "
542
564
)
543
565
544
566
545
567
def download_file_by_id (record_id , token = None ):
546
- url = f"https://data.caltechlibrary.dev/api/records/{ record_id } "
547
-
568
+ url = f"https://data.caltech.edu/api/records/{ record_id } "
548
569
549
570
headers = {
550
571
"accept" : "application/vnd.datacite.datacite+json" ,
@@ -555,7 +576,7 @@ def download_file_by_id(record_id, token=None):
555
576
556
577
try :
557
578
response = requests .get (url , headers = headers )
558
-
579
+
559
580
if response .status_code != 200 :
560
581
# Might have a draft
561
582
response = requests .get (
0 commit comments