From c897a3b07c6b352b2a1ca21db0b05dc51faddb4b Mon Sep 17 00:00:00 2001 From: severak Date: Sun, 2 Jan 2022 21:42:08 +0100 Subject: [PATCH] First version of command line app. --- kyselo.nim | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 kyselo.nim diff --git a/kyselo.nim b/kyselo.nim new file mode 100644 index 0000000..2426f6a --- /dev/null +++ b/kyselo.nim @@ -0,0 +1,50 @@ +# Kyselo companion tool +import std/parseopt +import std/json +import std/httpclient +import std/os +import std/uri + +proc writeHelp() = + echo """Kyselo companion v 0.1 + +possible commands: + +-D, --download-images-for=backup-someone.jsonl +""" + quit(QuitSuccess) + +proc writeVersion() = + echo "0.1" + quit(QuitSuccess) + +proc downloadImages(backupFile : string) = + var client = newHttpClient() + echo "Backing up images from ", backupFile, "..." + for line in backupFile.lines: + let post = parseJson(line) + try: + let url = post["url"].getStr() + let target = getCurrentDir() & url + if not fileExists(target): + echo "downloading ", url + var (dir, name, ext) = splitFile(target) + createDir(dir) + let imgUri = parseUri("https://kyselo.eu") / url + client.downloadFile(imgUri, target) + except KeyError: + discard + ## neřešíme + quit(QuitSuccess) + +for kind, key, val in getopt(): + case kind + of cmdLongOption, cmdShortOption: + case key + of "help", "h": writeHelp() + of "version", "v": writeVersion() + of "download-images-for", "D": downloadImages(val) + of cmdArgument: discard + of cmdEnd: assert(false) # cannot happen + +writeHelp() \ No newline at end of file