Skip to content

Commit

Permalink
Speedup & completion of download on errors. (#30)
Browse files Browse the repository at this point in the history
* Replaces errors by warnings during image download.

If an error occurs during downloading an image from a set, a warning instead of
an error is emitted. This allows the rest of the set being downloaded.

* Avoids unnecessary Flickr accesses.

When image overrides are turned off, the check of an existing file should be
done before getting the available image sizes from Flickr. This avoids
unnecessary Flickr accesses and speeds up the process.
  • Loading branch information
saemy authored and lmammino committed Aug 5, 2016
1 parent bfb8abf commit 1dfe55c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
7 changes: 6 additions & 1 deletion bin/flickr-set-get
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ function getSet(setId, userId, commandOptions) {
var numProcessed = 0;
var numDownloaded = 0;
var numSkipped = 0;
var numWarnings = 0;

api.on('setInfo', function onSetInfo(info) {
cli.info('Downloading ' + info.total + ' photos from "' + info.title + '" by ' + info.ownername);
Expand All @@ -142,9 +143,13 @@ function getSet(setId, userId, commandOptions) {
});

api.on('done', function onDone(results) {
cli.ok('All done. ' + numDownloaded + ' photos downloaded, ' + numSkipped + ' skipped');
cli.ok('All done. ' + numDownloaded + ' photos downloaded, ' + numSkipped + ' skipped, ' + numWarnings + ' warnings');
});

api.on('warning', function onWarning(error) {
numWarnings++;
cli.error(error);
});
api.on('error', function onError(error) {
cli.error(error);
process.exit(1);
Expand Down
26 changes: 15 additions & 11 deletions lib/Flickr.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,33 +222,37 @@ function Flickr(apiKey, options) {
var tasks = info.photos.map(function mapTasks(currentValue, index, array) {
var photoId = currentValue.id;
return function asyncTask(cb) {
var path = _path.join(_this.options.outputDir, photoId + '.jpg');
if (_this.options.noOverwrite && fs.existsSync(path)) {
_this.emit('photoSkipped', photoId, path);
async.setImmediate(function onAsync() {
cb(null, path);
});

return;
}

getPhotoSizes(photoId, function onGetPhotoSizes(err, sizes) {

if (err) {
_this.emit('error', err);
return cb(err);
_this.emit('warning', err);
return cb(null, path);
}

_this.emit('photoSizes', photoId, sizes);

if (!sizes[_this.options.size]) {
var sizeErr = new Error('Size "' + _this.options.size + '" not available');
_this.emit('error', sizeErr);

return cb(sizeErr);
}
_this.emit('warning', sizeErr);

var path = _path.join(_this.options.outputDir, photoId + '.jpg');
if (_this.options.noOverwrite && fs.existsSync(path)) {
_this.emit('photoSkipped', photoId, path);
return cb(null, path);
}

download(sizes[_this.options.size].source, path, function onDownloadComplete() {

if (err) {
_this.emit('error', err);
return cb(err);
_this.emit('warning', err);
return cb(null, path);
}

_this.emit('photoDownloaded', photoId, path);
Expand Down
2 changes: 1 addition & 1 deletion tests/Flickr.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ describe('Flickr', function ƒ() {
it('should raise an error if a given size does not exists', function ƒ(done) {
var f = new Flickr('apiKey', {concurrency: 1, outputDir: 'temp', size: 'Wrong size'});

f.on('error', function onError(error) {
f.on('warning', function onWarning(error) {
rmDirSyncIfExists('temp');
if (error.message.match(/Size "Wrong size" not available/)) {
done();
Expand Down

0 comments on commit 1dfe55c

Please sign in to comment.