Skip to content

Instantly share code, notes, and snippets.

@simsketch
Created January 29, 2016 01:57
Show Gist options
  • Save simsketch/2d96984a7d79410fb5ba to your computer and use it in GitHub Desktop.
Save simsketch/2d96984a7d79410fb5ba to your computer and use it in GitHub Desktop.
Parse fetch tweets
var _ = require('underscore');
var oauth = require("cloud/libs/oauth.js");
Parse.Cloud.job("twitterFeed", function(request, status) {
Parse.Cloud.useMasterKey();
// These are the Twitter users you want Tweets from, excluding the '@'
var screenNames = [
"abcnewsone"
];
var promise = Parse.Promise.as();
_.each(screenNames, function(screenName){
promise = promise.then(function(){
return getTweets(screenName);
});
});
Parse.Promise.when(promise).then(function(){
status.success("Tweets saved");
}, function(error){
status.error("Tweets failed to update");
});
});
function getTweets(screenName){
var promise = new Parse.Promise();
var Tweets = Parse.Object.extend("Tweets");
var query = new Parse.Query(Tweets);
query.equalTo("screen_name", screenName);
query.descending("id_str")
query.limit(1);
query.find().then(function(results){
if(results.length === 0){
// If this is the first time this script has run, then we need don't
// need to have the since_id param
var urlLink = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" + screenName + "&exclude_replies=true&include_rts=false&count=100";
} else {
var lastTweetId = results[0].get("id_str");
var urlLink = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" + screenName + "&exclude_replies=true&include_rts=false&since_id=" + lastTweetId;
};
var consumerSecret = "a2kqcsRo4dotjtM9ZhbNZ79FiuirrkPlSDcrALvLdx750vn3nJ";
var tokenSecret = "9gxA89GGuCSRbfgnI4L6jZhYBG0OiOFnwrWeYKN3dlvFn";
var oauth_consumer_key = "55h0VAWjPQhk4zr8aY6ros4CV";
var oauth_token = "26132109-1sLvqptBmtE8f4IBdRZS9vaEExT6TPCridP0pqG83";
var nonce = oauth.nonce(32);
var ts = Math.floor(new Date().getTime() / 1000);
var timestamp = ts.toString();
var accessor = {
consumerSecret: consumerSecret,
tokenSecret: tokenSecret
};
var params = {
oauth_version: "1.0",
oauth_consumer_key: oauth_consumer_key,
oauth_token: oauth_token,
oauth_timestamp: timestamp,
oauth_nonce: nonce,
oauth_signature_method: "HMAC-SHA1"
};
var message = {
method: "GET",
action: urlLink,
parameters: params
};
oauth.SignatureMethod.sign(message, accessor);
var normPar = oauth.SignatureMethod.normalizeParameters(message.parameters);
var baseString = oauth.SignatureMethod.getBaseString(message);
var sig = oauth.getParameter(message.parameters, "oauth_signature") + "=";
var encodedSig = oauth.percentEncode(sig);
Parse.Cloud.httpRequest({
method: "GET",
url: urlLink,
headers: {
Authorization: 'OAuth oauth_consumer_key="'+oauth_consumer_key+'", oauth_nonce=' + nonce + ', oauth_signature=' + encodedSig + ', oauth_signature_method="HMAC-SHA1", oauth_timestamp=' + timestamp + ',oauth_token="'+oauth_token+'", oauth_version="1.0"'
},
success: function(httpResponse) {
var data = JSON.parse(httpResponse.text);
var tweets = new Array();
for (var i = 0; i < data.length; i++) {
var Tweets = Parse.Object.extend("Tweets"),
tweet = new Tweets(),
content = data[i];
tweet.set("text", content.text);
tweet.set("source", content.source);
tweet.set("retweet_count", content.retweet_count);
tweet.set("created_at", content.created_at );
tweet.set("favorite_count", content.favorite_count);
tweet.set("retweeted", content.retweeted);
tweet.set("id_str", content.id_str);
tweet.set("screen_name", screenName);
tweets.push(tweet);
};
Parse.Object.saveAll(tweets, {
success: function(objs) {
promise.resolve();
},
error: function(error) {
console.log(error);
promise.reject(error.message);
}
});
},
error: function(error) {
console.log(error);
promise.reject(error.message);
}
});
});
return promise;
}
@hjgabrielsen
Copy link

I know this is really old.. But i have this old script fetching tweets, but it truncates at 126 characters , eventhough the actual tweet is much longer.

I tried the following: tweet_mode='extended'

var urlLink = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" + screenName + "&exclude_replies=true&include_rts=false&tweet_mode='extended'&count=100&since_id=" + lastTweetId;

But no luck.. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment