diff --git a/doi_manager.py b/doi_manager.py index a9044d9..4459905 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 @@ -46,7 +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, input_unit, input_contact) print("Complete.") elif sys.argv[1] == "retrieve-fda-handles": @@ -61,6 +72,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..22ef2f1 100755 --- a/utils/doi_mets.py +++ b/utils/doi_mets.py @@ -1,8 +1,16 @@ -from utils.gsheets_manager import retrieve_doi_mets +import os.path +from utils.gsheets_manager import retrieve_doi_mets, update_registrations 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 @@ -339,11 +347,62 @@ 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}' + Journal_url = key[5][7] + # 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, Journal_url] + [list_dois[doi_index]] + print(new_value) + body = { + 'values': [new_value] + } + 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, row[6]] + [ + list_dois[doi_index]] + doi_index = doi_index + 1 + print(new_value) + body = { + 'values': [new_value] + } + 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 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()) +