Created
August 13, 2015 00:59
-
-
Save horacioh/a067c91d60fd9e215e09 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
function getTweets() { | |
var promise = new Parse.Promise(); | |
var Tweets = Parse.Object.extend("Tweets"); | |
var query = new Parse.Query(Tweets); | |
//GET SEARCH URL HERE: https://dev.twitter.com/rest/public/search | |
var urlLink = "SEARCH_URL_HERE"; | |
query.descending("id_str"); | |
//query.ascending("id_str"); | |
query.limit(1); | |
query.find().then(function(results) { | |
if (results.length > 0) { | |
var lastTweet = results[0].get("id_str"); | |
console.log(lastTweet); | |
urlLink = urlLink + "&since_id=" + lastTweet; | |
//urlLink = urlLink + "&max_id=" + lastTweet; | |
} else { | |
console.log("NO RESULTS :("); | |
} | |
var consumerSecret = "SECRET"; | |
var tokenSecret = "TOKEN"; | |
var oauth_consumer_key = "AUTH_KEY"; | |
var oauth_token = "AUTH_TOKEN"; | |
var nonce = oauth.nonce(32); | |
var ts = Math.floor(new Date().getTime() / 1000); | |
var timestamp = ts.toString(); | |
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 resp = JSON.parse(httpResponse.text); | |
var data = resp.statuses; | |
var tweets = new Array(); | |
for (var i = 0; i < data.length; i++) { | |
var Tweets = Parse.Object.extend("Tweets"), | |
tweet = new Tweets(); | |
var content = data[i]; | |
tweet.set("id_str", content.id_str); | |
tweet.set("author", content.user.screen_name); | |
tweet.set("author_name", content.user.name); | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment