From de61baf81626e185acd7b981271cd3ab0d8332be Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Wed, 13 Dec 2023 15:10:50 +0000 Subject: [PATCH] wallpapuur working --- src/common.nim | 1 - src/common/http.nim | 14 +++++++ src/notify.nim | 38 ++++++++++++++++++ src/util/wallpapurr.nim | 85 +++++++++++++++++++++-------------------- 4 files changed, 96 insertions(+), 42 deletions(-) create mode 100644 src/common/http.nim create mode 100644 src/notify.nim diff --git a/src/common.nim b/src/common.nim index 51af888..7936d3d 100644 --- a/src/common.nim +++ b/src/common.nim @@ -8,4 +8,3 @@ export info var myConfig* = newConfig() - diff --git a/src/common/http.nim b/src/common/http.nim new file mode 100644 index 0000000..b59421f --- /dev/null +++ b/src/common/http.nim @@ -0,0 +1,14 @@ +import httpclient + +proc download*(link: string): string = + var client = newHttpClient(timeout = 10000) + try: + let resp = client.get(link) + if resp.status == $Http200: + return resp.body + except: + echo getCurrentExceptionMsg() + return "" + +proc save*(file: string, content: string) = + writeFile(file,content) diff --git a/src/notify.nim b/src/notify.nim new file mode 100644 index 0000000..1b5ffc5 --- /dev/null +++ b/src/notify.nim @@ -0,0 +1,38 @@ +import strutils +import osproc + +type + Note* = object + urgency*: Urgency + title*: string + content*: string + timeout*: int + Urgency* = enum + Normal = "normal" + Low = "low" + Urgent = "urgent" + Critical = "critical" + +proc newNote*(): Note = + return Note(urgency: Normal, title: "Notification", content: "Hello, I am a notifications", timeout: 2000) + +proc `$`*(n: Note): string = + let str = "notify-send -u $U $T $C -t $N" + .replace("$U", $n.urgency) + .replace("$T", n.title.escape) + .replace("$C", n.content.escape) + .replace("$N", $n.timeout) + return str + +proc send*(n: Note) = + discard execCmdEx($n) + +proc send*(s: varargs[string,`$`]) = + var n = newNote() + n.title = s[0] + if s.len > 1: + n.content = s[1..^1].join("") + else: + n.content = "" + send(n) + diff --git a/src/util/wallpapurr.nim b/src/util/wallpapurr.nim index 1b73ccb..a1b375f 100644 --- a/src/util/wallpapurr.nim +++ b/src/util/wallpapurr.nim @@ -1,52 +1,44 @@ import os +import json import osproc +import random import strutils import sequtils -import random +import httpclient import ../common +import ../common/http import ../parser import ../output +import ../notify var UNSPLASH_KEY = "" var BG_DIR = "/tmp/" var LAST_FILE = "" var LAST = "" -var GET_FROM_UNSPLASH = false const UNSPLASH_URL = "https://api.unsplash.com/photos/random?query=$QUERY&orientation=landscape" -type - Note* = object - urgency*: Urgency - title*: string - content*: string - timeout*: int - Urgency* = enum - Normal = "normal" - Low = "low" - Urgent = "urgent" - Critical = "critical" - -proc newNote*(): Note = - return Note(urgency: Normal, title: "Notification", content: "Hello, I am a notifications", timeout: 2000) - -proc `$`*(n: Note): string = - let str = "notify-send -u $U $T $C -t $N" - .replace("$U", $n.urgency) - .replace("$T", n.title.escape) - .replace("$C", n.content.escape) - .replace("$N", $n.timeout) - return str - -proc send(n: Note) = - discard execCmdEx($n) - proc getFromUnsplash(q: var string): string = - createDir(BG_DIR & "/unsplash") - echo "Getting from Unsplash" + let dir = BG_DIR & "/unsplash/" & q & "/" + createDir(dir) + notify.send("Getting from Unsplash") q = q.replace(" ","%20") let uri = UNSPLASH_URL.replace("$QUERY",q) + var client = newHttpClient(timeout = 10000) + let id = "Client-ID " & UNSPLASH_KEY + client.headers = newHttpHeaders({"Authorization": id}) + try: + let resp = client.get(uri) + if resp.status == $Http200: + let j = parseJson(resp.body) + let link = j["links"]["download"].getStr + let filename = dir & j["slug"].getStr & ".jpg" + let img = download(link) + filename.save(img) + return filename + except: + echo getCurrentExceptionMsg() proc getFiles(dir: string): seq[string] = var files: seq[string] = @[] @@ -59,21 +51,27 @@ proc getFiles(dir: string): seq[string] = proc getLast() = LAST = readFile(LAST_FILE).strip() +proc setLastFileName(file: string) = + writeFile(LAST_FILE, file) + LAST = file + proc getImageFromDir(): string = - echo "Getting Random file from " & BG_DIR + notify.send("Getting Random file from:",BG_DIR) var img_files = getFiles(BG_DIR).filter(proc(f: string): bool = f != LAST) + randomize() img_files.shuffle() let img_file = img_files[0] + notify.send("Found : ", img_file) + return img_file - echo "Found : ", img_file +proc setImage(img: string) = + notify.send("Setting Background to:",img) + let feh = "feh --bg-fill " & img.escape + discard execCmdEx(feh) proc setLast() = - var n: Note = newNote() - n.title = "Setting Background to Last" - n.content = LAST - n.send() + notify.send("Setting Background to Last", LAST) let feh = "feh --bg-fill " & LAST.escape - echo feh discard execCmdEx(feh) proc getDesign(): Info = @@ -90,18 +88,23 @@ proc go*() = UNSPLASH_KEY = myConfig.unsplash_key BG_DIR = myConfig.bg_dir LAST_FILE = BG_DIR & "/last.txt" - getLast() + + if args.from_unsplash and args.query == "": + args.query = queryPrompt() + var img = "" if args.query != "" or args.from_unsplash: - if args.query == "": - args.query = queryPrompt() echo "Query: ", args.query img = getFromUnsplash(args.query) elif args.last: + getLast() setLast() + quit(0) else: img = getImageFromDir() - echo img + setImage(img) + setLastFilename(img) +