Last active
December 11, 2015 12:48
-
-
Save Ultrabenosaurus/4603200 to your computer and use it in GitHub Desktop.
A script to pull the latest tweets from a given Twitter user, parse it for links, usernames and hashtags, then turn the date/time into a link to the tweet.
This file contains 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
jQuery(document).ready(function(){ | |
twitter(); | |
}); | |
/* | |
* twitter() | |
* | |
* only parameter is an object of options: | |
* | |
* profile (string) The profile to pull tweets from | |
* element (string) The ID of the HTML element to contain output | |
* tweets (integer) The total number of tweets you wish to display | |
* retweets (boolean) Whether or not you want retweets in the output | |
* limit (integer) Used automatically when retweets not allowed, contains original value of tweets variable | |
* actions (object (or false for no actions)) An array of actions to display and a string to separate them with | |
* separator (string) Separating character(s) | |
* actions (array) The actions to perform out of: profile, reply, retweet, favorite | |
**/ | |
function twitter(opts){ | |
profile = ((typeof opts !== 'undefined' && typeof opts.profile !== 'undefined') ? opts.profile : "twitter"); | |
element = ((typeof opts !== 'undefined' && typeof opts.element !== 'undefined') ? opts.element : "twitter"); | |
tweets = ((typeof opts !== 'undefined' && typeof opts.tweets !== 'undefined') ? opts.tweets : 2); | |
retweets = ((typeof opts !== 'undefined' && typeof opts.retweets !== 'undefined' && opts.retweets == false) ? "0" : "true"); | |
limit = ((typeof opts !== 'undefined' && typeof opts.limit !== 'undefined') ? opts.limit : tweets); | |
if(typeof opts !== 'undefined' && typeof opts.actions == 'object'){ | |
actions = opts.actions; | |
actions.separator = ((typeof actions.separator == 'undefined') ? " | " : ((typeof actions.separator == 'string') ? actions.separator : " | ")); | |
actions.actions = ((Object.prototype.toString.call( actions.actions ) === '[object Array]') ? actions.actions : ['reply', 'retweet', 'favorite']); | |
actions = ((actions.actions.length > 0) ? actions : false); | |
} else { | |
actions = { | |
separator: " | ", | |
actions: ['profile', 'reply', 'retweet', 'favorite'] | |
}; | |
} | |
if(actions){ | |
action_rules = { | |
profile: ['user', 'screen_name'], | |
reply: ['tweet', 'in_reply_to'], | |
retweet: ['retweet', 'tweet_id'], | |
favorite: ['favorite', 'tweet_id'], | |
favourite: ['favorite', 'tweet_id'] | |
}; | |
} | |
_actions_backup = actions; | |
elem = jQuery("#"+element); | |
if(elem){ | |
jQuery.getJSON("https://api.twitter.com/1/statuses/user_timeline/"+profile+".json?count="+tweets+"&include_rts="+retweets+"&callback=?", function(data) { | |
if(data.length == limit){ | |
months = ['Januray', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; | |
String.prototype.parseURL = function() { | |
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_:%&~\?\/.=]+/g, function(url) { | |
return url.link(url); | |
}); | |
}; | |
String.prototype.parseUsername = function() { | |
return this.replace(/[@]+[A-Za-z0-9\-_]+/g, function(u) { | |
var username = u.replace("@",""); | |
return u.link("http://twitter.com/"+username); | |
}); | |
}; | |
String.prototype.parseHashtag = function() { | |
return this.replace(/[#]+[A-Za-z0-9\-_]+/g, function(t) { | |
var tag = t.replace("#","%23"); | |
return t.link("http://search.twitter.com/search?q="+tag); | |
}); | |
}; | |
if(actions && actions.actions.indexOf('profile') >= 0){ | |
elem.append("<div id='action_profile'><a target='_blank' href='https://twitter.com/intent/"+action_rules.profile[0]+"?"+action_rules.profile[1]+"="+profile+"'>"+profile+"</a></div>"); | |
actions.actions.splice(actions.actions.indexOf('profile'), 1); | |
} | |
for(var i = 0; i < data.length; i++){ | |
elem.append('<div id="icon" class="tweet_'+i+'"></div>'); | |
var tweet = data[i].text.parseURL().parseUsername().parseHashtag(); | |
elem.append("<div id='tweet_"+data[i].id_str+"' class='tweet_"+i+"'>"+tweet+"</div>"); | |
var tdate = new Date(data[i].created_at); | |
var sdate = months[tdate.getMonth()] + " " + tdate.getDate() + ", " + tdate.getFullYear() + " - " + tdate.getHours() + ":" + tdate.getMinutes(); | |
var tweet_link = "http://twitter.com/"+profile+"/status/"+data[i].id_str; | |
elem.append("<div id='datetime_"+data[i].id_str+"' class='tweet_"+i+"'><a href='"+tweet_link+"'>"+sdate+"</a></div>"); | |
if(actions){ | |
elem.append("<div id='actions_"+data[i].id_str+"' class='tweet_"+i+"'>"); | |
for(var j = 0, l = actions.actions.length; j < l; j++){ | |
if(j > 0 && j < l){ | |
elem.append(actions.separator); | |
} | |
_act = actions.actions[j]; | |
_link = "<a target='_blank' href='https://twitter.com/intent/"+action_rules[_act][0]+"?"+action_rules[_act][1]+"="+data[i].id_str+"'>"+_act+"</a>"; | |
elem.append("<span id='"+_act+"_"+data[i].id_str+"'>"+_link+"</span>"); | |
} | |
elem.append('</div>'); | |
} | |
} | |
} else { | |
twitter({profile: profile, element: element, tweets: (tweets+1), retweets: retweets, limit: limit, actions: _actions_backup}); | |
} | |
}); | |
} | |
} |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>Last Tweet</title> | |
</head> | |
<body> | |
<div id="twitter"></div> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> | |
<script src="latestTweets.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment