Skip to content

Instantly share code, notes, and snippets.

@phosphoer
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phosphoer/19a059f9d36b7896a4e1 to your computer and use it in GitHub Desktop.
Save phosphoer/19a059f9d36b7896a4e1 to your computer and use it in GitHub Desktop.
Bing Wallpaper Downloader
#!/usr/bin/env node
var http = require('http');
var fs = require('fs');
var homePath = process.env.HOME || process.env.USERPROFILE;
var imagesPath = '/Pictures/bing-wallpapers';
if (!fs.existsSync(homePath + imagesPath))
fs.mkdirSync(homePath + imagesPath);
function downloadImage(url, name)
{
var filePath = homePath + imagesPath + '/' + name;
if (fs.existsSync(filePath))
{
console.log('skipping', url);
return;
}
console.log('downloading', url);
http.get(url, function(response)
{
if (response.statusCode !== 200)
{
console.log('Couldn\'t download', url);
return;
}
var file = fs.createWriteStream(filePath);
response.on('data', function(data)
{
file.write(data);
});
response.on('end', function()
{
file.end();
});
});
}
http.get('http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10&mkt=en-US', function(response)
{
var responseText = '';
response.on('data', function(data)
{
responseText += data.toString();
});
response.on('end', function()
{
var json = JSON.parse(responseText);
for (var i = 0; i < json.images.length; ++i)
{
var index = json.images[i].urlbase.lastIndexOf('/') + 1;
var imgExt = '_1920x1200.jpg';
var imgName = json.images[i].urlbase.substr(index) + imgExt;
var imgPath = json.images[i].urlbase + imgExt;
var imgUrl = 'http://www.bing.com' + imgPath;
downloadImage(imgUrl, imgName);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment