Last active
June 15, 2020 22:39
-
-
Save hammertoe/4b1dc236a9db1a2208bb778268efbe19 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
app.get('/tweets', | |
require('connect-ensure-login').ensureLoggedIn(), | |
function(req, res) { | |
// Create client instance to connect to Twitter | |
// We pass in the access token we got from the OAuth process | |
var T = new Twit({ | |
consumer_key: process.env['TWITTER_CONSUMER_KEY'], | |
consumer_secret: process.env['TWITTER_CONSUMER_SECRET'], | |
access_token: req.user.token, | |
access_token_secret: req.user.tokenSecret, | |
timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests. | |
strictSSL: true, // optional - requires SSL certificates to be valid. | |
}) | |
// Fetch the tweets from the user's timeline, we need the | |
// 'extended' mode to get the full 240 characters | |
T.get('statuses/home_timeline', { count: 20, | |
tweet_mode: 'extended' }, | |
async (err, data, response) => { | |
// Create an agent instance that we will use to connect | |
// to the Watson Tone Analyzer service, we include a throttle | |
// adapter to attempt to prevent us overloading the API rate limits | |
const agent = axios.create({ | |
timeout: 1000, | |
auth: {username: 'apikey', | |
password: tone_key}, | |
adapter: throttleAdapterEnhancer(axios.defaults.adapter, { threshold: 1000 }) | |
}); | |
// Process all the tweets in parallel async | |
let tweets = await Promise.all(data.map(async tweet => { | |
// if the tweet is a retweet then the actual text is in the | |
// retweeted status | |
let status = tweet.retweeted_status || tweet; | |
let text = status.full_text; | |
// connect to tone analyser and send the text to it | |
// insert the returned document tones into the original | |
// tweet object | |
try { | |
let tones = await agent.post(tone_url, {text: text}); | |
tweet.tones = tones.data.document_tone.tones; | |
} catch (error) { | |
console.error(error); | |
} | |
return tweet; | |
})) | |
// Filter through all the tweets we now have | |
let joy_tweets = tweets.filter(tweet => { | |
// ensure we have actually got tones for the tweet | |
if (tweet.tones) { | |
// loop through each tone and if it is angry or protected | |
// then filter this tweet out | |
for (let i=0; i<tweet.tones.length; i++) { | |
if(tweet.tones[i].tone_id == 'anger') { | |
return false; | |
} | |
if(tweet.user.protected) { | |
return false; | |
} | |
} | |
// if the tweet is joyful then include it | |
for (let i=0; i<tweet.tones.length; i++) { | |
if(tweet.tones[i].tone_id == 'joy') { | |
return true; | |
} | |
} | |
} | |
}) | |
res.json({'tweets': joy_tweets}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment