-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (64 loc) · 2.54 KB
/
Copy pathmain.py
File metadata and controls
76 lines (64 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3
# Basic migration from Nextcloud JSON (export) to Bitwarden JSON (import)
# - folders are not supported
# - type is only 'login' (no "card" "identity" or "secure note")
#
# Bitwarden JSON format doc: https://bitwarden.com/help/export-your-data/
#
# Authors: Martin Monperrus, Paul Lardet
# Original URL: https://gist.github.com/monperrus/c671817d53f6fc4bfe8d1773f28262d7
# New repository: https://github.com/godisopensource/np2bw/
# License: Public domain
import csv
import json
from datetime import datetime, timezone
def csv_to_json(csv_file_path, json_file_path):
with open(csv_file_path, 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
data = list(reader)
with open(json_file_path, 'w', encoding='utf-8') as jsonfile:
json.dump(data, jsonfile, indent=4, ensure_ascii=False)
csv_file_path = "/Users/paul/Downloads/np.csv" # Put CSV database from Nextcloud Passwords
json_file_path = "/Users/paul/Downloads/nextcloudpasswords.json" # Json converted file will be here
csv_to_json(csv_file_path, json_file_path)
with open(json_file_path, "r", encoding='utf-8') as file:
kp = json.load(file)
def entry_to_bw_json(entry):
current_time = datetime.now(timezone.utc).isoformat()
return {
"passwordHistory": None,
"revisionDate": current_time,
"creationDate": current_time,
"deletedDate": None,
"id": entry.get("Id", ""),
"organizationId": None,
"folderId": entry.get("ID du dossier", None),
"type": 1,
"reprompt": 0,
"name": entry.get("Libéllé", ""),
"notes": entry.get("Notes") or None,
"favorite": entry.get("Favori", "").lower() == "vrai",
"login": {
"fido2Credentials": [],
"uris": [{"match": None, "uri": entry.get("Lien", "")}] if entry.get("Lien") else [],
"username": entry.get("Nom d’utilisateur", ""),
"password": entry.get("Mot de passe", ""),
"totp": None
},
"collectionIds": None
}
def export(kp):
items = [entry_to_bw_json(i) for i in kp]
data = {
"encrypted": False,
"folders": [],
"items": items
}
print(f'Imported {len(items)} elements')
try:
with open('bitwarden_import.json', 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print(f"Successfully saved file to bitwarden_import.json")
except Exception as e:
print('An error occurred while trying to save json file:', e)
export(kp)