Skip to content

Instantly share code, notes, and snippets.

@dariusk

dariusk/index.js

Created Feb 20, 2016
Embed
What would you like to do?
@TinyAssistant
var Twit = require('twit');
var T = new Twit(require('./config.js'));
var wordfilter = require('wordfilter');
var ent = require('ent');
var readability = require('readability-api');
readability.configure({
consumer_key: 'CONSUMER_KEY',
consumer_secret: 'CONSUMER_SECRET'
});
Array.prototype.pick = function() {
return this[Math.floor(Math.random()*this.length)];
};
Array.prototype.pickRemove = function() {
var index = Math.floor(Math.random()*this.length);
return this.splice(index,1)[0];
};
function generate() {
return new Promise((resolve, reject) => {
readability.xauth('tinysubversions', 'XAUTH_KEY', (err, tokens) => {
var reader = new readability.reader({
access_token: tokens.oauth_token,
access_token_secret: tokens.oauth_token_secret
});
reader.bookmarks({per_page: 50}, (err, bookmarks) => {
var bookmarkList = bookmarks.bookmarks;
var bookmark = bookmarkList.pick();
var id = bookmark.article.id;
reader.article(id, function (err, article) {
console.log(article.content, article.url, Object.keys(article), id, article.excerpt);
var content = ent.decode(article.content.replace(/(<([^>]+)>)/ig,''));
console.log(content);
var sentences = content.match( /[^\.!\?]+[\.!\?]+/g);
if (sentences !== null) {
sentences = sentences.map(sentence => {
return sentence.replace(/\n/g,'').trim();
});
sentences = sentences.filter(sentence => {
return sentence.length > 40 &&
sentence.length < 110 &&
// get rid of false sentence ends like "According to Charles S."
sentence.match(/\s\w\.$/) === null;
});
console.log(sentences);
console.log(bookmarkList.length);
var tweet = sentences.pick() + ' ' + article.url;
resolve(tweet);
}
else {
console.log('There were no sentences in this article!');
}
});
});
});
}).catch((e) => console.log(e));
}
function tweet() {
generate().then(myTweet => {
if (!wordfilter.blacklisted(myTweet)) {
console.log(myTweet);
T.post('statuses/update', { status: myTweet }, (err, reply) => {
if (err) {
console.log('error:', err);
}
else {
console.log('reply:', reply);
}
});
}
}).catch((e) => console.log(e));
}
// Tweet once on initialization
tweet();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment