First version of command line app.
This commit is contained in:
parent
fb4d9ecda2
commit
c897a3b07c
|
@ -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()
|
Loading…
Reference in New Issue