87 lines
2.5 KiB
TypeScript
Executable file
87 lines
2.5 KiB
TypeScript
Executable file
#!/bin/env -S deno run -A
|
|
|
|
import * as path from "https://deno.land/std@0.138.0/path/mod.ts"
|
|
|
|
main()
|
|
async function main() {
|
|
const img_url = await find_daily_xkcd()
|
|
const tmp_dir = module_dir(import.meta) + "/tmp"
|
|
const dl_path = `${tmp_dir}/downloaded.png`
|
|
const bg_path = `${tmp_dir}/gen_${Date.now()}.png`
|
|
const curr_path = `${tmp_dir}/latest.png`
|
|
log("parameters", { img_url, tmp_dir, dl_path, bg_path, curr_path })
|
|
await run("mkdir", "-p", tmp_dir)
|
|
await download_file(img_url, dl_path)
|
|
await generate_wallpaper(dl_path, bg_path)
|
|
await set_wallpaper(bg_path)
|
|
await run("cp", bg_path, curr_path)
|
|
log("done")
|
|
}
|
|
|
|
async function find_daily_xkcd() {
|
|
const res = await fetch("https://xkcd.com/info.0.json")
|
|
const { img } = await res.json()
|
|
return img
|
|
}
|
|
|
|
async function download_file(url: string, dl_path: string) {
|
|
await run("wget", url, "-O", dl_path)
|
|
}
|
|
|
|
async function generate_wallpaper(src: string, dest: string) {
|
|
const [w, h] = await get_max_res()
|
|
await run(
|
|
"magick",
|
|
src,
|
|
...["-gravity", "Center"],
|
|
...["-extent", `${w}x${h}`],
|
|
...["-background", "#ffffff"],
|
|
...["-negate"],
|
|
...["-level", "-50"],
|
|
...["-brightness-contrast", "14x1"],
|
|
dest,
|
|
)
|
|
}
|
|
|
|
async function set_wallpaper(bg_path: string) {
|
|
const abs_path = path.resolve(bg_path)
|
|
await run(
|
|
"gsettings",
|
|
"set",
|
|
"org.cinnamon.desktop.background",
|
|
"picture-uri",
|
|
`file://${abs_path}`,
|
|
)
|
|
}
|
|
|
|
async function get_max_res() {
|
|
const output = await run("xrandr")
|
|
const stdout = (new TextDecoder()).decode(output.stdout)
|
|
const resolutions = [] as [number, number][]
|
|
for (const line of stdout.split("\n")) {
|
|
const [_, rest] = line.split(" connected ")
|
|
if (rest === undefined) continue
|
|
const stripped = rest.replace("primary", "")
|
|
const [res] = stripped.split("+")
|
|
const [w, h] = res.split("x").map((n) => parseInt(n))
|
|
resolutions.push([w, h])
|
|
}
|
|
const maxes = [0, 0] as [number, number]
|
|
for (const res of resolutions) {
|
|
maxes[0] = Math.max(res[0], maxes[0])
|
|
maxes[1] = Math.max(res[1], maxes[1])
|
|
}
|
|
return maxes
|
|
}
|
|
|
|
function log(...args: unknown[]) {
|
|
console.log("[download.ts]", ...args)
|
|
}
|
|
|
|
async function run(name: string, ...args: string[]) {
|
|
return await new Deno.Command(name, { args, stdout: "piped", stderr: "inherit" }).output()
|
|
}
|
|
|
|
function module_dir({ url }: ImportMeta): string {
|
|
return path.resolve(path.dirname(path.fromFileUrl(url)))
|
|
}
|