Last active
August 29, 2015 14:06
-
-
Save phosphoer/19a059f9d36b7896a4e1 to your computer and use it in GitHub Desktop.
Bing Wallpaper Downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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