Skip to content

Instantly share code, notes, and snippets.

@mrlesmithjr
Created May 25, 2021 02:47
Show Gist options
  • Save mrlesmithjr/c7303c1c2986014c041854733ef8d92e to your computer and use it in GitHub Desktop.
Save mrlesmithjr/c7303c1c2986014c041854733ef8d92e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
import glob
from datetime import datetime
import csv
src = "./"
date = datetime.now()
files = glob.glob(f"{src}*.json", recursive=True)
def write(filename, data):
with open(filename, "w") as f:
writer = csv.writer(f)
writer.writerows(data)
for file in files:
data = {
"logins": [],
"collections": {},
"secure_notes": [],
"secure_note_collections": {},
"other": [],
}
with open(file, "r") as f:
json_data = json.load(f)
collections = json_data.get("collections")
items = json_data["items"]
folders = json_data.get("folders")
if collections is not None:
collection_mapping = {}
for item in collections:
collection_name = item["name"].replace(" ", "_")
collection_mapping[item["id"]] = collection_name
data["collections"][collection_name] = []
data["secure_note_collections"][collection_name] = []
else:
pass
for item in items:
title = item["name"]
note = item["notes"]
if item["type"] == 1:
username = item["login"]["username"]
password = item["login"]["password"]
uris = item["login"].get("uris")
if uris is not None and uris != []:
url = uris[0]["uri"]
else:
url = ""
if item["collectionIds"] is None:
data["logins"].append([title, url, username, password, note])
else:
for id in item["collectionIds"]:
data["collections"][collection_mapping[id]].append(
[title, url, username, password, note]
)
elif item["type"] == 2:
if item["collectionIds"] is None:
data["secure_notes"].append([title, note])
else:
for id in item["collectionIds"]:
data["secure_note_collections"][collection_mapping[id]].append(
[title, note]
)
for key, value in data.items():
if key == "logins":
value.insert(0, ["Title", "URL", "Username", "Password", "Note"])
filename = f"{file}.{key}.csv"
write(filename, value)
elif key == "collections":
for col_name, col_data in value.items():
col_data.insert(0, ["Title", "URL", "Username", "Password", "Note"])
filename = f"{file}.{key}.{col_name}.csv"
write(filename, col_data)
elif key == "secure_notes":
value.insert(0, ["Title", "Note"])
filename = f"{file}.{key}.csv"
write(filename, value)
elif key == "secure_note_collections":
for col_name, col_data in value.items():
col_data.insert(0, ["Title", "Note"])
filename = f"{file}.{key}.{col_name}.csv"
write(filename, col_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment