Skip to content

Instantly share code, notes, and snippets.

@oldquest
Created June 19, 2013 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldquest/5812843 to your computer and use it in GitHub Desktop.
Save oldquest/5812843 to your computer and use it in GitHub Desktop.
jquery.tweet.js plugin for Pixelthrone themes
// jquery.tweet.js - See http://tweet.seaofclouds.com/ or https://github.com/seaofclouds/tweet for more info
// Copyright (c) 2008-2012 Todd Matthews & Steve Purcell
// Modified by Stan Scates for https://github.com/StanScates/Tweet.js-Mod
// Modified by Pixelthone
(function(b){if(typeof define==="function"&&define.amd){define(["jquery"],b)}else{b(jQuery)}}(function(b){b.fn.tweet=function(x){var a=b.extend({modpath:"/twitter/",username:null,list_id:null,list:null,favorites:false,query:null,avatar_size:null,count:3,fetch:null,page:1,retweets:true,intro_text:null,outro_text:null,join_text:null,auto_join_text_default:"i said,",auto_join_text_ed:"i",auto_join_text_ing:"i am",auto_join_text_reply:"i replied to",auto_join_text_url:"i was looking at",loading_text:null,refresh_interval:null,twitter_url:"twitter.com",twitter_api_url:"api.twitter.com",twitter_search_url:"search.twitter.com",template:"{avatar}{time}{join}{text}",comparator:function(c,d){return d.tweet_time-c.tweet_time},filter:function(c){return true}},x);var y=/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;function p(e,d){if(typeof e==="string"){var g=e;for(var f in d){var c=d[f];g=g.replace(new RegExp("{"+f+"}","g"),c===null?"":c)}return g}else{return e(d)}}b.extend({tweet:{t:p}});function v(c,d){return function(){var e=[];this.each(function(){e.push(this.replace(c,d))});return b(e)}}function q(c){return c.replace(/</g,"&lt;").replace(/>/g,"^&gt;")}b.fn.extend({linkUser:v(/(^|[\W])@(\w+)/gi,'$1<span class="at">@</span><a href="http://'+a.twitter_url+'/$2">$2</a>'),linkHash:v(/(?:^| )[\#]+([\w\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u00ff\u0600-\u06ff]+)/gi,' <a href="http://'+a.twitter_search_url+"/search?q=&tag=$1&lang=all"+((a.username&&a.username.length==1&&!a.list)?"&from="+a.username.join("%2BOR%2B"):"")+'" class="tweet_hashtag">#$1</a>'),makeHeart:v(/(&lt;)+[3]/gi,"<tt class='heart'>&#x2665;</tt>")});function w(c,d){return c.replace(y,function(g){var h=(/^[a-z]+:/i).test(g)?g:"http://"+g;var e=g;for(var f=0;f<d.length;++f){var i=d[f];if(i.url==h&&i.expanded_url){h=i.expanded_url;e=i.display_url;break}}return'<a href="'+q(h)+'">'+q(e)+"</a>"})}function r(c){return Date.parse(c.replace(/^([a-z]{3})( [a-z]{3} \d\d?)(.*)( \d{4})$/i,"$1,$2$4$3"))}function t(f){var d=(arguments.length>1)?arguments[1]:new Date();var c=parseInt((d.getTime()-f)/1000,10);var e="";if(c<1){e="just now"}else{if(c<60){e=c+" seconds ago"}else{if(c<120){e="about a minute ago"}else{if(c<(45*60)){e="about "+(parseInt(c/60,10)).toString()+" minutes ago"}else{if(c<(2*60*60)){e="about an hour ago"}else{if(c<(24*60*60)){e="about "+(parseInt(c/3600,10)).toString()+" hours ago"}else{if(c<(48*60*60)){e="about a day ago"}else{e="about "+(parseInt(c/86400,10)).toString()+" days ago"}}}}}}}return e}function u(c){if(c.match(/^(@([A-Za-z0-9-_]+)) .*/i)){return a.auto_join_text_reply}else{if(c.match(y)){return a.auto_join_text_url}else{if(c.match(/^((\w+ed)|just) .*/im)){return a.auto_join_text_ed}else{if(c.match(/^(\w*ing) .*/i)){return a.auto_join_text_ing}else{return a.auto_join_text_default}}}}}function z(){var f=a.modpath,e=(a.fetch===null)?a.count:a.fetch,c={include_entities:1};if(a.list){return{host:a.twitter_api_url,url:"/1.1/lists/statuses.json",parameters:b.extend({},c,{list_id:a.list_id,slug:a.list,owner_screen_name:a.username,page:a.page,count:e,include_rts:(a.retweets?1:0)})}}else{if(a.favorites){return{host:a.twitter_api_url,url:"/1.1/favorites/list.json",parameters:b.extend({},c,{list_id:a.list_id,screen_name:a.username,page:a.page,count:e})}}else{if(a.query===null&&a.username.length===1){return{host:a.twitter_api_url,url:"/1.1/statuses/user_timeline.json",parameters:b.extend({},c,{screen_name:a.username,page:a.page,count:e,include_rts:(a.retweets?1:0)})}}else{var d=(a.query||"from:"+a.username.join(" OR from:"));return{host:a.twitter_search_url,url:"/search.json",parameters:b.extend({},c,{page:a.page,q:d,rpp:e})}}}}}function o(d,c){if(c){return("user" in d)?d.user.profile_image_url_https:o(d,false).replace(/^http:\/\/[a-z0-9]{1,3}\.twimg\.com\//,"https://s3.amazonaws.com/twitter_production/")}else{return d.profile_image_url||d.user.profile_image_url}}function s(d){var c={};c.item=d;c.source=d.source;c.name=d.from_user_name||d.user.name;c.screen_name=d.from_user||d.user.screen_name;c.avatar_size=a.avatar_size;c.avatar_url=o(d,(document.location.protocol==="https:"));c.retweet=typeof(d.retweeted_status)!="undefined";c.tweet_time=r(d.created_at);c.join_text=a.join_text=="auto"?u(d.text):a.join_text;c.tweet_id=d.id_str;c.twitter_base="http://"+a.twitter_url+"/";c.user_url=c.twitter_base+c.screen_name;c.tweet_url=c.user_url+"/status/"+c.tweet_id;c.reply_url=c.twitter_base+"intent/tweet?in_reply_to="+c.tweet_id;c.retweet_url=c.twitter_base+"intent/retweet?tweet_id="+c.tweet_id;c.favorite_url=c.twitter_base+"intent/favorite?tweet_id="+c.tweet_id;c.retweeted_screen_name=c.retweet&&d.retweeted_status.user.screen_name;c.tweet_relative_time=t(c.tweet_time);c.entities=d.entities?(d.entities.urls||[]).concat(d.entities.media||[]):[];c.tweet_raw_text=c.retweet?("RT @"+c.retweeted_screen_name+" "+d.retweeted_status.text):d.text;c.tweet_text=b([w(c.tweet_raw_text,c.entities)]).linkUser().linkHash()[0];c.tweet_text_fancy=b([c.tweet_text]).makeHeart()[0];c.user=p('<a class="tweet_user" href="{user_url}">{screen_name}</a>',c);c.join=a.join_text?p(' <span class="tweet_join">{join_text}</span> ',c):" ";c.avatar=c.avatar_size?p('<a class="tweet_avatar" href="{user_url}"><img src="{avatar_url}" height="{avatar_size}" width="{avatar_size}" alt="{screen_name}\'s avatar" title="{screen_name}\'s avatar" border="0"/></a>',c):"";c.time=p('<span class="tweet_time"><a href="{tweet_url}" title="view tweet on twitter">{tweet_relative_time}</a></span>',c);c.text=p('<span class="tweet_text">{tweet_text_fancy}</span>',c);c.reply_action=p('<a class="tweet_action tweet_reply" href="{reply_url}">reply</a>',c);c.retweet_action=p('<a class="tweet_action tweet_retweet" href="{retweet_url}">retweet</a>',c);c.favorite_action=p('<a class="tweet_action tweet_favorite" href="{favorite_url}">favorite</a>',c);return c}return this.each(function(g,d){var e=b('<ul class="tweet_list">');var f='<p class="tweet_intro">'+a.intro_text+"</p>";var h='<p class="tweet_outro">'+a.outro_text+"</p>";var c=b('<p class="loading">'+a.loading_text+"</p>");if(a.username&&typeof(a.username)=="string"){a.username=[a.username]}b(d).unbind("tweet:load").bind("tweet:load",function(){if(a.loading_text){b(d).empty().append(c)}b.ajax({dataType:"json",type:"post",async:false,url:a.modpath||"/twitter/",data:{request:z()},success:function(j,l){if(j.message){console.log(j.message)}var k=j.response;b(d).empty().append(e);if(a.intro_text){e.before(f)}e.empty();if(k.statuses!==undefined){resp=k.statuses}else{if(k.results!==undefined){resp=k.results}else{resp=k}}var i=b.map(resp,s);i=b.grep(i,a.filter).sort(a.comparator).slice(0,a.count);e.append(b.map(i,function(m){return"<li>"+p(a.template,m)+"</li>"}).join("")).children("li:first").addClass("tweet_first").end().children("li:odd").addClass("tweet_even").end().children("li:even").addClass("tweet_odd");if(a.outro_text){e.after(h)}b(d).trigger("loaded").trigger((i?"empty":"full"));if(a.refresh_interval){window.setTimeout(function(){b(d).trigger("tweet:load")},1000*a.refresh_interval)}if(typeof tweet_callback=="function"){tweet_callback()}}})}).trigger("tweet:load")})}}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment