From 0dc3d1b9499bd235707b1103663a3f6ac7ee87ac Mon Sep 17 00:00:00 2001 From: ZOEY-YuanZhou Date: Thu, 8 Aug 2024 13:35:17 -0400 Subject: [PATCH 1/3] Add doi_registration --- doi_manager.py | 2 ++ utils/doi_mets.py | 65 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/doi_manager.py b/doi_manager.py index a9044d9..3293fc0 100755 --- a/doi_manager.py +++ b/doi_manager.py @@ -47,6 +47,7 @@ ) print("Writing proposed DOIs to patron metadata sheet...") write_doi_mets(sys.argv[3], dois) + DoiMinter.doi_registration(issue_level_mets, dois) print("Complete.") elif sys.argv[1] == "retrieve-fda-handles": @@ -61,6 +62,7 @@ 6. Report that write-out was successful """ + elif sys.argv[1] == "build-xml": print("Retrieving metadata from template sheet...") issue_level_mets = retrieve_doi_mets(sys.argv[3], "mets_main") diff --git a/utils/doi_mets.py b/utils/doi_mets.py index 92c4d1a..e27f1b0 100755 --- a/utils/doi_mets.py +++ b/utils/doi_mets.py @@ -339,11 +339,72 @@ def mint(reg_sheet_id, number_needed): break return generated_dois - def doi_registration(list_dois,title="",journal="",date="",unit="",contact="",url="",handle=""): + def doi_registration(key, list_dois, unit=None, contact=None, url=None): """ This function should write the DOIs passed as a pararamter (e.g. list of DOIs) to the registry GSheet In addition to the list of DOIs to register, it should include a series of optional parameters to write to the other columns of the sheet as well as auto-increment column A. The write step should be treated as an append to the values already in the GSheet to prevent accidentally overwriting what is there. :return: - """ \ No newline at end of file + """ + Journal = key[2][0] + Volume = key[5][3] + Issue = key[5][4] + Journal_information = f'{Journal}, {Volume} {Issue}' + # Issue Contents Metadata + Issue = key[8:] + + if os.path.exists(G_TOKEN_FILE): + creds = Credentials.from_authorized_user_file(G_TOKEN_FILE) + + service = build("sheets", "v4", credentials=creds) + sheet = service.spreadsheets() + + # Get the value from MAIN_DOI_REGISTRY_SHEET + result = sheet.values().get(spreadsheetId=MAIN_DOI_REGISTRY_SHEET, + range=REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE).execute() + column_values = result.get("values", []) + # Find where the blank in MAIN_DOI_REGISTRY_SHEET begins + index = column_values[-1][0] + + # Get current date + current_date = datetime.now() + formatted_date = current_date.strftime('%Y-%m-%d') + + # Add Journal data to MAIN_DOI_REGISTRY_SHEET + doi_index = -1 + index = int(index) + 1 + new_value = [index] + [Journal_information] + [Journal_information] + [formatted_date] + [unit, contact, + url] + [ + list_dois[doi_index]] + print(new_value) + body = { + 'values': [new_value] + } + + sheet.values().append( + spreadsheetId=MAIN_DOI_REGISTRY_SHEET, + range=REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE, + valueInputOption="RAW", + body=body, + ).execute() + + # Add Issue data to MAIN_DOI_REGISTRY_SHEET row by row + doi_index = 0 + for row in Issue: + index = int(index) + 1 + new_value = [index] + [row[1]] + [Journal_information] + [formatted_date] + [unit, contact, url] + [ + list_dois[doi_index]] + doi_index = doi_index + 1 + print(new_value) + body = { + 'values': [new_value] + } + + sheet.values().append( + spreadsheetId=MAIN_DOI_REGISTRY_SHEET, + range=REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE, + valueInputOption="RAW", + body=body, + ).execute() + From d24edc86615bfafb54141f495ef93f1cf31cf05d Mon Sep 17 00:00:00 2001 From: ZOEY-YuanZhou Date: Thu, 8 Aug 2024 14:55:27 -0400 Subject: [PATCH 2/3] Add doi_registration --- doi_manager.py | 1 + utils/doi_mets.py | 10 +++++++++- utils/sheets_creds_builder.py | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doi_manager.py b/doi_manager.py index 3293fc0..68e3e05 100755 --- a/doi_manager.py +++ b/doi_manager.py @@ -1,5 +1,6 @@ import sys +import os.path from utils.gsheets_manager import retrieve_doi_mets, write_doi_mets from utils.sheets_creds_builder import refresh_credentials from utils.doi_mets import JournalMetsHandler, DoiMinter diff --git a/utils/doi_mets.py b/utils/doi_mets.py index e27f1b0..f75962b 100755 --- a/utils/doi_mets.py +++ b/utils/doi_mets.py @@ -1,8 +1,16 @@ +import os.path from utils.gsheets_manager import retrieve_doi_mets from random import random import uuid from datetime import datetime -from global_settings import ALLOWED_CHARS, DEPOSITOR_NAME, DEPOSITOR_EMAIL_ADDRESS +from global_settings import ( + ALLOWED_CHARS, DEPOSITOR_NAME, DEPOSITOR_EMAIL_ADDRESS, + G_TOKEN_FILE, + MAIN_DOI_REGISTRY_SHEET, + REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE +) +from googleapiclient.discovery import build +from google.oauth2.credentials import Credentials import re diff --git a/utils/sheets_creds_builder.py b/utils/sheets_creds_builder.py index f9e0592..3144403 100755 --- a/utils/sheets_creds_builder.py +++ b/utils/sheets_creds_builder.py @@ -30,3 +30,4 @@ def refresh_credentials(): with open('doi_workflow_token.json', 'w') as token: token.write(creds.to_json()) + From 2498d39f00d59ab371a9c53a4c7afa79233cd84e Mon Sep 17 00:00:00 2001 From: ZOEY-YuanZhou Date: Fri, 20 Sep 2024 15:42:31 -0400 Subject: [PATCH 3/3] modification on gsheets_manager --- doi_manager.py | 11 ++++++++++- utils/doi_mets.py | 30 ++++++++++-------------------- utils/gsheets_manager.py | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/doi_manager.py b/doi_manager.py index 68e3e05..4459905 100755 --- a/doi_manager.py +++ b/doi_manager.py @@ -47,8 +47,17 @@ MAIN_DOI_REGISTRY_SHEET, len(issue_level_mets[8:]) ) print("Writing proposed DOIs to patron metadata sheet...") + + input_unit = sys.argv[4] if len(sys.argv) > 4 else "" + input_contact = sys.argv[5] if len(sys.argv) > 5 else "" + + if bool(input_unit) and len(input_unit) < 5: + print("Warning: Check input unit.") + + if bool(input_contact) and len(input_contact) < 5: + print("Warning: Check input contact.") write_doi_mets(sys.argv[3], dois) - DoiMinter.doi_registration(issue_level_mets, dois) + DoiMinter.doi_registration(issue_level_mets, dois, input_unit, input_contact) print("Complete.") elif sys.argv[1] == "retrieve-fda-handles": diff --git a/utils/doi_mets.py b/utils/doi_mets.py index f75962b..22ef2f1 100755 --- a/utils/doi_mets.py +++ b/utils/doi_mets.py @@ -1,5 +1,5 @@ import os.path -from utils.gsheets_manager import retrieve_doi_mets +from utils.gsheets_manager import retrieve_doi_mets, update_registrations from random import random import uuid from datetime import datetime @@ -355,10 +355,12 @@ def doi_registration(key, list_dois, unit=None, contact=None, url=None): overwriting what is there. :return: """ + Journal = key[2][0] Volume = key[5][3] Issue = key[5][4] Journal_information = f'{Journal}, {Volume} {Issue}' + Journal_url = key[5][7] # Issue Contents Metadata Issue = key[8:] @@ -382,37 +384,25 @@ def doi_registration(key, list_dois, unit=None, contact=None, url=None): # Add Journal data to MAIN_DOI_REGISTRY_SHEET doi_index = -1 index = int(index) + 1 - new_value = [index] + [Journal_information] + [Journal_information] + [formatted_date] + [unit, contact, - url] + [ - list_dois[doi_index]] + new_value = [index] + [Journal_information] + [Journal_information] + [formatted_date] + \ + [unit, contact, Journal_url] + [list_dois[doi_index]] print(new_value) body = { 'values': [new_value] } - - sheet.values().append( - spreadsheetId=MAIN_DOI_REGISTRY_SHEET, - range=REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE, - valueInputOption="RAW", - body=body, - ).execute() + update_registrations(REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE, body) # Add Issue data to MAIN_DOI_REGISTRY_SHEET row by row doi_index = 0 + for row in Issue: + print(len(row)) index = int(index) + 1 - new_value = [index] + [row[1]] + [Journal_information] + [formatted_date] + [unit, contact, url] + [ + new_value = [index] + [row[1]] + [Journal_information] + [formatted_date] + [unit, contact, row[6]] + [ list_dois[doi_index]] doi_index = doi_index + 1 print(new_value) body = { 'values': [new_value] } - - sheet.values().append( - spreadsheetId=MAIN_DOI_REGISTRY_SHEET, - range=REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE, - valueInputOption="RAW", - body=body, - ).execute() - + update_registrations(REGISTRY_TEMPLATE_TITLE_COLUMN_RANGE, body) diff --git a/utils/gsheets_manager.py b/utils/gsheets_manager.py index b439330..80fe373 100755 --- a/utils/gsheets_manager.py +++ b/utils/gsheets_manager.py @@ -14,6 +14,7 @@ METS_SERIALS_DOI_COLUMN_RANGE, METS_SERIALS_ISSUE_DOI_COLUMN_RANGE, REGISTRY_TEMPLATE_COLUMN_RANGE, + MAIN_DOI_REGISTRY_SHEET, METS_CITATIONS_TEMPLATE_RANGE, METS_AUTHORS_TEMPLATE_RANGE, SCOPES, @@ -129,7 +130,20 @@ def write_doi_mets(sheet_id, append_vals, retrieve_type="mets_main"): return response +def update_registrations(range, body): + if os.path.exists(G_TOKEN_FILE): + creds = Credentials.from_authorized_user_file(G_TOKEN_FILE) + service = build("sheets", "v4", credentials=creds) + sheet = service.spreadsheets() + response = ( + sheet.values().append( + spreadsheetId=MAIN_DOI_REGISTRY_SHEET, + range=range, + valueInputOption="RAW", + body=body, + ).execute()) + return response ### MORE SAMPLE CODE BELOW