Chris Mendez in Javascript, For Developers

Grab data from a Twitter Proxy

Pull cached Twitter data from http://socialproxy.classicalkusc.org and present it as HTML.

/**
* -------------------------------------------------------
* KUSC.org Twitter Feed
* -------------------------------------------------------
* 
* Version: 1
* Created: cmendez@kusc.org
* Modified: 6/12/2012
* 
* -------------------------------------------------------
* Notes:
* Purpose: Display @classicalkusc on www.kusc.org
* 
* */

function feed(tweets) {  
  var statusHTML = [];

  var tweets = tweets.items;
  //console.log( tweets );
  $.each( tweets, function( i, value) {
    //console.log( i, value );
    var username = tweets[i].user.screen_name;
    //console.log( username );

    var status = tweets[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,function(url) {
      return '<a href="'+url+'" target="_blank">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
    }).replace(/\B#([_a-z0-9]+)/ig, function(hashtag) {
      return '<a href="http://twitter.com/search?q=%23'+hashtag.substring(1)+'" target="_blank">'+hashtag.charAt(0)+hashtag.substring(1)+'</a>';
    });
    //Custom Design
    var icon = "http://www.kusc.org/Pics/icons/icon_twitter.jpg";
    var icon_sm = "http://www.kusc.org/Pics/icons/icon_twitter_small.jpg"; 
    //STYLE="list-style-image: url(http://www.kusc.org/Pics/icons/icon_twitter_small.jpg)
    var img  = $("<img>").attr( { src: icon, border: 0 } );
        //img.attr( { width: 20, height: 17 } );

    statusHTML.push( '<img border="0" src="http://www.kusc.org/Pics/icons/icon_twitter.jpg" /><ul><li><span>'+status+'</span> <span style="font-size:85%"><a href="http://twitter.com/'+username+'/status/'+tweets[i].id_str+'" target="_blank"><em>('+relative_time(tweets[i].created_at)+')</em></a> <a class="reply" href="http://twitter.com/intent/tweet?in_reply_to='+tweets[i].id_str+'">Reply</a> <a class="retweet" href="http://twitter.com/intent/retweet?tweet_id='+tweets[i].id_str+'">Retweet</a></span></li></ul><br />');
  });
  $('.tweet').html( statusHTML.join('') );
  $('.tweet img').css({ "float": "left", "margin": "10px 10px 0 0" });
  $('.tweet').css({ "padding-top": "10px" });
  $(".tweet ul").css({ "list-style-type": "none" });
}

//twitter time
function relative_time(time_value) {  
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
//OnLoad
$(document).ready( function() {
  //load twitter feed -- jquerified original code from http://twitter.com/javascripts/blogger.js
  var url = "https://api.twitter.com/1/statuses/user_timeline.json?screen_name=classicalkusc&count=3&include_rts=1&callback=?";
  var url = "http://socialproxy.classicalkusc.org/twitter?limit=3&callback=?";

  //load twitter feed -- jquerified original code from http://twitter.com/javascripts/blogger.js
  $.getJSON( url, function(){});
});