-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jason Frame
committed
Mar 23, 2012
0 parents
commit b406349
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Twitty - An Unsophisticated Twitter Timeline Client (for browsers) | ||
|
||
© 2012 Jason Frame [ [jason@onehackoranother.com](mailto:jason@onehackoranother.com) / [@jaz303](http://twitter.com/jaz303) ] | ||
|
||
## Overview | ||
|
||
Very simple, no-frills method of accessing a user's timeline in the browser. For displaying "latest tweet" | ||
lists and the like. | ||
|
||
No dependencies. | ||
|
||
## Try it out | ||
|
||
var twitty = new Twitty(); | ||
twitty.getUserTimeline('jaz303', function(tweets) { | ||
for (var i = 0; i < tweets.length; i++) { | ||
var tweet = tweets[i]; | ||
// Each tweet is a JSON object as returned by the standard Twitter JSON API. | ||
// The prototype is augmented with a few extra convenience methods: | ||
// Return tweet text with URLs and screen-names converted to hyperlinks: | ||
tweet.linkify(); | ||
// Return the permalink to this tweet: | ||
tweet.permaLink(); | ||
// Return Twitter intent links for the following actions: | ||
tweet.replyLink(); | ||
tweet.retweetLink(); | ||
tweet.favouriteLink(); | ||
// Returns an English approximation of the relative time of this tweet: | ||
tweet.relativeTimeInWords(); | ||
} | ||
}); | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(function(exports) { | ||
|
||
var INTERVALS = [[86400, 'day'], [3600, 'hour'], [60, 'minute'], [1, 'seconds']], | ||
INTENT = 'https://twitter.com/intent/'; | ||
|
||
var nextID = 0; | ||
|
||
var tweetMethods = { | ||
linkify: function() { | ||
return this.text.replace(/@(\w+)/g, function() { | ||
return "<a href='http://twitter.com/" + RegExp.$1 + "'>@" + RegExp.$1 + "</a>"; | ||
}).replace(/(https?:\/\/[^\s]+)/g, function() { // regex needs work | ||
return "<a href='" + RegExp.$1 + "'>" + RegExp.$1 + "</a>"; | ||
}); | ||
}, | ||
|
||
permaLink: function() { return 'https://twitter.com/' + this.user.screen_name + '/status/' + this.id; }, | ||
replyLink: function() { return INTENT + 'tweet?in_reply_to=' + this.id; }, | ||
retweetLink: function() { return INTENT + 'retweet?tweet_id=' + this.id; }, | ||
favouriteLink: function() { return INTENT + 'favorite?tweet_id=' + this.id; }, | ||
|
||
relativeTimeInWords: function() { | ||
var diff = Math.floor((new Date() - new Date(this.created_at)) / 1000); | ||
|
||
if (diff < 0) return '???'; | ||
|
||
for (var i = 0; i < INTERVALS.length; i++) { | ||
var val = diff / INTERVALS[i][0]; | ||
if (val >= 2) return '' + Math.floor(val) + ' ' + INTERVALS[i][1] + 's ago'; | ||
if (val >= 0.8) return '1 ' + INTERVALS[i][1] + ' ago'; | ||
} | ||
} | ||
}; | ||
|
||
function Twitty() {} | ||
Twitty.prototype = { | ||
getUserTimeline: function(screenName, callback) { | ||
var localCallback = function(tweets) { | ||
tweets = tweets || []; | ||
console.log(tweets); | ||
for (var i = 0; i < tweets.length; i++) { | ||
tweets[i].prototype = tweetMethods; | ||
tweets[i].__proto__ = tweetMethods; | ||
} | ||
callback(tweets); | ||
} | ||
|
||
var callbackName = '_twitter_cb_' + (nextID++); | ||
window[callbackName] = localCallback; | ||
|
||
var script = document.createElement('script'); | ||
script.src = "http://twitter.com/statuses/user_timeline/" + screenName + ".json?callback=" + callbackName + "&count=5&include_rts=1"; | ||
document.body.appendChild(script); | ||
} | ||
} | ||
|
||
exports.Twitty = Twitty; | ||
|
||
})(this); |