Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doi_manager.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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")
Expand Down
67 changes: 63 additions & 4 deletions utils/doi_mets.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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:
"""
"""

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)
14 changes: 14 additions & 0 deletions utils/gsheets_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions utils/sheets_creds_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ def refresh_credentials():
with open('doi_workflow_token.json', 'w') as token:
token.write(creds.to_json())