From a47a5364c496224fb6d2989c5b9e37f5f7e7497c Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Wed, 13 Sep 2023 16:40:42 +0200 Subject: [PATCH] added: pyhton cli tool --- cli/python/app.py | 75 ++++++++++++++++++++++++++++++++++ cli/python/settingssample.json | 10 +++++ 2 files changed, 85 insertions(+) create mode 100644 cli/python/app.py create mode 100644 cli/python/settingssample.json diff --git a/cli/python/app.py b/cli/python/app.py new file mode 100644 index 0000000..0390aac --- /dev/null +++ b/cli/python/app.py @@ -0,0 +1,75 @@ +import io +import json +import subprocess +import urllib.parse +import os + +def listnotes(filter = ""): + for i in reversed(range(len(data))): + if filter in data[i]["title"]: + print("[" + str(i) + "]", data[i]["title"]) + +def readtextfile(path): + with io.open(path, mode = "r", encoding = "utf-8") as f: + return f.read() + +def writetextfile(path, content): + with io.open(path, mode = "w", encoding = "utf-8") as f: + f.write(content) + +settings = json.loads(readtextfile("settings.json")) + +if os.path.isfile("data/backupdata.acs"): + os.remove("data/backupdata.acs") + +subprocess.call(["curl", "-s", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "data/backupdata.acs", settings["url"] + "/handler.php"]) +subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/backupdata.json", "--decrypt", "data/backupdata.acs"]) + +data = json.loads(readtextfile("data/backupdata.json")) + +command = "list" +while not (command == "quit" or command == "exit" or command == "q"): + + if command == "list": + listnotes() + elif command.split()[0] == "find": + searchstring = command.replace("find ", "") + listnotes(searchstring) + else: + try: + index = int(command) + note = data[index] + content = note["content"] + + writetextfile("data/backupnote.md", content) + writetextfile("data/note.md", content) + + subprocess.call(settings["commands"]["editor"] + ["data/note.md"]) + newcontent = readtextfile("data/note.md") + + if newcontent != content: + + listnotes() + subprocess.call(["diff", "--color", "data/backupnote.md", "data/note.md"]) + + note["content"] = newcontent + data.remove(note) + data.insert(0, note) + + writetextfile("data/data.json", json.dumps(data)) + + subprocess.call([settings["commands"]["gpg"], "-q", "--encrypt", "--yes", "--trust-model", "always", "--output", "data/data.acs", "--armor", "-r", settings["gpguser"], "data/data.json"]); + newdata = readtextfile("data/data.acs") + + postdata = "action=push&password=" + settings["password"] + "&data=" + urllib.parse.quote_plus(newdata) + + writetextfile("data/postdata", postdata) + + subprocess.call(["curl", "-s", "-X", "POST", "-d", "@data/postdata", settings["url"] + "/handler.php"]) + else: + print("no change") + + except: + print("error with command " + command) + + command = input("> ") \ No newline at end of file diff --git a/cli/python/settingssample.json b/cli/python/settingssample.json new file mode 100644 index 0000000..56c027e --- /dev/null +++ b/cli/python/settingssample.json @@ -0,0 +1,10 @@ +{ + "url": "http://localhost:8000", + "password": "", + "gpguser": "", + "commands": + { + "editor": ["vim"], + "gpg": "gpg" + } +} \ No newline at end of file