From 0abebaebedfeb2ac35c950a09203ad56cf96053e Mon Sep 17 00:00:00 2001 From: laptopsftw Date: Wed, 27 Oct 2021 08:06:18 +0000 Subject: [PATCH] First Commit Checks if file ends in *.json, will be processed as json, else *.csv --- internetarchive/cli/ia_metadata.py | 8 ++++++-- internetarchive/cli/ia_upload.py | 10 +++++++--- internetarchive/utils.py | 5 +++++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/internetarchive/cli/ia_metadata.py b/internetarchive/cli/ia_metadata.py index 7e9e38b5..638ad713 100644 --- a/internetarchive/cli/ia_metadata.py +++ b/internetarchive/cli/ia_metadata.py @@ -66,6 +66,7 @@ from internetarchive.cli.argparser import get_args_dict, get_args_dict_many_write,\ get_args_header_dict from internetarchive.exceptions import ItemLocateError +from internetarchive.utils import (is_json) # Only import backports.csv for Python2 (in support of FreeBSD port). PY2 = sys.version_info[0] == 2 @@ -270,8 +271,11 @@ def main(argv, session): if args['--spreadsheet']: if not args['--priority']: args['--priority'] = -5 - with io.open(args['--spreadsheet'], 'rU', newline='', encoding='utf-8') as csvfp: - spreadsheet = csv.DictReader(csvfp) + with io.open(args['--spreadsheet'], 'rU', newline='', encoding='utf-8') as fp: + if is_json(args['--spreadsheet']): + spreadsheet = json.load(fp) + else: + spreadsheet = csv.DictReader(fp) responses = [] for row in spreadsheet: if not row['identifier']: diff --git a/internetarchive/cli/ia_upload.py b/internetarchive/cli/ia_upload.py index 9eab9686..74cb2a1d 100644 --- a/internetarchive/cli/ia_upload.py +++ b/internetarchive/cli/ia_upload.py @@ -78,7 +78,7 @@ from internetarchive.cli.argparser import get_args_dict, convert_str_list_to_unicode from internetarchive.session import ArchiveSession from internetarchive.utils import (InvalidIdentifierException, get_s3_xml_text, - is_valid_metadata_key, validate_s3_identifier) + is_valid_metadata_key, validate_s3_identifier, is_json) # Only import backports.csv for Python2 (in support of FreeBSD port). PY2 = sys.version_info[0] == 2 @@ -260,8 +260,12 @@ def main(argv, session): # Bulk upload using spreadsheet. else: # Use the same session for each upload request. - with io.open(args['--spreadsheet'], 'rU', newline='', encoding='utf-8') as csvfp: - spreadsheet = csv.DictReader(csvfp) + + with io.open(args['--spreadsheet'], 'rU', newline='', encoding='utf-8') as fp: + if is_json(args['--spreadsheet']): + spreadsheet = json.load(fp) + else: + spreadsheet = csv.DictReader(fp) prev_identifier = None for row in spreadsheet: for metadata_key in row: diff --git a/internetarchive/utils.py b/internetarchive/utils.py index aaea022d..51f263e0 100644 --- a/internetarchive/utils.py +++ b/internetarchive/utils.py @@ -398,3 +398,8 @@ def merge_dictionaries(dict0, dict1, keys_to_drop=None): # Items from `dict1` take precedence over items from `dict0`. new_dict.update(dict1) return new_dict + + +def is_json(filename): + # Checks whether filetype is .json, else fallsback to .csv file format + return bool(re.fullmatch('^.*\.json$', filename))