Created
June 20, 2013 18:34
Azure Mobile Services scheduled job to retrieve tweets from Twitter REST API 1.1
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
var updatesTable = tables.getTable('Updates'); | |
var request = require('request'); | |
var url = "https://api.twitter.com/1.1/search/tweets.json?q=%23mobileservices&result_type=recent"; | |
var consumerKey = '[your consumer key]', | |
accessToken= '[your access token]', | |
consumerSecret = '[your consumer secret]', | |
accessTokenSecret = '[your access token secret]'; | |
function getUpdates() { | |
// Check what is the last tweet we stored when the job last ran | |
// and ask Twitter to only give us more recent tweets | |
appendLastTweetId( | |
url, | |
function twitterUrlReady(url){ | |
request.get({ | |
url: url, | |
oauth: { | |
consumer_key: consumerKey, | |
consumer_secret: consumerSecret, | |
token: accessToken, | |
token_secret: accessTokenSecret | |
} | |
}, function tweetsLoaded (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var results = JSON.parse(body).statuses; | |
if(results){ | |
console.log('Fetched new results from Twitter'); | |
results.forEach(function visitResult(tweet){ | |
if(!filterOutTweet(tweet)){ | |
var update = { | |
twitterId: tweet.id, | |
text: tweet.text, | |
author: tweet.user.screen_name, | |
date: tweet.created_at, | |
photo: tweet.user.profile_image_url | |
}; | |
updatesTable.insert(update); | |
} | |
}); | |
} | |
} else { | |
console.error('Could not contact Twitter'); | |
} | |
}); | |
}); | |
} | |
// Find the largest (most recent) tweet ID we have already stored | |
// (if we have stored any) and ask Twitter to only return more | |
// recent ones | |
function appendLastTweetId(url, callback){ | |
updatesTable | |
.orderByDescending('twitterId') | |
.read({success: function readUpdates(updates){ | |
if(updates.length){ | |
callback(url + '&since_id=' + (updates[0].twitterId + 2)); | |
} else { | |
callback(url); | |
} | |
}}); | |
} | |
function filterOutTweet(tweet){ | |
// Remove retweets and replies | |
return (tweet.text.indexOf('RT') === 0 || tweet.to_user_id); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment