Last active
March 5, 2021 20:22
-
-
Save StephenBrown2/06812f3b1dfb14e9a5712378c61e3930 to your computer and use it in GitHub Desktop.
Condition LastPass Export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import csv | |
import html | |
import sys | |
from pathlib import Path | |
if len(sys.argv) != 2: | |
print(f"Usage:\n\t{sys.argv[0]} [lastpass_export.csv]") | |
sys.exit(1) | |
lp = Path(sys.argv[1]) | |
large = lp.parent / (lp.stem + "_large" + lp.suffix) | |
values = [] | |
large_values = [] | |
for entry in csv.DictReader(lp.open("r")): | |
# Expected headers: url,username,password,extra,name,grouping,fav | |
del entry["grouping"] # No folders! | |
for k, v in entry.items(): | |
if k == "url": | |
entry[k] = v | |
else: | |
entry[k] = html.unescape(v) | |
# As a rule of thumb, character counts will grow between 30-50% when encrypted. | |
if (len(entry["password"]) * 1.4) > 1000: | |
print(f"Warning! password for {entry['url']} is too long. Moving to 'extra' field.") | |
if entry["extra"]: | |
entry["extra"] = "\n".join(entry["password"], entry["extra"]) | |
else: | |
entry["extra"] = entry["password"] | |
del entry["password"] | |
if (len(entry["extra"]) * 1.4) > 10000: | |
print(f"Error! 'extra' entry for {entry['url']} is too long. You may need to upload it as an attachment.") | |
large_values.append(entry) | |
print(f"Added entry to {large}") | |
continue | |
values.append(entry) | |
with lp.open("w") as f: | |
writer = csv.DictWriter(f, fieldnames=list(values[0].keys())) | |
writer.writeheader() | |
for v in values: | |
writer.writerow(v) | |
if large_values: | |
with large.open("w") as f: | |
writer = csv.DictWriter(f, fieldnames=list(large_values[0].keys())) | |
writer.writeheader() | |
for v in large_values: | |
writer.writerow(v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment