Create a gist now

Instantly share code, notes, and snippets.

Embed
var secret = require("./secret");
var flickrOptions = secret.flickrOptions;
var Flickr = require("flickrapi");
exports.trimTitle = function(str) {
return str.replace(/ --.*$/g, '').replace(/#[^ ]+/g, '').replace(/[- _]/g, '');
};
var hash = {};
exports.possiblyDeletePhoto = function(p, flickr) {
console.log("Deleting " + p.title);
p.deleted = true;
flickr.photos.delete({photo_id: p.id}, function(err, result) {
console.log(err, result);
} );
};
exports.keepNewPhoto = function(oldPhoto, newPhoto) {
if (newPhoto.title.length > oldPhoto.title.length)
return true;
if (newPhoto.title.length < oldPhoto.title.length)
return false;
if (newPhoto.id < oldPhoto.id)
return true;
return false;
};
exports.processPhoto = function(p, flickr) {
var trimmed = exports.trimTitle(p.title);
if (trimmed && hash[trimmed] && p.id != hash[trimmed].id) {
// We keep the one with the longer title or the newer date
if (exports.keepNewPhoto(hash[trimmed], p)) {
exports.possiblyDeletePhoto(hash[trimmed], flickr);
hash[trimmed] = p;
}
else if (p.id != hash[trimmed].id) {
exports.possiblyDeletePhoto(p, flickr);
}
} else {
hash[trimmed] = p;
}
};
function processPageOfFlickrResults(err, result, flickr) {
if (!result) return;
for (var i = 0; i < result.photos.photo.length; i++) {
exports.processPhoto(result.photos.photo[i], flickr);
console.log(result.photos.photo[i].title);
}
}
var search = process.argv.length >= 2 ? process.argv[2] : '';
Flickr.authenticate(flickrOptions, function(error, flickr) {
flickr.photos.search(
{user_id: flickrOptions.user_id, per_page: 500, text: search},
function(err, result) {
processPageOfFlickrResults(err, result, flickr);
var pages = result.photos.pages;
for (var i = 2; i < pages; i++) {
flickr.photos.search(
{user_id: flickrOptions.user_id, per_page: 500, page: i, text: search},
function(err, result) {
processPageOfFlickrResults(err, result, flickr);
});
};
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment