Skip to content

Instantly share code, notes, and snippets.

@eamonnfaherty
Created November 28, 2024 17:04
Show Gist options
  • Save eamonnfaherty/da52d4950eabee28f6f638f8bde2d53e to your computer and use it in GitHub Desktop.
Save eamonnfaherty/da52d4950eabee28f6f638f8bde2d53e to your computer and use it in GitHub Desktop.
import json
import yaml
import os
from pathlib import Path
def load_manifest(manifest_path):
# Load the expanded manifest YAML file
with open(manifest_path, 'r') as f:
manifest = yaml.safe_load(f)
# Create a mapping of email to account_id
email_to_account = {
account['email'].lower(): account['account_id']
for account in manifest['accounts']
}
return email_to_account
def process_json_files(source_dir, target_dir, email_to_account):
# Create target directory if it doesn't exist
Path(target_dir).mkdir(parents=True, exist_ok=True)
# Process each JSON file in the source directory
for filename in os.listdir(source_dir):
print("looping")
if not filename.endswith('.json'):
print("looping continue")
continue
source_path = os.path.join(source_dir, filename)
print(f"looking at {source_path}")
try:
with open(source_path, 'r') as f:
json_data = json.load(f)
# Get the email address from the JSON
email = json_data.get('AccountEmailAddress', '').lower()
# Look up the account ID
if email in email_to_account:
account_id = email_to_account[email]
# Write to new file with account ID as filename
target_path = os.path.join(target_dir, f"{account_id}.json")
with open(target_path, 'w') as f:
json.dump(json_data, f, indent=2)
print(f"Processed {filename} -> {account_id}.json")
else:
print(f"Warning: No account ID found for email {email} in {filename}")
except json.JSONDecodeError:
print(f"Error: Failed to parse JSON file {filename}")
except Exception as e:
print(f"Error processing {filename}: {str(e)}")
manifest_path = 'manifest-expanded.yaml' # Update this path as needed
source_dir = 'unsorted' # Update this path as needed
target_dir = 'sorted'
try:
# Load the email to account mapping
email_to_account = load_manifest(manifest_path)
# Process all JSON files
process_json_files(source_dir, target_dir, email_to_account)
except Exception as e:
print(f"Error: {str(e)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment