Last active
January 1, 2016 04:09
-
-
Save johanbrook/8089908 to your computer and use it in GitHub Desktop.
Quick script for downloading a bunch of wallpapers from a given
array of URLs, in a special resolution.
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 | |
/* | |
Quick script for downloading a bunch of wallpapers from a given | |
array of URLs, in a special resolution. | |
Call script: | |
node download.js [pathToUrlFile, resolution, destination] | |
Default options: | |
{ | |
urls: "wallpapers.json", // Path to JSON file with wallpapers, on the format ' {"urls": ['http://..', 'http://']} ' | |
resolution: "1440x900", // Desired resolution | |
destination: "../" // Desired destination for wallpapers | |
} | |
By Johan Brook, 2013 | |
("probably would've taken me shorter time to just download those fucking wallpapers manually") | |
*/ | |
var exec = require("child_process").exec | |
var fs = require("fs") | |
var options = {} | |
var root = this | |
// Kick it off | |
init(process.argv.slice(2)) | |
/* | |
Initialization | |
*/ | |
function init(args) { | |
options.urls = args[0] || "wallpapers.json" | |
options.resolution = args[1] || "1440x900" | |
options.destination = args[2] || "../" | |
var wallpapers = JSON.parse(fs.readFileSync(options.urls)) | |
if(wallpapers && wallpapers.urls) { | |
downloadWallpapersFromUrls(wallpapers.urls) | |
} else { | |
console.error("The file '" + options.urls + "' doesn't exist!") | |
} | |
} | |
/* | |
When all wallpapers are downloaded | |
*/ | |
function done(nbr) { | |
console.log("\n** Done! ** "+ nbr + " wallpapers were downloaded") | |
} | |
/* | |
Download wallpapers from urls in `urls` param array | |
*/ | |
function downloadWallpapersFromUrls(urls) { | |
_each(urls, downloadWallpaperFromUrl, done) | |
} | |
function downloadWallpaperFromUrl(url, callback) { | |
parseUrlForResolution(url, options.resolution, function(finalImageUrl, originalUrl) { | |
exec('wget -P '+options.destination + " "+finalImageUrl, function(error, stdout, sterr) { | |
if(error) { | |
callback(error) | |
} | |
else { | |
console.log("Downloading '" + prettyFileName(finalImageUrl) + "' in the resolution "+options.resolution+" ...") | |
callback(null) | |
} | |
}) | |
}) | |
} | |
/* | |
Parse and return a nice representation of the wallpaper url | |
*/ | |
function prettyFileName(url) { | |
var parts = url.split("/") | |
return parts[parts.length - 1] | |
} | |
/* | |
`resolution` should be on the form <number>x<number>, | |
like "1440x900". | |
Correct resolution will be appended just before the | |
file type extension in `url`, like: | |
http://site.com/picture_1440x900.jpg | |
(res: 2560x1440) => http://site.com/picture_2560x1440.jpg | |
*/ | |
function parseUrlForResolution(url, resolution, callback) { | |
callback(url.replace(/(\d+x\d+)/g, resolution), url) | |
} | |
/* | |
Tools You Don't Have To Care About | |
------------- | |
*/ | |
/* | |
Special version of Array.forEach | |
From https://github.com/caolan/async/blob/master/lib/async.js | |
Apply iterator to every element in arr and call callback | |
whenever all elements are called. | |
*/ | |
function _each(arr, iterator, callback) { | |
callback = callback || function () {} | |
if (!arr.length) { | |
return callback() | |
} | |
var completed = 0 | |
arr.forEach(function (x) { | |
iterator(x, _only_once(function (err) { | |
if (err) { | |
callback(err); | |
callback = function () {} | |
} | |
else { | |
completed += 1 | |
if (completed >= arr.length) { | |
callback(arr.length) | |
} | |
} | |
})) | |
}) | |
} | |
/* | |
Make sure a function fn was called only once | |
*/ | |
function _only_once(fn) { | |
var called = false; | |
return function() { | |
if (called) throw new Error("Callback was already called."); | |
called = true; | |
fn.apply(root, arguments); | |
} | |
} |
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
{ | |
"urls" : [ | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_155_bob_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_159_bob_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_162_blk_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_179_red_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_204_bob_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_213_wht_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_46_blk_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_4_bob_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_55_red_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_57_red_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_59_blk_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_5_wht_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_91_red_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_107_red_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_124_bob_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_13_bob_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_149_red_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_150_wht_1440x900.jpg", | |
"http://goodfuckingdesignadvice.com/uploads/wallpaper/1440x900/GFDA_151_wht_1440x900.jpg" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment