Skip to content

Instantly share code, notes, and snippets.

@altbodhi
Last active March 4, 2023 13:07
Show Gist options
  • Save altbodhi/321b4f0bcc3756edfc3c4a659942eb38 to your computer and use it in GitHub Desktop.
Save altbodhi/321b4f0bcc3756edfc3c4a659942eb38 to your computer and use it in GitHub Desktop.
F# script for xfce wallpaper picture of day
#r "nuget: FSharp.Data, 5.0.2"
open System
open System.Diagnostics
open FSharp.Data
open System.IO
let [<Literal>] feedUrl = "https://peapix.com/bing/feed"
type BingoImage = JsonProvider<feedUrl>
let out = Path.Combine(__SOURCE_DIRECTORY__, "bing")
Directory.CreateDirectory(out)
let image =
BingoImage.Load feedUrl
|> Array.tryFind (fun e -> File.Exists(Path.Combine(out,Path.GetFileName(e.ImageUrl))) |> not)
let monitor =
let ps = Process(StartInfo = ProcessStartInfo(FileName = "xrandr", RedirectStandardOutput = true))
if ps.Start() |> not then printfn "xrandr not started"; exit 0
let lines = ps.StandardOutput.ReadToEnd().Split('\n')
match lines |> Array.tryFind(fun e -> e.Contains(" connected ")) with
|None -> failwith "Monitor not found."
|Some x -> x.Split(' ').[0]
let prop = $"/backdrop/screen0/monitor{monitor}/workspace0/last-image"
match image with
| None -> printfn "No new images."; exit 0
| Some img ->
match Http.Request(img.ImageUrl).Body with
| Binary bytes ->
printfn "Image: %A" img
let fileName = Path.Combine(out,Path.GetFileName(img.ImageUrl))
File.WriteAllBytes(fileName, bytes)
Process.Start ("xfconf-query", $"-c xfce4-desktop -p {prop} -s \"{fileName}\"")
|> ignore
| Text text -> printfn "Error: %s" text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment