Skip to content

Instantly share code, notes, and snippets.

@bigdrum
Created January 11, 2009 14:06
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 bigdrum/45703 to your computer and use it in GitHub Desktop.
Save bigdrum/45703 to your computer and use it in GitHub Desktop.
//Usage: http://sites.google.com/site/coosgadgets/2ii
//this code is based on http://www.matthias-schuetz.eu/twitter-ubiquity.html
twitter_template = "\
<div style=\"overflow: auto; height: 450px;\"> \
{for entry in entries} \
<div style=\"padding:0;margin:0;padding-top:2px;border-bottom-style: dashed; border-width: 1px; font-family: sans-serif;\"> \
<a href=\"http://twitter.com/home?in_reply_to=${entry.user.screen_name}&in_reply_to_status_id=${entry.id}&status=%40${entry.user.screen_name}+\" ><img src=\"${entry.user.profile_image_url}\" style=\"height: 2em; float: left;margin-right:3px; margin-top:4px;border:none;\"/></a> \
<span style=\"font-weight: bold\">${entry.user.screen_name}</span> \
<span>${entry.text}</span> \
<div style=\"clear:both;\"></div> \
</div> \
{/for}</div>";
function get_twitter_friends( callback ) {
//displayMessage("reloading twitter friend list");
jQuery.get("http://twitter.com/statuses/friends.json", {}, function(data){
var friends = {};
for (var i in data) {
var friend = data[i];
friends[friend.screen_name] = friend.screen_name;
}
callback(friends);
}, "json");
}
var noun_twitter_friend = {
_name: "twitter friend",
friend_list: null,
callback:function(friends) {
//noun_twitter_friend.friend_list = friends;
CmdUtils.friend_list = friends;
},
suggest: function(text, html) {
if (CmdUtils.friend_list == null) {
get_twitter_friends( this.callback);
return [];
}
if( text.length < 2 )
return [];
var suggestions = [];
for ( var c in CmdUtils.friend_list ) {
if (c.match(text, "i"))
suggestions.push(CmdUtils.makeSugg(CmdUtils.friend_list[c]));
}
return suggestions.splice(0, 5);
}
};
/*
* ugly hack.. but this is the only way i found, such that i
* could persist any kind of value between calls to the preview
* function.
*/
CmdUtils.tw_timeline = null;
CmdUtils.tw_timeline_update = null;
CmdUtils.ti_curr_url = null;
CmdUtils.ti_shorten_url = null;
CmdUtils.CreateCommand({
name:"2ii",
author: {name: "Gu YE", email: "cooyeah@gmail.com"},
description: "An improved version of twitter command.",
homepage: "http://sites.google.com/site/coosgadgets/2ii",
license: "MIT",
takes:{"message":noun_arb_text},
modifiers:{"@":noun_twitter_friend},
update_timeline: function(cont){
var now = new Date();
// if timeline up to date, call continuation and return
if ((CmdUtils.tw_timeline != null) && (CmdUtils.tw_timeline_update != null) && ((now - CmdUtils.tw_timeline_update) < 120000)){ // 2mins
cont(CmdUtils.tw_timeline);
} else {
// else, update and call continuation
//displayMessage("updating twitter timeline");
CmdUtils.tw_timeline_update = now;
jQuery.get("http://twitter.com/statuses/friends_timeline.json", {count:10},
function(data){
var posts=data.slice(0,10);
for(var entry in posts){
posts[entry]["text"]=posts[entry]["text"].replace(/(http:\/\/[^\s]+)/i,"<a href=\"$1\">$1</a>");
}
CmdUtils.tw_timeline = CmdUtils.renderTemplate(twitter_template, {entries: posts});;
CmdUtils.tw_timeline_update = new Date();
//displayMessage("update set to"+ this.timeline_update);
cont(CmdUtils.tw_timeline);
},
"json");
}
},
preview: function(p, query, mods) {
var qtext="";
p.innerHTML="";
qtext=this.preprocess(query.text,true);
if (mods["@"] && mods["@"].text){
qtext= "@"+mods["@"].text+" "+qtext;
}
var rem = 140 - query.text.length;
this.update_timeline(function (cur_timeline) {
p.innerHTML = qtext+"<p style=\"font-size:smaller;\">Remaining: "+rem+" chars. Type #l for current URL. Click avatar to reply.</p>"+cur_timeline;
});
},
preprocess: function(updatetext,shorten) {
if(shorten && updatetext.match(/(^|\s)#l(\s|$)/)){
if(CmdUtils.ti_curr_url!=context.focusedWindow.location){
jQuery.ajax({
type: "GET",async:false,
url: 'http://is.gd/api.php',
data: { 'longurl' : context.focusedWindow.location+"" },
error: function() {
displayMessage("Shorten url error.");
CmdUtils.ti_shorten_url=context.focusedWindow.location;
},
success: function(shortURL) {
//displayMessage("Shorten:"+shortURL);
CmdUtils.ti_shorten_url = shortURL;
}
});
CmdUtils.ti_curr_url=context.focusedWindow.location;
}
return updatetext.replace(/(^|\s)#l(\s|$)/,"$1"+CmdUtils.ti_shorten_url+"$2");
}else{
return updatetext;
}
},
execute: function(directObj, mods) {
var statusText = this.preprocess(directObj.text,true);
if(statusText.length < 1) {
displayMessage("Twitter requires a status to be entered");
return;
}
if (mods["@"] && mods["@"].text)
statusText = "@"+mods["@"].text+" "+statusText;
var updateUrl = "https://twitter.com/statuses/update.json";
var updateParams = {
source: "ubiquity",
status: statusText.slice(0, 140)
};
jQuery.ajax({
type: "POST",
url: updateUrl,
data: updateParams,
dataType: "json",
error: function() {
displayMessage("Twitter error - status not updated");
},
success: function() {
displayMessage("Twitter status updated");
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment