Created
April 10, 2015 16:56
-
-
Save AYLIEN/304db58f57b7e3960635 to your computer and use it in GitHub Desktop.
Newswire Sample
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
//************************************** | |
//Configuration details specific to you. | |
//************************************** | |
const AYLIEN_APP_ID = YOUR_AYLIEN_APP_ID; | |
const AYLIEN_APP_KEY = YOUR_AYLIEN_APP_KEY; | |
const MAILGUN_API_KEY = YOUR_MAILGUN_API_KEY; | |
const SENDING_EMAIL_ACCOUNT = 'sending@example.com'; | |
const RECIPIENTS = 'Recipient 1 <recipient@example.com>'; | |
const NUM_STORRIES_PER_RSS_FEED = 3; | |
var rssFeeds = [ | |
"http://www.kdnuggets.com/feed", | |
"http://news.ycombinator.com/rss" | |
]; | |
//************************************** | |
//End of configuration section | |
//************************************** | |
var AYLIENTextAPI = require('aylien_textapi'), | |
request = require('request'), | |
xml2js = require('xml2js'), | |
Mailgun = require('mailgun').Mailgun; | |
var textapi = new AYLIENTextAPI({ | |
application_id: AYLIEN_APP_ID, | |
application_key: AYLIEN_APP_KEY | |
}); | |
var mg = new Mailgun(MAILGUN_API_KEY); | |
var emailBody = ''; | |
var numStories = 0; | |
rssFeeds.forEach(function(rssFeed) { | |
setTimeout(function() { | |
request(rssFeed, function(error, response, body) { | |
if (error === null) { | |
parser.parseString(body); | |
} else { | |
console.log('rss error : ', error); | |
} | |
}); | |
var parser = new xml2js.Parser(); | |
console.log("Processing rss feed - " + rssFeed + "..."); | |
parser.addListener('end', function(result) { | |
var items = result.rss.channel[0].item; | |
items.slice(0, NUM_STORRIES_PER_RSS_FEED).forEach(function( | |
item) { | |
var title, link; | |
title = item.title[0]; | |
link = item.link[0]; | |
textapi.classify(link, function(error, result) { | |
if (error === null && result.categories[0]) { | |
var label = result.categories[0].label; | |
var code = result.categories[0].code; | |
textapi.summarize({ | |
url: link, | |
sentences_number: 3 | |
}, function(err, resp) { | |
if (err === null) { | |
var summary = ""; | |
for (var i = 0; i & lt; resp.sentences.length; i++) { | |
summary += resp.sentences[i] + " "; | |
} | |
var story = '\n\nTitle : ' + title + | |
'\n' + 'Classification : ' + label + | |
'\n' + 'Link : ' + link + | |
'\nSummary : ' + summary; | |
emailBody += story; | |
numStories++; | |
if (numStories == | |
NUM_STORRIES_PER_RSS_FEED * rssFeeds.length | |
) { | |
console.log( | |
"Preparing Digest email..."); | |
mg.sendText(SENDING_EMAIL_ACCOUNT, [ | |
RECIPIENTS | |
], | |
'Your Daily RSS Digest', | |
emailBody, | |
'noreply@example.com', {}, | |
function(err) { | |
if (err) console.log( | |
'Error sending message to mailgun...: ' + | |
err); | |
else console.log('Success'); | |
}); | |
} | |
} | |
}); | |
} else { | |
console.log('classify error : ', error); | |
console.log('classify error ', result.categories[ | |
0]); | |
numStories++; | |
} | |
}); | |
}); | |
}); | |
}, 3000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment