Showing posts with label twitter. Show all posts
Showing posts with label twitter. Show all posts

Friday, June 19, 2009

Twittering with Ubiquity

Although Firefox Ubiquity was released in August last year, it was only a couple of weeks ago that I really had a chance to look at it. It is kinda difficult to explain what Ubiquity is, so I will not. Instead, I recommend you watch this video. Ubiquity can be installed from here.

Mozilla has a lot of great documentation on how to use and extend Ubiquity commands. I would recommend you start from this page.

This post deals with posting 'tweets' with Ubiquity.
  • Posting a regular text twitter: This is pretty straight-forward, you just bring up Ubiquity and type twitter and whatever message you want to post.
  • Posting a selection: Select a section of text, bring up Ubiquity and type twitter. Your selection will make up the twitter content.
But I wanted more. I wanted to tweet about the song I am currently listening to. So, I hacked together a Ubiquity command for it. Paste the following in the command editor window (ubiquity command: command-editor)

/*
Post Current Song To Twitter
*/

const TWITTER_STATUS_MAXLEN = 160;

CmdUtils.CreateCommand({
name:"tweet-song",
_getSong: function() {
var trackData = window.foxytunesGetCurrentTrackData();
return CmdUtils.makeSugg("'" + trackData.title + "' by " + trackData.artist);
},

preview:function(previewBlock) {
var song = this._getSong();
var previewTemplate = "Updates your Twitter status to:
" +
"I am listening to ${status}

" +
"Characters remaining: ${chars}";

var truncateTemplate = "
The last ${truncate} " +
"characters will be truncated.";

var previewData = {
status: song.text,
chars: TWITTER_STATUS_MAXLEN - song.text.length
};

var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);

if (previewData.chars < 0) {
var truncateData = {
truncate: 0 - previewData.chars
};
previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
}
previewBlock.innerHTML = previewHTML;
},

execute:function() {
var statusText = this._getSong();
if (statusText.chars < 0) {
displayMessage("Twitter requires a status to be entered");
return;
}
var updateUrl = "https://twitter.com/statuses/update.json";
var updateParams = {
source: "ubiquity",
status: "I am listening to " + statusText.text
};

jQuery.ajax({
type:"POST",
url: updateUrl,
data: updateParams,
dataType: "json",
error:function() {
displayMessage("Twitter error - status not updated");
},
success:function() {
displayMessage("Twitter status updated.");
}
});
}
});


You will also need to install FoxyTunes to get this command to work. That is it.

Start up a song in iTunes or any other supported player, bring up Ubiquity and type 'tweet-song' and hit enter to post a tweet that looks like 'I am listening to 'Alex Gopher With Demon Presen' by Jazz Boutique (CD Series)' (Note: You may have to login with your twitter account for this to work).

Credits: The tweet-song code is based on work by Blair McBride (twitter command) and Abimanyu Raja (get-lyrics command).