#!/usr/bin/env nodejsscript
/* jshint esversion: 11,-W097, -W040, module: true, node: true, expr: true, undef: true *//* global echo, $, pipe, s, fetch, cyclicLoop */
const img_params= "?width=1920";
const url_api= "https://commons.wikimedia.org/w/api.php";
const url_image= "https://commons.wikimedia.org/wiki/Special:FilePath/";
import { join } from "node:path";
const path_home= $.xdg.home`Obrázky/Bing Image Of The Day/`;
const path_info= join(path_home, "images.json");
$.api()
.version("2025-04-02")
.command("pull", "Pull new/today image(s)")
.action(async function pull(){
const images= {
now: await getImagePath(0),
prev: await getImagePath(-1)
};
const paths= await downloadImages(images);
updateHTML(images);
const images_save= Object.entries(images)
.reduce((acc, [ key, { caption } ])=>
Reflect.set(acc, key, caption) && acc,
{});
convert(paths, images_save);
s.echo(JSON.stringify(images_save, null, "\t")).to(path_info);
newPanel(images_save.now);
$.exit(0);
})
.command("redraw")
.action(function redraw(){
const paths= [ "now", "prev" ].map(key=> join(path_home, `${key}.jpg`));
const images= s.cat(path_info).xargs(JSON.parse);
convert(paths, images);
newPanel(images.now);
$.exit(0);
})
.command("status")
.action(function status(){
const images= s.cat(path_info).xargs(JSON.parse);
const [ stats ]= s.ls("-l", path_info);
echo({ timestamp: stats.mtime, ...images });
$.exit(0);
})
.parse();
function newPanel(caption){
const link= `(i)`;
s.sed("-i", /\