testnet-laconicd-stack/cli/core.py
2024-08-13 15:06:54 +05:30

50 lines
2.0 KiB
Python

import csv
import hashlib
import json
def hash_subscriber_id(subscriber_id):
return '0x' + hashlib.sha256(subscriber_id.encode()).hexdigest()
def process_subscribers(onboarded_json, subscribers_csv, output):
# Load JSON data from the file
with open(onboarded_json, 'r') as json_file:
json_data = json.load(json_file)
# Create a dictionary mapping kyc_id to participant data
kyc_map = {participant['kyc_id']: participant for participant in json_data['participants']}
# Load subscribers data from the CSV file and process it using map
with open(subscribers_csv, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
subscribers = list(csv_reader)
# Use map to process subscribers
def process_subscriber(subscriber):
hashed_subscriber_id = hash_subscriber_id(subscriber['subscriber_id'])
participant = kyc_map.get(hashed_subscriber_id)
if participant:
return {
'subscriber_id': subscriber['subscriber_id'],
'email': subscriber['email'],
'cosmos_address': participant['cosmos_address'],
'nitro_address': participant['nitro_address'],
'role': participant['role'],
'hashed_subscriber_id': participant['kyc_id'],
'status': subscriber['status'],
'premium': subscriber['premium?'],
'created_at': subscriber['created_at']
}
return None
# Apply the map function and filter out None values
output_data = list(filter(None, map(process_subscriber, subscribers)))
# Write the matched data to a new CSV file
with open(output, 'w', newline='') as csv_file:
fieldnames = ['subscriber_id', 'email', 'cosmos_address', 'nitro_address', 'role', 'hashed_subscriber_id', 'status', 'premium', 'created_at']
csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
csv_writer.writeheader()
csv_writer.writerows(output_data)
print(f'Data has been written to {output}')