2024-02-19 20:37:09 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/* jshint esversion: 6,-W097, -W040, browser: true, expr: true, undef: true */
|
|
|
|
import { readFileSync, writeFileSync, createWriteStream } from "fs";
|
|
|
|
import { get } from "https";
|
|
|
|
import { homedir } from "os";
|
|
|
|
|
|
|
|
const url_main= "https://www.bing.com";
|
|
|
|
const folder= homedir()+"/Obrázky/Bing Image Of The Day/";
|
|
|
|
|
|
|
|
get_(url_main+"/HPImageArchive.aspx?format=js&idx=0&n=2&mkt=cs-CZ")
|
|
|
|
.then(res=> {
|
2024-02-29 15:12:14 +01:00
|
|
|
let body= "";
|
|
|
|
res.on("data", chunk=> body+= chunk);
|
|
|
|
res.on("end", ()=> pipe(data, update)(body));
|
2024-02-19 20:37:09 +01:00
|
|
|
})
|
|
|
|
.catch(e=> console.error(String(e)));
|
|
|
|
|
|
|
|
function update(data){
|
2024-02-29 15:12:14 +01:00
|
|
|
if(data===null) return false;
|
|
|
|
Promise.allSettled(data.map(({ url, copyright }, id)=> getImage_(url, id ? "prev" : "now", copyright)))
|
|
|
|
.then(res=> {
|
|
|
|
let template= readFileSync(folder+"index_template.html").toString();
|
|
|
|
writeFileSync(folder+"index.html", res.reduce((out, { status, value })=> status==="rejected" ? out : out.replace(`::${value.name}::`, value.description), template));
|
|
|
|
})
|
|
|
|
.catch(e=> console.error(String(e)));
|
2024-02-19 20:37:09 +01:00
|
|
|
}
|
|
|
|
function getImage_(url, name, desc){
|
2024-02-29 15:12:14 +01:00
|
|
|
const description= desc.replace("(©", "<br>(©");
|
|
|
|
return get_(url_main+url)
|
|
|
|
.then(res=> {
|
|
|
|
const fs= createWriteStream(folder+name+'.jpg');
|
|
|
|
res.pipe(fs);
|
|
|
|
return new Promise(res=> fs.on("finish", ()=> {
|
|
|
|
fs.close();
|
|
|
|
res({ name, description });
|
|
|
|
}));
|
|
|
|
});
|
2024-02-19 20:37:09 +01:00
|
|
|
}
|
|
|
|
function data(body){
|
2024-02-29 15:12:14 +01:00
|
|
|
try { return JSON.parse(body).images; }
|
|
|
|
catch (_) { return null; }
|
2024-02-19 20:37:09 +01:00
|
|
|
}
|
|
|
|
function get_(url){ return new Promise(function(res,rej){ get(url, res).on("error", rej); }); }
|
|
|
|
function pipe(...f){ return Array.prototype.reduce.bind(f, (acc, f)=> f(acc)); }
|