Skip to content

Instantly share code, notes, and snippets.

@nhindman
Last active August 29, 2015 13:58
Show Gist options
  • Save nhindman/10282716 to your computer and use it in GitHub Desktop.
Save nhindman/10282716 to your computer and use it in GitHub Desktop.
var request = require("request");
var EMBEDLY_KEY = '8c394ac3008745d48fe82133e6acc577';
var embedly = require('embedly');
var util = require('util');
// new embedly({key: EMBEDLY_KEY}, function(err, api) {
// if (!!err) {
// console.error('Error creating Embedly api');
// console.error(err.stack, api);
// return;
// }
// });
var summarize = function(url, callback) {
request("https://api.embed.ly/1/extract", {
qs: {url: url, key: EMBEDLY_KEY}
}, function(error, response, body) {
if (!error && response.statuCode == 200) {
callback(JSON.parse(body));
} else {
console.log("modules/embedly#summarize", error, response);
}
});
};
// exports.summarize = summarize;
var Twit = require("twit");
var embedly = require('embedly');
var T = new Twit({
consumer_key: 'i7lsLu4dOv66EEDzCWEifyoXd',
consumer_secret: '3I7NQsQsjQpCsadhN8t1Ry9Xb3oglUsDTzME6Cc0EGLvrVmZQB',
access_token: '1067293915-gXahWnpOxPZydxkzAO4YzWNfhaSkZp8Nz6KfW4L',
access_token_secret: '1e8Y0rCaLlz9SkdlIqBuMsBbEJvDY1SUb9ZClh6WNkmim',
});
var tweets = [];
var urls = [];
var urlPattern = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/
T.get('statuses/user_timeline', { 'screen_name' : 'claykohut', 'count': 100 }, function(err, reply) {
for (i in reply){
tweet = reply[i].text;
if (urlPattern.test(tweet)){
console.log(tweet);
tweets.push(tweet);
var urlRegex = /(https?:\/\/[^\s]+)/g;
tweet.replace(urlRegex, function(tweet) {
console.log(tweet)
urls.push(tweet);
})
}
}
urls.forEach(function(url) {
summarize(url, function(summary) {
console.log(summary);
});
});
console.log(tweets.length)
})
@nhindman
Copy link
Author

nhindman commented Apr 9, 2014

Great, thanks so much; it's working now. I did try to add stuff from the node.js embedly module, and have removed that per your advice. As you can probably tell, I'm very new to node.js and npm packages.

It may be just me, but the embed.ly docs seem pretty minimal. For instance, I couldn't find any syntax in the docs resembling the summarize function you put together on line 16, and I'm still pretty shaky on understanding the structure of that request, lines 17 & 18 in particular.

But the good news is that it's working, so now my task is just going through the body response I'm getting back in the terminal and pull out the things that are of interest: the title, the description and the thumbnail url of the header image.

Reading up on JSONP as well to see where that will come into play..

@tastycode
Copy link

The docs describe a REST api. REST apis are used by pretty much all services that need to communicate with code running on servers. Having said that, you wouldn't really find docs on how to do this within embedly, embedly would just provide endpoints and you would expected to be able to code your way to them. Every language running on servers has a way to make basic http requests.

For example

You can just hit their endpoint from your terminal using curl or any other tool that can make a get request to an endpoint.

$ curl -v "https://api.embed.ly/1/extract?url=SOME_URL&key=EMBEDLY_KEY
     qs: {url: url, key: EMBEDLY_KEY}

Additionally, you could copy and paste the same URL and put it into your browser and see the result. No libraries required, you just need something to make the http request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment