Commit 4589f14b authored by ZettaScript's avatar ZettaScript

Adapted for LevelDB

parent 3692b97b
......@@ -12,7 +12,11 @@ Une partie de duniter.db est exportée en JSON par `gencache.py` (opération fai
## Installation
Générez le cache JSON à partir de duniter.db (il vous faut un serveur Duniter pour cela) (et python3-sqlite3) :
Installer python3-plyvel :
sudo pip3 install plyvel
Générez le cache JSON à partir de duniter.db (il vous faut un serveur Duniter arrêté pour cela) :
python3 gencache.py -d /chemin/vers/duniter.db -e
......
#!/usr/bin/env python3
"""
Dependances: python-sqlite3
Dependances: python3-plyvel
Licence CC-0 (Public domain)
Compatible with WorldWotMap v0.1.0
Compatible with Duniter >=1.7.9 (with LevelDB)
"""
import math, sqlite3, sys, os, json
import math, plyvel, sys, os, json
def getargv(argv, arg, default=""):
if arg in argv and len(argv) > argv.index(arg)+1:
......@@ -13,47 +13,42 @@ def getargv(argv, arg, default=""):
else:
return default
def getCertsList(db):
certs = []
c = db.cursor()
c.execute("SELECT * FROM `c_index`")
while True:
data = c.fetchone()
if not data:
break
certs.append([data[1], data[2], data[6], data[7], data[11]])
return certs
def getIdentitiesList(db):
ids = {}
c = db.cursor()
c.execute("SELECT * FROM `i_index`")
while True:
data = c.fetchone()
if not data:
break
ids[data[2]] = [data[1], data[2], data[7], data[11]]
return ids
def exportJSON(outfile, data):
f = open(outfile, "w")
f.write(json.dumps(data))
json.dump(data, f)
f.close()
if __name__ == "__main__":
if "--help" in sys.argv:
print("""Duniter DB extractor
Licence CC0
Duniter must not be running while extracting from LevelDB datas.
Options:
-d <path> Change duniter.db path
default: ~/.config/duniter/duniter_default/duniter.db
default: ~/.config/duniter/duniter_default/data/leveldb
-e <path> Export db in JSON to <path>
default: duniterdb.json
""")
exit()
dbpath = os.path.expanduser(getargv(sys.argv, "-d", "~/.config/duniter/duniter_default/duniter.db"))
db = sqlite3.connect(dbpath)
result = {"certs":[], "ids":{}}
dbpath = os.path.expanduser(getargv(sys.argv, "-d", "~/.config/duniter/duniter_default/data/leveldb"))
iindex = plyvel.DB(dbpath + "/level_iindex")
cindex = plyvel.DB(dbpath + "/level_cindex")
for pub, row in iindex:
idty = json.loads(row.decode())[0]
certs = json.loads(cindex.get(pub).decode())
result["ids"][idty["pub"]] = [idty["uid"], idty["pub"], idty["member"], idty["writtenOn"]]
for cert in certs["issued"]:
if cert["expired_on"] == 0:
result["certs"].append([idty["pub"], cert["receiver"], cert["expires_on"], cert["expired_on"], cert["writtenOn"]])
iindex.close()
cindex.close()
if "-e" in sys.argv:
exportJSON(getargv(sys.argv, "-e", "duniterdb.json"), {"certs":getCertsList(db), "ids":getIdentitiesList(db)})
exportJSON(getargv(sys.argv, "-e", "duniterdb.json"), result)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment