Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cianclarke/3364750 to your computer and use it in GitHub Desktop.
Save cianclarke/3364750 to your computer and use it in GitHub Desktop.
Append the image URL for posts from Instagram and Tumblr to a tweet's entities
var imageController,
util = require("util"),
request = require("request");
imageController = {
checkForTweetUrlMedia: function (tweet, callback) {
var url = (tweet.entities && tweet.entities.urls && tweet.entities.urls.length>0) ? tweet.entities.urls[0].expanded_url : false,
rex = false,
matchAt = 1;
// Instagram
if (url && url.match(/instagr(\.am|am\.com)\/p\//)){
rex = /<span class="img".+style="background-image: url\((.+)\);/
}
// Tumblr
if (url && url.match(/tmblr\.co/)){
rex = /http:\/\/[0-9]+\.media\.tumblr\.com\/.+\.(jpg|png)/;
matchAt = 0;
console.log('tumblr');
}
if (rex){
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var results = rex.exec(body);
if (results && results.length>1){
var imageUrl = results[matchAt];
tweet.entities = tweet.entities || {};
tweet.entities.media = [];
tweet.entities.media.push({ media_url: imageUrl });
return callback(null, tweet); // we got an error, but we still want the tweet, like!
}
}
//return callback(error, tweet);
});
}else{
console.log('no match for URL ' + url);
return callback(null, tweet);
}
}
};
module.exports = imageController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment