Permalink
Browse files

initial

  • Loading branch information...
0 parents commit b4063498e89ec13afdcd06f86bc0b2f0cc2d8617 @jaz303 committed Mar 23, 2012
Showing with 99 additions and 0 deletions.
  1. +40 −0 README.mdown
  2. +59 −0 twitty.js
@@ -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();
+
+ }
+ });
+
+
@@ -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);

0 comments on commit b406349

Please sign in to comment.