Skip to content

Instantly share code, notes, and snippets.

@AYLIEN
Last active August 29, 2015 14:11
Show Gist options
  • Save AYLIEN/a3a61c841d4c07b3a28c to your computer and use it in GitHub Desktop.
Save AYLIEN/a3a61c841d4c07b3a28c to your computer and use it in GitHub Desktop.
Analyzing SubReddits
//**********************************************************************
// Enter a SubReddit below (var subReddit = 'technology') to
// receive an analysis of the top stories.
//**********************************************************************
var subReddit = 'technology';
var numberOfStories = 3;
var AYLIENTextAPI = require('aylien_textapi'),
request = require('request'),
xml2js = require('xml2js');
var textapi = new AYLIENTextAPI({
application_id: 'YourApplicationId',
application_key: 'YourApplicationKey'
});
var analysisResults = {};
var regexp = /([^"]*)\"\>\[link\]/;
var parser = new xml2js.Parser();
var counter = 3 * numberOfStories;
callAylienAPIs(outputResults);
function callAylienAPIs(callback) {
parser.addListener('end', function(result) {
var items = result.rss.channel[0].item;
items.slice(1,numberOfStories+1).forEach(function(item) {
var title = item.title[0];
analysisResults[title] = {};
analysisResults[title].title = title;
var description = item.description[0];
var link = description.match(regexp)[1];
analysisResults[title].link = link;
analysisResults[title].comments_link = item.link[0];
textapi.classify(link, function(error, result) {
if (error === null && result.categories[0] != undefined) {
var cat = {};
cat.label = result.categories[0].label;
cat.code = result.categories[0].code;
cat.confidence = result.categories[0].confidence;
analysisResults[title].category = cat;
analysisResults[title].category_error = false;
} else {
analysisResults[title].category_error = true;
}
counter--;
if (counter == 0) {
callback();
}
});
textapi.hashtags(link, function(error, result) {
if (error === null) {
analysisResults[title].hashtags = result.hashtags;
var j = 0;
analysisResults[title].hashtags.forEach(function(hash) {
++j;
});
analysisResults[title].hashtags_error = false;
} else {
analysisResults[title].hashtags_error = true;
}
counter--;
if (counter == 0) {
callback();
}
});
textapi.summarize(link, function(error, result) {
if (error === null) {
analysisResults[title].sentences = result.sentences;
var i = 0;
analysisResults[title].sentences.forEach(function(sent) {
++i;
});
analysisResults[title].summarization_error = false;
} else {
analysisResults[title].summarization_error = true;;
}
counter--;
if (counter == 0) {
callback();
}
});
});
});
}
function outputResults() {
for (var key in analysisResults) {
console.log("\n**************\n" + analysisResults[key].title + "\n**************\n");
if (analysisResults[key].summarization_error == false && analysisResults[key].sentences.length != 0) {
j = 0;
console.log("\nKey Sentences :\n");
analysisResults[key].sentences.forEach(function(sent) {
++j;
console.log(j + ") " + sent + "-\n");
});
} else {
console.log("No Summarization available for this story\n");
}
if (analysisResults[key].category_error == false) {
console.log("\nClassification :\n");
console.log("Category Label : " + analysisResults[key].category.label);
console.log("IPTC Code : " + analysisResults[key].category.code);
console.log("Confidence : " + analysisResults[key].category.confidence);
} else {
console.log("No Classification available for this story\n");
}
var j = 0;
console.log("\nHashtags :\n");
if (analysisResults[key].category_error == false) {
var tags = "";
analysisResults[key].hashtags.forEach(function(hash) {
tags+= hash;
tags+= ",";
});
console.log(tags + "\n");
} else {
console.log("No Hashtags available for this story\n");
}
console.log("\nStory Link :\n" + analysisResults[key].link + "\n");
console.log("Story Comments Link :\n" + analysisResults[key].comments_link + "\n");
console.log("================== Next Story =================\n\n");
}
}
request('http://www.reddit.com/r/' + subReddit + '/.rss', function (error, response, body) { if (error === null) {
parser.parseString(body);
} else {
console.log(error);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment