Created
January 31, 2015 18:44
-
-
Save sachac/a20c80c4072c8b5ae961 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
/** | |
* Adds "Blogged" links to Flickr for images that don't yet have "Blogged" in their description. | |
* Command-line argument: URL to retrieve and parse | |
*/ | |
var secret = require('./secret'); | |
var flickrOptions = secret.flickrOptions; | |
var Flickr = require("flickrapi"); | |
var fs = require('fs'); | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var imageData = {}; | |
var $; | |
function setDescriptionsFromURL(url) { | |
request(url, function(error, response, body) { | |
// Parse the images | |
$ = cheerio.load(body); | |
$('article').each(function() { | |
var prettyLink = $(this).find("h2 a").attr("href"); | |
if (!prettyLink.match(/weekly/i) && !prettyLink.match(/monthly/i)) { | |
collectLinks($(this), prettyLink, imageData); | |
} | |
}); | |
updateFlickrPhotos(); | |
}); | |
} | |
function updateFlickrPhotos() { | |
Flickr.authenticate(flickrOptions, function(error, flickr) { | |
flickr.photos.search( | |
{user_id: flickrOptions.user_id, | |
per_page: 500, | |
extras: 'description', | |
text: ' -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: ' -blogged'}, | |
function(err, result) { | |
processPage(err, result, flickr); | |
}); | |
} | |
}); | |
}); | |
} | |
function collectLinks(article, prettyLink, imageData) { | |
var results = []; | |
article.find(".body a").each(function() { | |
var link = $(this); | |
if (link.attr('href')) { | |
if (link.attr('href').match(/sachachua/) | |
|| !link.attr('href').match(/^http/)) { | |
imageData[exports.trimTitle(link.attr('href'))] = prettyLink; | |
} else if (link.attr('href').match(/flickr.com/)) { | |
imageData[exports.trimTitle(link.text())] = prettyLink; | |
} | |
} | |
}); | |
return results; | |
} | |
exports.trimTitle = function(str) { | |
return str.replace(/^.*\//, '').replace(/^wpid-/g, '').replace(/[^A-Za-z0-9]/g, '').replace(/png$/, '').replace(/[0-9]$/, ''); | |
}; | |
function processPage(result, flickr) { | |
if (!result) return; | |
for (var i = 0; i < result.photos.photo.length; i++) { | |
var p = result.photos.photo[i]; | |
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) { | |
if (err) { console.log(err, res); } | |
} ); | |
} | |
} | |
} | |
setDescriptionsFromURL(process.argv[2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment