Skip to content

Instantly share code, notes, and snippets.

@sachac
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sachac/01b21572cccb0a558aa3 to your computer and use it in GitHub Desktop.
Save sachac/01b21572cccb0a558aa3 to your computer and use it in GitHub Desktop.
/**
* Upload the file to my Flickr sketchbook and then moves it to
* Dropbox/Inbox/To blog. Save the Org Mode links in the clipboard. -
* means the photo already existed, + means it was uploaded.
*/
var async = require('async');
var cp = require('child_process');
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var flickr = require('flickr-with-uploads');
var secret = require("./secret");
var SKETCHBOOK_PHOTOSET_ID = '72157641017632565';
var BLOG_INBOX_DIRECTORY = 'c:\\sacha\\dropbox\\inbox\\to blog\\';
var api = flickr(secret.flickrOptions.api_key,
secret.flickrOptions.secret,
secret.flickrOptions.access_token,
secret.flickrOptions.access_token_secret);
var result = [];
function getTags(filename) {
var tags = [];
var match;
var re = new RegExp('#([^ ]+)', 'g');
while ((match = re.exec(filename)) !== null) {
tags.push(match[1]);
}
return tags.join(' ');
}
// assert(getTags("foo #bar #baz qux") == "bar baz");
function checkIfPhotoExists(filename, doesNotExist, existsFunction, done) {
var base = path.basename(filename).replace(/.png$/, '');
api({method: 'flickr.photos.search',
user_id: secret.flickrOptions.user_id,
text: base},
function(err, response) {
var found = undefined;
if (response && response.photos[0].photo) {
for (var i = 0; i < response.photos[0].photo.length; i++) {
if (response.photos[0].photo && response.photos[0].photo[i]['$'].title == base) {
found = i; break;
}
}
}
if (found !== undefined) {
existsFunction(response.photos[0].photo[found], done);
} else {
doesNotExist(filename, done);
}
});
}
function formatExistingPhotoAsOrg(photo, done) {
var title = photo['$'].title;
var url = 'https://www.flickr.com/photos/'
+ photo['$'].owner
+ '/' + photo['$'].id;
result.push('- [[' + url + '][' + title + ']]');
done();
}
function formatAsOrg(response) {
var title = response.photo[0].title[0];
var url = response.photo[0].urls[0].url[0]['_'];
result.push('+ [[' + url + '][' + title + ']]');
}
function uploadImage(filename, done) {
api({
method: 'upload',
title: path.basename(filename.replace(/.png$/, '')),
is_public: 1,
hidden: 1,
safety_level: 1,
photo: fs.createReadStream(filename),
tags: getTags(filename.replace(/.png$/, ''))
}, function(err, response) {
if (err) {
console.log('Could not upload photo: ', err);
done();
} else {
var newPhoto = response.photoid[0];
async.parallel(
[
function(done) {
api({method: 'flickr.photos.getInfo',
photo_id: newPhoto}, function(err, response) {
if (response) { formatAsOrg(response); }
done();
});
},
function(done) {
api({method: 'flickr.photosets.addPhoto',
photoset_id: SKETCHBOOK_PHOTOSET_ID,
photo_id: newPhoto}, function(err, response) {
if (!err) {
moveFileToBlogInbox(filename, done);
} else {
console.log('Could not add ' + filename + ' to Sketchbook');
done();
}
});
}],
function() {
done();
});
}
});
}
function moveFileToBlogInbox(filename, done) {
fs.rename(filename, BLOG_INBOX_DIRECTORY + path.basename(filename),
function(err) {
if (err) { console.log(err); }
done();
});
}
var arguments = process.argv.slice(2);
async.each(arguments, function(item, done) {
if (item.match('\\*')) {
glob.glob(item, function(err, files) {
if (!files) return;
async.each(files, function(file, done) {
checkIfPhotoExists(file, uploadImage, formatExistingPhotoAsOrg, done);
}, function() {
done();
});
});
} else {
checkIfPhotoExists(item, uploadImage, formatExistingPhotoAsOrg, done);
}
}, function(err) {
console.log(result.join("\n"));
var child = cp.spawn('clip');
child.stdin.write(result.join("\n"));
child.stdin.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment