Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video download #34

Merged
merged 5 commits into from
Aug 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Videos instead of their thumbnails are downloaded.
Photos that list the value "Video Original" as an available size are threated as
videos. The download url of them has to be extracted from one the video sizes
(e.g. "Video Original", "HD MP4", "Mobile MP4", "Site MP4", etc.). Otherwise,
the video's thumbnail is fetched. Moreover, the file extension is adjusted to
"mp4".
  • Loading branch information
saemy committed Aug 20, 2016
commit 1dbd942cce83e14ea87d2fa4e2fbec3940e2f5ed
1 change: 1 addition & 0 deletions bin/flickr-set-get
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ program
.option('-c, --concurrency <n>', 'The number of concurrent requests', parseInt, null)
.option('-o, --outputDir <s>', 'The directory where to save the downloaded images', null, null)
.option('-s, --size <s>', 'The size of the image to download (eg. "Original", "Large", "Medium", etc.)', null, null)
.option('-vs, --videoSize <s>', 'The size of the video to download (eg. "Video Original", "HD MP4", "Mobile MP4", etc.)', null, null)
.option('-n, --noOverwrite', 'If set does not overwrite existing files', null, null)
.action(function action(setId, userId, options) {
getSet(setId, userId, options);
Expand Down
36 changes: 27 additions & 9 deletions lib/Flickr.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var download = require('./download.js');
* - **concurrency** {number} the maximum number of concurrent http requests (default: 10)
* - **outputDir** {string} the path where to save the images (default: ".")
* - **size** {string} The size of the image to download (eg. "Original", "Large", "Medium", etc.)
* - **videoSize** {string} The size of the video to download (eg. "Video Original", "HD MP4", "Mobile MP4", etc.)
* - **noOverwrite** {boolean} if true avoids to override already existing files (default: false)
* - **auth** {boolean|object} if false it will not use authentication. Otherwise it should be an object
* containing the keys `secret` and `authToken` (or `miniToken`) (default: false)
Expand All @@ -45,6 +46,7 @@ function Flickr(apiKey, options) {
concurrency: 10,
outputDir: './',
size: 'Original',
videoSize: 'Video Original',
noOverwrite: false,
auth: false
};
Expand Down Expand Up @@ -223,13 +225,22 @@ function Flickr(apiKey, options) {
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);
});
var videoPath = _path.join(_this.options.outputDir, photoId + '.mp4');
if (_this.options.noOverwrite) {
var exists = fs.existsSync(path);
if (fs.existsSync(videoPath)) {
exists = true;
path = videoPath;
}

if (exists) {
_this.emit('photoSkipped', photoId, path);
async.setImmediate(function onAsync() {
cb(null, path);
});

return;
return;
}
}

getPhotoSizes(photoId, function onGetPhotoSizes(err, sizes) {
Expand All @@ -241,14 +252,21 @@ function Flickr(apiKey, options) {

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

if (!sizes[_this.options.size]) {
var sizeErr = new Error('Size "' + _this.options.size + '" not available');
var size = _this.options.size;
if (sizes['Video Original']) {
// This is a video.
size = _this.options.videoSize;
path = videoPath;
}

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

return cb(null, path);
}

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

if (err) {
_this.emit('warning', err);
Expand Down