Last active
March 30, 2019 05:13
-
-
Save mohsen1/2bba3165ade2c4016c10 to your computer and use it in GitHub Desktop.
Download Chromecast backgrounds
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
var https = require('https'); | |
var fs = require('fs'); | |
var url = 'https://raw.githubusercontent.com/dconnolly/chromecast-backgrounds/master/backgrounds.json'; | |
Array.prototype.getLast = function() { | |
return this[this.length - 1]; | |
}; | |
function logFail(err){ | |
console.log('Failed!'); | |
console.error(err); | |
} | |
function download(arr, index){ | |
var item = arr[index]; | |
if (!item) return; | |
var name = item.url.split('/').getLast(); | |
name = item.author + name; | |
if (name.substr(-4) !== '.jpg') { | |
name += '.jpg'; | |
} | |
var file = fs.createWriteStream(name); | |
https.get(item.url, function (res){ | |
res.pipe(file); | |
res.on('end', function(){ | |
console.log('Finished downloading ' + name ); | |
file.end(); | |
download(arr, index+1); | |
}); | |
res.on('error', logFail) | |
}); | |
} | |
https.get(url, function (listRes) { | |
var body = ''; | |
listRes.on('data', function(chunk){ | |
body += chunk; | |
}); | |
listRes.on('error', logFail); | |
listRes.on('end', function(){ | |
var arr = JSON.parse(body); | |
download(arr, 0); | |
}); | |
}).on('error', logFail); |
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
var https = require('https'); | |
var fs = require('fs'); | |
var url = 'https://raw.githubusercontent.com/dconnolly/chromecast-backgrounds/master/backgrounds.json'; | |
function makeName(item) { | |
var name = item.url.split('/'); | |
name = name[name.length - 1]; | |
name = item.author + name; | |
if (name.substr(-4) !== '.jpg') { name += '.jpg'; } | |
return name; | |
} | |
https.get(url, function (listRes) { | |
var body = ''; | |
listRes.on('data', function(chunk){ body += chunk; }); | |
listRes.on('end', function(){ | |
JSON.parse(body).forEach(function(item){ | |
var file = fs.createWriteStream(makeName(item)); | |
https.get(item.url, function (res){ | |
res.pipe(file); | |
res.on('end', function(){ file.end(); }); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment