Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CodeNinja89
Last active December 23, 2015 12:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodeNinja89/6635510 to your computer and use it in GitHub Desktop.
Save CodeNinja89/6635510 to your computer and use it in GitHub Desktop.
Use Codebird-JS library to periodically fetch user's home timeline. The catch: uses Apache Cordova and the app runs on Android. Tested using: Android 4.1.2, Cordova 2.9.0 and Codebird 2.4.3. Uses pin based authentication. Once authenticated, the OAuth access token and token secret are stored in the InAppBrowser's local storage hence there is no …
/* thanks to amwelles / widget.html (https://gist.github.com/amwelles/5776750)
* for the following function.
* simple and elegant.
*/
function parseText(text) {
var link = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var user = /@(\w+)/ig;
var hashTags = /#(\w+)/ig;
var desc = "";
if(link) {
desc = text.replace(link,'<a href="$1" target="_blank">$1</a>');
} if(user) {
desc = desc.replace(user,'<a href="https://twitter.com/$1" target="_blank">@$1</a>');
} if(hashTags) {
desc = desc.replace(hashTags,'<a href="https://twitter.com/search?q=%23$1" target="_blank">#$1</a>');
}
return desc;
}
function fetchTweets() {
$.ajaxSetup({
cache: false
});
var params = {
"include_rts": "1",
"count": "5"
};
var ref = window.setInterval(
function() {
cb.__call(
"statuses_homeTimeline", params,
function (reply) {
for(var key in reply){
$('#timeline').prepend('<li><p>' + reply[key].user["screen_name"] + ': ' + parseText(reply[key].text) +'</p></li>');
}
}
);
}, 300000);
}
/**************************** Let the coding begin ****************************/
var cb = new Codebird;
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
cb.setConsumerKey("YOUR CONSUMER KEY", "YOUR CONSUMER SECRET");
// check if we already have access tokens
if(localStorage.accessToken && localStorage.tokenSecret) {
// then directly setToken() and read the timeline
cb.setToken(localStorage.accessToken, localStorage.tokenSecret);
cb.__call(
"statuses_homeTimeline", {},
function (reply) {
for(var key in reply){
if(reply[key].text)
$('#timeline').append('<li><p>' + reply[key].user["screen_name"] +': ' + parseText(reply[key].text) +'</p></li>');
}
}
);
fetchTweets(); //periodically fetch tweets. Make sure that it is not too frequent as you may get rate limited.
} else { // authorize the user and ask her to get the pin.
cb.__call(
"oauth_requestToken",
{oauth_callback: "oob"},
function (reply) {
// nailed it!
cb.setToken(reply.oauth_token, reply.oauth_token_secret);
cb.__call(
"oauth_authorize", {},
function (auth_url) {
var ref = window.open(auth_url, '_blank'); // call to InAppBrowser
ref.addEventListener('exit', authorize);
}
);
}
);
}
}
function authorize() {
var pin = prompt("Enter pin");
if(pin!=null && pin!=""){
cb.__call(
"oauth_accessToken", {oauth_verifier: pin},
function(reply) {
cb.setToken(reply.oauth_token, reply.oauth_token_secret);
localStorage.accessToken = reply.oauth_token;
localStorage.tokenSecret = reply.oauth_token_secret; // twitter does not expire tokens, so no refresh token is required. Bingo!
cb.__call(
"statuses_homeTimeline", {},
function (reply) {
for (var key in reply) {
$('#timeline').append('<li><p>' + reply[key].user["screen_name"] +': ' + parseText(reply[key].text) +'</p></li>');
}
}
);
}
);
fetchTweets();
}
}
@josephoeseru
Copy link

hello

@CodeNinja89
Copy link
Author

@daljeet123
Copy link

mj

@daljeet123
Copy link

plugin for twitter integeration?????

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