Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cong-min/f8e1134df234a7e102d1ac788d11ab17 to your computer and use it in GitHub Desktop.
Save cong-min/f8e1134df234a7e102d1ac788d11ab17 to your computer and use it in GitHub Desktop.
🏞 Bing Daily Wallpaper
Fetch Bing Daily Wallpaper by Node.js & PHP.
e.g. https://congm.in/bing.php
<?php
$str = file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
$data = json_decode($str);
$imghost = 'https://www.bing.com';
$imgpath = $data -> {"images"}[0] -> {"url"};
if ($imgpath) {
$imgurl = $imghost . $imgpath;
header('Location:' . $imgurl);
exit();
} else {
exit('error');
}
?>
const http = require('http');
http.createServer((req, response) => {
const today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);
const tomorrow = new Date();
tomorrow.setTime(today.getTime() + (24 * 3600 * 1000));
response.writeHead(200, {
'Expires': tomorrow.toUTCString(),
'Cache-Control': 'public, max-age=3600',
'Last-Modified': today.toUTCString(),
'Content-Type': 'image/jpeg'
});
http.get('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1', (bing_res) => {
let bing_body = [], bing_data = {};
bing_res.on('data', (chunk) => {
bing_body.push(chunk);
});
bing_res.on('end', () => {
bing_body = Buffer.concat(bing_body);
bing_data = JSON.parse(bing_body.toString());
http.get(`https://www.bing.com${bing_data.images[0].url}`, (img_res) => {
let img_body = [];
img_res.on('data', (chunk) => {
img_body.push(chunk);
});
img_res.on('end', () => {
img_body = Buffer.concat(img_body);
response.write(img_body, 'binary');
response.end();
});
});
});
});
}).listen(8129);
<?php
$str = file_get_contents('https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');
$data = json_decode($str);
$imghost = 'https://www.bing.com';
$imgpath = $data -> {"images"}[0] -> {"url"};
if ($imgpath) {
$imgurl = $imghost . $imgpath;
$img = imagecreatefromjpeg($imgurl);
header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime(date('Y-m-d', strtotime('+1 day')))) . ' GMT');
header('Cache-Control: public, max-age=3600');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', strtotime(date('Y-m-d'))) . ' GMT');
header('Content-Type: image/jpeg');
imageinterlace($img, 1);
imagejpeg($img);
imagedestroy($img);
} else {
exit('error');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment