2023-09-13 16:40:42 +02:00
|
|
|
import io
|
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
import urllib.parse
|
|
|
|
import os
|
2023-09-13 17:10:46 +02:00
|
|
|
import sys
|
2023-09-14 11:17:24 +02:00
|
|
|
import time
|
2023-09-18 09:01:53 +02:00
|
|
|
import re
|
2023-09-22 09:57:54 +02:00
|
|
|
import shutil
|
2023-09-18 09:01:53 +02:00
|
|
|
|
|
|
|
def filename(note):
|
|
|
|
return re.sub(r'[\?\"<>|\*:\/\\]', '_', note["title"]) + ".md"
|
2023-09-13 16:40:42 +02:00
|
|
|
|
2023-09-15 12:06:50 +02:00
|
|
|
def listnotes(filter = "", checkcontent = False):
|
2023-09-14 13:37:13 +02:00
|
|
|
matching = []
|
2023-09-13 16:40:42 +02:00
|
|
|
for i in reversed(range(len(data))):
|
2023-09-18 09:01:53 +02:00
|
|
|
|
2023-09-14 14:30:04 +02:00
|
|
|
if filter.lower() in data[i]["title"].lower():
|
2023-09-13 16:40:42 +02:00
|
|
|
print("[" + str(i) + "]", data[i]["title"])
|
2023-09-14 13:37:13 +02:00
|
|
|
matching.append(data[i])
|
2023-09-18 09:01:53 +02:00
|
|
|
|
2023-09-15 12:06:50 +02:00
|
|
|
elif checkcontent and filter.lower() in data[i]["content"].lower():
|
2023-09-15 08:49:36 +02:00
|
|
|
print("[" + str(i) + "]", data[i]["title"])
|
2023-09-15 14:01:26 +02:00
|
|
|
lines = data[i]["content"].split("\n")
|
|
|
|
for j in range(len(lines)):
|
|
|
|
line = lines[j]
|
2023-09-15 08:49:36 +02:00
|
|
|
if filter.lower() in line.lower():
|
|
|
|
index = line.lower().index(filter.lower())
|
2023-09-15 14:01:26 +02:00
|
|
|
print("\t" + str(j) + ":", line[:100])
|
2023-09-15 08:49:36 +02:00
|
|
|
matching.append(data[i])
|
2023-09-18 09:01:53 +02:00
|
|
|
|
2023-09-14 13:37:13 +02:00
|
|
|
return matching
|
2023-09-13 16:40:42 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-09-13 17:10:46 +02:00
|
|
|
def editnote(note):
|
|
|
|
content = note["content"]
|
|
|
|
|
2023-09-22 09:57:54 +02:00
|
|
|
backupfilepath = "session/" + filename(note) + str(time.time())
|
2023-09-21 14:02:38 +02:00
|
|
|
writetextfile(backupfilepath, content)
|
2023-09-21 14:09:13 +02:00
|
|
|
writetextfile("session/" + filename(note), content)
|
2023-09-13 17:10:46 +02:00
|
|
|
|
2023-09-21 14:09:13 +02:00
|
|
|
subprocess.call(settings["commands"]["editor"] + ["session/" + filename(note)])
|
|
|
|
newcontent = readtextfile("session/" + filename(note) )
|
2023-09-13 17:10:46 +02:00
|
|
|
|
|
|
|
if newcontent != content:
|
2023-09-21 14:09:13 +02:00
|
|
|
subprocess.call(settings["commands"]["diff"] + [backupfilepath, "session/" + filename(note)])
|
2023-09-13 17:10:46 +02:00
|
|
|
note["content"] = newcontent
|
|
|
|
data.remove(note)
|
|
|
|
data.insert(0, note)
|
2023-09-14 11:31:21 +02:00
|
|
|
savedata()
|
2023-09-13 17:10:46 +02:00
|
|
|
|
|
|
|
else:
|
|
|
|
print("no change")
|
|
|
|
|
2023-09-14 11:31:21 +02:00
|
|
|
def savedata():
|
|
|
|
if settings["mode"] == "remote":
|
2023-09-21 14:09:13 +02:00
|
|
|
writetextfile("session/data.json", json.dumps(data))
|
|
|
|
subprocess.call([settings["commands"]["gpg"], "-q", "--encrypt", "--yes", "--trust-model", "always", "--output", "session/data.acs", "--armor", "-r", settings["gpguser"], "session/data.json"]);
|
|
|
|
newdata = readtextfile("session/data.acs")
|
2023-09-14 11:31:21 +02:00
|
|
|
postdata = "action=push&password=" + settings["password"] + "&data=" + urllib.parse.quote_plus(newdata)
|
2023-09-21 14:09:13 +02:00
|
|
|
writetextfile("session/postdata", postdata)
|
|
|
|
output = subprocess.check_output(["curl", "-X", "POST", "-d", "@session/postdata", settings["url"] + "/handler.php"]).decode("utf-8")
|
2023-09-21 08:55:22 +02:00
|
|
|
print("curl output: " + output)
|
|
|
|
if output != '{"result": "ok"}':
|
|
|
|
if ask("Save failed. Try again?"):
|
|
|
|
savedata()
|
2023-09-14 11:31:21 +02:00
|
|
|
else:
|
2023-09-21 14:09:13 +02:00
|
|
|
writetextfile("session/local.json", json.dumps(data))
|
2023-09-14 11:31:21 +02:00
|
|
|
|
2023-09-14 11:44:29 +02:00
|
|
|
def loaddata():
|
|
|
|
if settings["mode"] == "remote":
|
2023-09-21 14:02:38 +02:00
|
|
|
timestamp = str(time.time())
|
|
|
|
|
2023-09-22 09:57:54 +02:00
|
|
|
subprocess.call(["curl", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "session/data.acs", settings["url"] + "/handler.php"])
|
|
|
|
subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "session/data.json", "--decrypt", "session/data.acs"])
|
2023-09-21 14:02:38 +02:00
|
|
|
|
2023-09-21 14:09:13 +02:00
|
|
|
return json.loads(readtextfile("session/data.json"))
|
2023-09-22 09:57:54 +02:00
|
|
|
|
2023-09-14 11:44:29 +02:00
|
|
|
else:
|
2023-09-21 14:09:13 +02:00
|
|
|
return json.loads(readtextfile("session/local.json"))
|
2023-09-14 11:44:29 +02:00
|
|
|
|
2023-09-14 11:31:21 +02:00
|
|
|
def ask(question):
|
2023-09-16 15:29:46 +02:00
|
|
|
answer = input(question + " [Y/n] ")
|
|
|
|
return answer == "y" or answer == "yes" or answer == ""
|
2023-09-14 11:31:21 +02:00
|
|
|
|
2023-09-18 09:01:53 +02:00
|
|
|
def initdatapath():
|
2023-09-21 10:23:15 +02:00
|
|
|
if not os.path.exists("history"):
|
|
|
|
os.mkdir("history")
|
2023-09-21 14:09:13 +02:00
|
|
|
if os.path.exists("session"):
|
2023-09-22 09:57:54 +02:00
|
|
|
shutil.make_archive("history/session" + str(time.time()), "zip", "session")
|
|
|
|
shutil.rmtree("session")
|
2023-09-21 14:09:13 +02:00
|
|
|
os.mkdir("session")
|
2023-09-18 09:01:53 +02:00
|
|
|
|
2023-09-14 11:31:21 +02:00
|
|
|
abspath = os.path.abspath(__file__)
|
|
|
|
dname = os.path.dirname(abspath)
|
|
|
|
os.chdir(dname)
|
2023-09-13 16:40:42 +02:00
|
|
|
|
2023-09-18 09:01:53 +02:00
|
|
|
initdatapath()
|
|
|
|
settings = json.loads(readtextfile("settings.json"))
|
2023-09-14 11:44:29 +02:00
|
|
|
data = loaddata()
|
2023-09-18 09:01:53 +02:00
|
|
|
|
2023-09-14 09:02:57 +02:00
|
|
|
command = ""
|
2023-09-15 12:06:50 +02:00
|
|
|
|
2023-09-13 17:10:46 +02:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
command = sys.argv[1]
|
2023-09-14 10:07:37 +02:00
|
|
|
if command.startswith("notes://"):
|
|
|
|
command = urllib.parse.unquote(command[8:-1])
|
|
|
|
|
2023-09-13 16:40:42 +02:00
|
|
|
while not (command == "quit" or command == "exit" or command == "q"):
|
|
|
|
|
2023-09-14 16:22:49 +02:00
|
|
|
action = None
|
2023-09-14 11:31:21 +02:00
|
|
|
if command[0:3] == "rm ":
|
2023-09-14 16:22:49 +02:00
|
|
|
action = "delete"
|
|
|
|
command = command[3:]
|
|
|
|
elif command[0:3] == "mv ":
|
|
|
|
action = "rename"
|
2023-09-14 11:44:29 +02:00
|
|
|
command = command[3:]
|
2023-09-16 15:29:46 +02:00
|
|
|
elif command[0:1] == "/":
|
2023-09-15 08:49:36 +02:00
|
|
|
action = "grep"
|
2023-09-16 15:29:46 +02:00
|
|
|
command = command[1:]
|
2023-09-15 09:05:31 +02:00
|
|
|
elif command[0:4] == "sms ":
|
|
|
|
action = "sms"
|
|
|
|
command = command[4:]
|
2023-09-15 12:06:50 +02:00
|
|
|
elif command[0:7] == "export ":
|
|
|
|
action = "export"
|
|
|
|
command = command[7:]
|
2023-09-18 07:58:43 +02:00
|
|
|
elif command == "settings":
|
|
|
|
action = "settings"
|
2023-09-14 11:44:29 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
index = int(command)
|
|
|
|
note = data[index]
|
|
|
|
except:
|
|
|
|
note = next((note for note in data if note["title"] == command), None)
|
|
|
|
|
2023-09-14 16:22:49 +02:00
|
|
|
if action == "delete":
|
2023-09-14 11:44:29 +02:00
|
|
|
if note and ask("delete '" + note["title"] + "'? "):
|
|
|
|
data.remove(note)
|
2023-09-14 11:31:21 +02:00
|
|
|
savedata()
|
2023-09-14 16:22:49 +02:00
|
|
|
elif action == "rename":
|
|
|
|
if note:
|
|
|
|
newname = input("new name: ")
|
|
|
|
if newname:
|
|
|
|
note["title"] = newname
|
|
|
|
savedata()
|
2023-09-15 09:05:31 +02:00
|
|
|
elif action == "sms":
|
|
|
|
if note and ask("send '" + note["title"] + "' by sms? "):
|
|
|
|
subprocess.call(["curl", "-s", "-X", "POST", "-F", "action=sms", "-F", "password=" + settings["password"], "-F", "data=" + urllib.parse.quote_plus(note["content"]), settings["url"] + "/handler.php"])
|
2023-09-15 12:06:50 +02:00
|
|
|
elif action == "export":
|
|
|
|
if note:
|
2023-09-21 14:09:13 +02:00
|
|
|
writetextfile("session/" + note["title"] + ".md", note["content"])
|
2023-09-18 07:58:43 +02:00
|
|
|
elif action == "settings":
|
|
|
|
subprocess.call(settings["commands"]["editor"] + ["settings.json"])
|
|
|
|
settings = json.loads(readtextfile("settings.json"))
|
2023-09-15 08:49:36 +02:00
|
|
|
elif note and not action == "grep":
|
2023-09-14 11:44:29 +02:00
|
|
|
editnote(note)
|
2023-09-14 09:02:57 +02:00
|
|
|
else:
|
2023-09-15 08:49:36 +02:00
|
|
|
matching = listnotes(command, action == "grep")
|
|
|
|
if len(matching) == 0 and not action == "grep":
|
2023-09-14 11:44:29 +02:00
|
|
|
if ask("create '" + command + "'? "):
|
|
|
|
note = {
|
|
|
|
"title": command,
|
|
|
|
"content": "---\ntitle: " + command + "\ndate: " + time.strftime("%Y-%m-%d") + "\ntags: \n---\n\n"
|
|
|
|
}
|
|
|
|
data.insert(0, note)
|
|
|
|
editnote(note)
|
2023-09-14 13:37:13 +02:00
|
|
|
elif len(matching) == 1:
|
2023-09-16 15:29:46 +02:00
|
|
|
note = matching.pop()
|
|
|
|
if ask("open '" + note["title"] + "'?"):
|
|
|
|
editnote(note)
|
2023-09-13 16:40:42 +02:00
|
|
|
|
2023-09-14 09:02:57 +02:00
|
|
|
command = input("> ")
|