forked from elektronaut/jquery.livetwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.livetwitter.min.js
1 lines (1 loc) · 4.94 KB
/
jquery.livetwitter.min.js
1
(function(a){if(!a.fn.reverse){a.fn.reverse=function(){return this.pushStack(this.get().reverse(),arguments)}}a.fn.liveTwitter=function(d,b,e){var c=this;a(this).each(function(){var f={};if(this.twitter){f=a.extend(this.twitter.settings,b);this.twitter.settings=f;if(d){this.twitter.query=d}this.twitter.limit=f.limit;this.twitter.mode=f.mode;if(this.twitter.interval){this.twitter.refresh()}if(e){this.twitter.callback=e}}else{f=a.extend({mode:"search",rate:15000,limit:10,imageSize:24,refresh:true,timeLinks:true,retweets:false,service:""},b);if(typeof f.showAuthor==="undefined"){f.showAuthor=(f.mode==="user_timeline")?false:true}if(!window.twitter_callback){window.twitter_callback=function(){return true}}this.twitter={settings:f,query:d,limit:f.limit,mode:f.mode,interval:false,container:this,lastTimeStamp:0,callback:e,relativeTime:function(i){var h=Date.parse(i);var k=(Date.parse(Date())-h)/1000;var j="";if(k<60){j=k+" seconds ago"}else{if(k<120){j="a minute ago"}else{if(k<(45*60)){j=(parseInt(k/60,10)).toString()+" minutes ago"}else{if(k<(90*60)){j="an hour ago"}else{if(k<(24*60*60)){j=""+(parseInt(k/3600,10)).toString()+" hours ago"}else{if(k<(48*60*60)){j="a day ago"}else{j=(parseInt(k/86400,10)).toString()+" days ago"}}}}}}return j},refreshTime:function(){var h=this;a(h.container).find("span.time").each(function(){var i=h.settings.timeLinks?a(this).find("a"):a(this);i.html(h.relativeTime(this.timeStamp))})},refresh:function(h){var k=this;if(this.settings.refresh||h){var i="";var n={};if(k.mode==="search"){if(this.query&&this.query!==""){n.q=this.query}if(this.settings.geocode){n.geocode=this.settings.geocode}if(this.settings.lang){n.lang=this.settings.lang}if(this.settings.rpp){n.rpp=this.settings.rpp}else{n.rpp=this.settings.limit}var l=[];for(var m in n){if(n.hasOwnProperty(m)){l[l.length]=m+"="+encodeURIComponent(n[m])}}l=l.join("&");if(f.service.length>0){i="http://"+f.service+"/api/search.json?"}else{i="http://search.twitter.com/search.json?"}i+=l+"&callback=?"}else{if(k.mode==="user_timeline"||k.mode==="home_timeline"){if(f.service.length>0){i="http://"+f.service+"/api/statuses/"+k.mode+"/"+encodeURIComponent(this.query)+".json?count="+k.limit+"&callback=?"}else{i="http://api.twitter.com/1/statuses/"+k.mode+"/"+encodeURIComponent(this.query)+".json?count="+k.limit+"&callback=?";if(k.mode==="user_timeline"&&this.settings.retweets){i+="&include_rts=1"}}}else{if(k.mode==="list"){var o=encodeURIComponent(this.query.user);var j=encodeURIComponent(this.query.list);i="http://api.twitter.com/1/"+o+"/lists/"+j+"/statuses.json?per_page="+k.limit+"&callback=?"}}}a.getJSON(i,function(q){var p=null;if(k.mode==="search"){p=q.results}else{p=q}var r=0;a(p).reverse().each(function(){var C="";var x="";var w="";var y="";if(k.mode==="search"){C=this.from_user;x=this.profile_image_url;w=this.created_at}else{C=this.user.screen_name;x=this.user.profile_image_url;w=this.created_at.replace(/^(\w+)\s(\w+)\s(\d+)(.*)(\s\d+)$/,"$1, $3 $2$5$4")}if(f.service.length>0){y="http://"+f.service+"/notice/"+this.id}else{y="http://twitter.com/"+C+"/statuses/"+this.id}var z=this.user;var u=this.text.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/,function(D){return D.link(D)});if(f.service.length>0){u=u.replace(/@[A-Za-z0-9_]+/g,function(D){return D.link("http://"+f.service+"/"+D.replace(/^@/,""))})}else{u=u.replace(/@[A-Za-z0-9_]+/g,function(D){return D.link("http://twitter.com/"+D.replace(/^@/,""))})}if(f.service.length>0){u=u.replace(/#[A-Za-z0-9_\-]+/g,function(D){return D.link("http://http://"+f.service+"/search/notice?q="+D.replace(/^#/,"%23"))})}else{u=u.replace(/#[A-Za-z0-9_\-]+/g,function(D){return D.link("http://search.twitter.com/search?q="+D.replace(/^#/,"%23"))})}if(!k.settings.filter||k.settings.filter(this)){if(Date.parse(w)>k.lastTimeStamp){r+=1;var B='<div class="tweet tweet-'+this.id+'">';if(k.settings.showAuthor){var A="";if(f.service.length>0){A="http://"+f.service+"/"+C}else{A="http://twitter.com/"+C}B+='<img width="'+f.imageSize+'" height="'+f.imageSize+'" src="'+x+'" /><p class="text"><span class="username"><a href="'+A+'">'+C+"</a>:</span> "}else{B+='<p class="text"> '}var s=k.relativeTime(w);var t=k.settings.timeLinks?'<a href="'+y+'">'+s+"</a>":s;B+=u+' <span class="time">'+t+"</span></p></div>";a(k.container).prepend(B);var v=w;a(k.container).find("span.time:first").each(function(){this.timeStamp=v});if(!h){a(k.container).find(".tweet-"+this.id).hide().fadeIn()}k.lastTimeStamp=Date.parse(w)}}});if(r>0){a(k.container).find("div.tweet:gt("+(k.limit-1)+")").remove();if(k.callback){k.callback(c,r)}a(c).trigger("tweets")}})}},start:function(){var h=this;if(!this.interval){this.interval=setInterval(function(){h.refresh()},h.settings.rate);this.refresh(true)}},stop:function(){if(this.interval){clearInterval(this.interval);this.interval=false}},clear:function(){a(this.container).find("div.tweet").remove();this.lastTimeStamp=null}};var g=this.twitter;this.timeInterval=setInterval(function(){g.refreshTime()},5000);this.twitter.start()}});return this}})(jQuery);