Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Luciano Mammino committed May 5, 2015
0 parents commit f4e71f3
Show file tree
Hide file tree
Showing 625 changed files with 97,313 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.apikey.js
35 changes: 35 additions & 0 deletions flickr-set-get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node

'use strict';

var cli = require('cli')
, flickr = require('./flickr/Flickr.js');

cli.parse({
apiKey: ['k', 'The flickr api key', 'string', require('./.apikey.js')],
parallelRequests: ['p', 'The number of parallel requests', 'number', 5]
});

cli.main(function(args, options) {
var api = new flickr(options.apiKey, options.parallelRequests);
var numPhotos = null;
var numDownloaded = 0;

api.on('setInfo', function(info) {
cli.info('Downloading '+info.total+' photos from "'+info.title+'" by ' + info.ownername);
numPhotos = info.total;
cli.progress(0);
});

api.on('photoDownloaded', function(){
numDownloaded++;
cli.progress(numDownloaded/numPhotos);
});

api.on('done', function(results){
cli.ok('All done.');
});

api.getSet(args[0], args[1]);
});

142 changes: 142 additions & 0 deletions flickr/Flickr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
'use strict';

var EventEmitter = require('events').EventEmitter
, async = require('async')
, util = require('util')
, extend = util._extend
, fs = require('fs')
, request = require('request')
, url = require('url');

// TODO add support for output folder
function Flickr(apiKey, parallelRequests) {
var self = this;

self.apiKey = apiKey;
if (typeof parallelRequests === 'undefined') {
parallelRequests = 5;
}
self.parallelRequests = parallelRequests;

function buildRequestUrl(method, params, apiKey, format) {
if (typeof apiKey === 'undefined') {
apiKey = self.apiKey;
}

if (typeof format === 'undefined') {
format = 'json';
}

var query = {
method : method,
api_key: apiKey,
format : format
};

if (format === 'json'){
query.nojsoncallback = 1;
}

if (params) {
query = extend(params, query);
}

return url.format({
protocol : 'https',
host: 'api.flickr.com',
pathname : 'services/rest',
query : query
});
}

function parseResponse(error, response, body) {
if (error || response.statusCode !== 200) {
throw new Error('Unexpected error with getInfo request');
}

var data = JSON.parse(body);
if (data.stat === 'fail') {
throw new Error('Error with flickr API: ' + data.message);
}

return data;
}

function getSetInfo(setId, userId, cb) {
var url = buildRequestUrl('flickr.photosets.getPhotos', {
photoset_id : setId,
user_id : userId
});

request(url, function(error, response, body) {
var data = parseResponse(error, response, body);

// TODO manage multi-paged sets
var info = {
title : data.photoset.title,
total : data.photoset.total,
ownername : data.photoset.ownername,
photos : data.photoset.photo
};

cb(info);
});
}

function getPhotoSizes(photoId, cb) {
var url = buildRequestUrl('flickr.photos.getSizes', {
photo_id : photoId
});

request(url, function(error, response, body) {
var data = parseResponse(error, response, body);

var sizes = {};

for (var i=0; i < data.sizes.size.length; i++) {
sizes[data.sizes.size[i].label] = data.sizes.size[i];
}

cb(sizes);
});
}

function downloadPhoto(url, path, cb) {
// TODO use temp files until the download is complete
request(url)
.pipe(fs.createWriteStream(path))
.on('close', cb);
}

// TODO avoid/reduce pyramid of doom (use promises?)
self.getSet = function(setId, userId, path, options) {
getSetInfo(setId, userId, function(info){
self.emit('setInfo', info);

var tasks = info.photos.map(function(currentValue, index, array){
var photoId = currentValue.id;
return function(cb){
getPhotoSizes(photoId, function(sizes){
self.emit('photoSizes', photoId, sizes);

var path = photoId + ".jpg";
downloadPhoto(sizes.Original.source, path, function(){
self.emit('photoDownloaded', photoId, path);
cb(null, path);
});
});
};
});

async.parallelLimit(tasks, self.parallelRequests, function(err, results){
self.emit('done', results);
});
});
};

return self;
}

util.inherits(Flickr, EventEmitter);

module.exports = Flickr;
3 changes: 3 additions & 0 deletions node_modules/async/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions node_modules/async/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f4e71f3

Please sign in to comment.