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() 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 os
import json
import osproc import osproc
import random
import strutils import strutils
import sequtils import sequtils
import random import httpclient
import ../common import ../common
import ../common/http
import ../parser import ../parser
import ../output import ../output
import ../notify
var UNSPLASH_KEY = "" var UNSPLASH_KEY = ""
var BG_DIR = "/tmp/" var BG_DIR = "/tmp/"
var LAST_FILE = "" var LAST_FILE = ""
var LAST = "" var LAST = ""
var GET_FROM_UNSPLASH = false
const UNSPLASH_URL = "https://api.unsplash.com/photos/random?query=$QUERY&orientation=landscape" 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 = proc getFromUnsplash(q: var string): string =
createDir(BG_DIR & "/unsplash") let dir = BG_DIR & "/unsplash/" & q & "/"
echo "Getting from Unsplash" createDir(dir)
notify.send("Getting from Unsplash")
q = q.replace(" ","%20") q = q.replace(" ","%20")
let uri = UNSPLASH_URL.replace("$QUERY",q) 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] = proc getFiles(dir: string): seq[string] =
var files: seq[string] = @[] var files: seq[string] = @[]
@ -59,21 +51,27 @@ proc getFiles(dir: string): seq[string] =
proc getLast() = proc getLast() =
LAST = readFile(LAST_FILE).strip() LAST = readFile(LAST_FILE).strip()
proc setLastFileName(file: string) =
writeFile(LAST_FILE, file)
LAST = file
proc getImageFromDir(): string = 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) var img_files = getFiles(BG_DIR).filter(proc(f: string): bool = f != LAST)
randomize()
img_files.shuffle() img_files.shuffle()
let img_file = img_files[0] 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() = proc setLast() =
var n: Note = newNote() notify.send("Setting Background to Last", LAST)
n.title = "Setting Background to Last"
n.content = LAST
n.send()
let feh = "feh --bg-fill " & LAST.escape let feh = "feh --bg-fill " & LAST.escape
echo feh
discard execCmdEx(feh) discard execCmdEx(feh)
proc getDesign(): Info = proc getDesign(): Info =
@ -90,18 +88,23 @@ proc go*() =
UNSPLASH_KEY = myConfig.unsplash_key UNSPLASH_KEY = myConfig.unsplash_key
BG_DIR = myConfig.bg_dir BG_DIR = myConfig.bg_dir
LAST_FILE = BG_DIR & "/last.txt" LAST_FILE = BG_DIR & "/last.txt"
getLast()
if args.from_unsplash and args.query == "":
args.query = queryPrompt()
var img = "" var img = ""
if args.query != "" or args.from_unsplash: if args.query != "" or args.from_unsplash:
if args.query == "":
args.query = queryPrompt()
echo "Query: ", args.query echo "Query: ", args.query
img = getFromUnsplash(args.query) img = getFromUnsplash(args.query)
elif args.last: elif args.last:
getLast()
setLast() setLast()
quit(0)
else: else:
img = getImageFromDir() img = getImageFromDir()
echo img setImage(img)
setLastFilename(img)