-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luciano Mammino
committed
May 5, 2015
0 parents
commit f4e71f3
Showing
625 changed files
with
97,313 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
.apikey.js |
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
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]); | ||
}); | ||
|
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
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.