wallpapuur working

This commit is contained in:
Paul Wilde 2023-12-13 15:10:50 +00:00
parent d26f5d3ba6
commit de61baf816
4 changed files with 96 additions and 42 deletions

View file

@ -8,4 +8,3 @@ export info
var myConfig* = newConfig()

14
src/common/http.nim Normal file
View file

@ -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)

38
src/notify.nim Normal file
View file

@ -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)

View file

@ -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)