Created
January 7, 2015 16:09
-
-
Save sachac/5eff5d01a6e03f7a494c to your computer and use it in GitHub Desktop.
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
var secret = require('./secret'); | |
var flickrOptions = secret.flickrOptions; | |
var Flickr = require("flickrapi"); | |
var fs = require('fs'); | |
/** | |
* Matches the image filenames in images.txt with the Flickr titles | |
* for images that don't yet have "Blogged" in their description. | |
*/ | |
exports.trimTitle = function(str) { | |
return str.replace(/^wpid-/g, '').replace(/ --.*$/g, '').replace(/[- _,]/g, ''); | |
}; | |
function readData(filename) { | |
var imageData = {}; | |
var data = fs.readFileSync(filename, {encoding: 'utf8'}); | |
var lines = data.split(/\n/); | |
for (var i = 0; i < lines.length; i++) { | |
if (lines[i]) { | |
var row = lines[i].split(/\t/); | |
var title = exports.trimTitle(row[0]); | |
imageData[title] = row[1]; | |
} | |
} | |
return imageData; | |
} | |
var imageData = readData('images.txt'); | |
function processPage(result, flickr) { | |
if (!result) return; | |
for (var i = 0; i < result.photos.photo.length; i++) { | |
var p = result.photos.photo[i]; | |
console.log(p.title); | |
var trimmed = exports.trimTitle(p.title); | |
var noTags = trimmed.replace(/#.*/g, ''); | |
var withTags = trimmed.replace(/#/g, ''); | |
var found = imageData[noTags] || imageData[withTags]; | |
if (found) { | |
var description = p.description._content; | |
if (description.match(found)) continue; | |
if (description) { | |
description += " - "; | |
} | |
description += '<a href="' + found + '">Blogged</a>'; | |
console.log("Updating " + p.title + " with " + description); | |
flickr.photos.setMeta( | |
{photo_id: p.id, | |
description: description}, | |
function(err, res) { | |
console.log(err, res); | |
} ); | |
} | |
} | |
} | |
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, | |
extras: 'description', | |
text: search + ' -blogged'}, function(err, result) { | |
processPage(result, flickr); | |
for (var i = 2 ; i < result.photos.pages; i++) { | |
flickr.photos.search( | |
{user_id: flickrOptions.user_id, per_page: 500, page: i, | |
extras: 'description', text: search + ' -blogged'}, | |
function(err, result) { | |
processPage(err, result, flickr); | |
}); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment