diff --git a/enpass-to-keepass.py b/enpass-to-keepass.py index bbd8914..4f2b152 100755 --- a/enpass-to-keepass.py +++ b/enpass-to-keepass.py @@ -12,76 +12,82 @@ import csv import json -ALLOWED_FIELDS = ['website', 'username', 'password'] -HEADERS = ['title', 'website', 'username', 'password', 'group', 'notes'] +ALLOWED_FIELDS = ['website', "username", 'password'] +HEADERS = ["title", 'website', "username", 'password', "group", "notes"] extra_keys = set([]) -def main(args): - results = [] - with open('export.json') as json_file: - data = json.load(json_file) - folders = data['folders'] - items = data['items'] - - for item in items: - results.append(processItem(item, folders)) - - if (len(results) == 0): - print('No rows to write (empty input file?)') - return - - print('%d rows processed' % len(results)) - print('Writing to %s' % args.output_file.name) - if (len(extra_keys) > 0): - print('Found extra keys: ' + ', '.join(extra_keys)) - - writer = csv.DictWriter(args.output_file, HEADERS) - writer.writeheader() - writer.writerows(results) - -def processItem(item, folders): - print('Reading item: ' + item['title']) +def process_item(item, folders): + print(f"Reading item: {item['title']}") result = { - 'title': item['title'], - 'notes': item['subtitle'] + '\n\n\n' + "title": item["title"], + "notes": item['subtitle'] + '\n\n\n' } - if item.get('folders'): + if folders and item.get("folders"): for folder in folders: - if folder['uuid'] == item['folders'][0]: - result['group'] = folder['title'] + if folder["uuid"] == item["folders"][0]: + result["group"] = folder["title"] break - email = '' - username = '' + email = "" + username = "" - for field in item.get('fields', []): - if field['label'] in result or field['label'].lower() not in ALLOWED_FIELDS: - if field['label'].lower() == 'e-mail': - email = field['value'] + for field in item.get("fields", []): + if field.get("label", "").lower() not in ALLOWED_FIELDS: + if field["label"].lower() == 'e-mail': + email = field["value"] continue - result['notes'] += "%s\n%s\n\n" % (field['label'], field['value']) - extra_keys.add(field['label']) + result["notes"] += "%s\n%s\n\n" % (field["label"], field["value"]) + extra_keys.add(field["label"]) else: - result[field['label'].lower()] = field['value'] - if field['label'].lower() == 'username': - username = field['value'] + result[field["label"].lower()] = field["value"] + if field["label"].lower() == "username": + username = field["value"] - result['notes'] += "NOTES\n\n" + item['note'] + result["notes"] += "NOTES\n\n" + item["note"] if not username and email: - result['username'] = email + result["username"] = email else: - result['notes'] += "E-mail\n" + email + result["notes"] += "E-mail\n" + email return result + +def convert_enpass_to_keypass(input_file, output_file): + with input_file as json_file: + data = json.load(json_file) + + if not data: + print("No JSON data load from {args.input_file.name}") + return + + folders = data.get("folders") + items = data.get("items") + + results = [process_item(item, folders) for item in items] + + if not results: + print("No rows to write (empty input file?)") + return + + print(f"{len(results)} rows processed") + print(f"Writing to {output_file.name}") + if extra_keys: + print(f"Found extra keys: {', '.join(extra_keys)}") + + writer = csv.DictWriter(output_file, HEADERS) + writer.writeheader() + writer.writerows(results) + + if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('input_file', type=argparse.FileType('r'), - help='Path to Enpass csv export file') + help='Path to Enpass JSON export file') parser.add_argument('output_file', type=argparse.FileType('w'), - help='Path to output file (csv)') - main(parser.parse_args()) + help='Path to output file (CSV)') + args = parser.parse_args() + convert_enpass_to_keypass(args.input_file, args.output_file)